embers-client 0.1.0

Client rendering, input handling, configuration, and scripting support for Embers.
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
use std::collections::{BTreeMap, VecDeque};
use std::path::Path;

use tracing::warn;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use embers_core::{
    ActivityState, BufferId, FloatGeometry, MuxError, NodeId, Point, Result, SessionId, Size,
};
use embers_protocol::{
    BufferLocation, BufferLocationAttachment, BufferRecord, BufferRequest, BufferResponse,
    ClientMessage, FloatingRequest, InputRequest, NodeRequest, ServerEvent, ServerResponse,
};

use crate::RenderGrid;
use crate::client::MuxClient;
use crate::config::ConfigManager;
use crate::controller::{KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use crate::input::{
    FallbackPolicy, InputResolution, InputState, KeyToken, NORMAL_MODE, SEARCH_MODE, SELECT_MODE,
    resolve_key,
};
use crate::presentation::{LeafFrame, NavigationDirection, PresentationModel};
use crate::renderer::Renderer;
use crate::scripting::{
    Action, BarSpec, Context, EventInfo, FloatingAnchor, FloatingGeometrySpec, FloatingSize,
    NotifyLevel, TabBarContext, TreeSpec,
};
use crate::state::{SearchMatch, SearchState, SelectionKind, SelectionPoint, SelectionState};
use crate::transport::Transport;

const WHEEL_SCROLL_LINES: u64 = 3;
const MAX_EXPANDED_ACTIONS: usize = 256;

#[derive(Clone, Debug, PartialEq, Eq)]
struct SearchPrompt {
    node_id: NodeId,
    query: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ResolvedTreeBuffer {
    Existing(BufferId),
    NewlySpawned(BufferId),
}

impl ResolvedTreeBuffer {
    fn id(self) -> BufferId {
        match self {
            Self::Existing(buffer_id) | Self::NewlySpawned(buffer_id) => buffer_id,
        }
    }

    fn created_by_helper(self) -> Option<BufferId> {
        match self {
            Self::Existing(_) => None,
            Self::NewlySpawned(buffer_id) => Some(buffer_id),
        }
    }
}

pub struct ConfiguredClient<T> {
    client: MuxClient<T>,
    config: ConfigManager,
    input_state: InputState,
    renderer: Renderer,
    notifications: Vec<String>,
    active_session_id: Option<SessionId>,
    viewport: Option<Size>,
    search_prompt: Option<SearchPrompt>,
    terminal_output: VecDeque<Vec<u8>>,
}

impl<T> ConfiguredClient<T>
where
    T: Transport,
{
    pub fn new(client: MuxClient<T>, config: ConfigManager) -> Self {
        Self {
            client,
            config,
            input_state: InputState::default(),
            renderer: Renderer,
            notifications: Vec::new(),
            active_session_id: None,
            viewport: None,
            search_prompt: None,
            terminal_output: VecDeque::new(),
        }
    }

    pub fn client(&self) -> &MuxClient<T> {
        &self.client
    }

    pub fn client_mut(&mut self) -> &mut MuxClient<T> {
        &mut self.client
    }

    pub fn config(&self) -> &ConfigManager {
        &self.config
    }

    pub fn notifications(&self) -> &[String] {
        &self.notifications
    }

    pub fn drain_terminal_output(&mut self) -> Vec<Vec<u8>> {
        self.terminal_output.drain(..).collect()
    }

    pub fn status_line(&self, session_id: SessionId, socket_path: &Path) -> String {
        let session_name = self
            .client
            .state()
            .sessions
            .get(&session_id)
            .map(|session| session.name.as_str())
            .unwrap_or("<missing>");
        if let Some(prompt) = &self.search_prompt {
            return format!("[{session_name}] /{}", prompt.query);
        }
        match self.notifications.last() {
            Some(message) => format!("[{session_name}] {message}"),
            None => format!("[{session_name}] {}  ctrl-q quit", socket_path.display()),
        }
    }

    pub async fn handle_key(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        key: KeyEvent,
    ) -> Result<()> {
        self.set_active_view(session_id, viewport);
        let presentation = self.prepare_presentation(session_id, viewport).await?;

        if self.input_state.current_mode() == SEARCH_MODE {
            return self
                .handle_search_key(session_id, viewport, &presentation, key)
                .await;
        }

        match key {
            KeyEvent::Bytes(bytes) => {
                if self.current_fallback_policy() != FallbackPolicy::Passthrough {
                    return Ok(());
                }
                let buffer_id = self.resolve_buffer_id(None, &presentation)?;
                self.send_bytes_to_buffer(buffer_id, session_id, bytes)
                    .await?;
                Ok(())
            }
            other => {
                let token = key_event_to_token(other)?;
                match resolve_key(
                    &self.config.active_script().loaded_config().bindings,
                    &self.config.active_script().loaded_config().modes,
                    &mut self.input_state,
                    token,
                ) {
                    InputResolution::ExactMatch(binding) => {
                        if self.should_passthrough_binding_in_alternate_screen(
                            &presentation,
                            &binding.target,
                        ) {
                            let buffer_id = self.resolve_buffer_id(None, &presentation)?;
                            return self
                                .send_bytes_to_buffer(
                                    buffer_id,
                                    session_id,
                                    sequence_to_bytes(&binding.sequence)?,
                                )
                                .await;
                        }
                        self.execute_actions(
                            Some(session_id),
                            Some(viewport),
                            binding.target.clone(),
                        )
                        .await
                    }
                    InputResolution::PrefixMatch => Ok(()),
                    InputResolution::Unmatched {
                        sequence,
                        fallback_policy,
                        ..
                    } => match fallback_policy {
                        FallbackPolicy::Passthrough => {
                            let buffer_id = self.resolve_buffer_id(None, &presentation)?;
                            self.send_bytes_to_buffer(
                                buffer_id,
                                session_id,
                                sequence_to_bytes(&sequence)?,
                            )
                            .await
                        }
                        FallbackPolicy::Ignore => Ok(()),
                    },
                }
            }
        }
    }

    pub async fn handle_paste(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        bytes: Vec<u8>,
    ) -> Result<()> {
        self.set_active_view(session_id, viewport);
        if self.input_state.current_mode() == SEARCH_MODE {
            return self.handle_search_paste(session_id, viewport, bytes).await;
        }
        if self.current_fallback_policy() != FallbackPolicy::Passthrough {
            return Ok(());
        }

        let presentation = self.prepare_presentation(session_id, viewport).await?;
        let buffer_id = self.resolve_buffer_id(None, &presentation)?;
        let bytes = if self
            .client
            .state()
            .snapshots
            .get(&buffer_id)
            .is_some_and(|snapshot| snapshot.bracketed_paste)
        {
            let mut wrapped = b"\x1b[200~".to_vec();
            wrapped.extend(bytes);
            wrapped.extend_from_slice(b"\x1b[201~");
            wrapped
        } else {
            bytes
        };
        self.send_bytes_to_buffer(buffer_id, session_id, bytes)
            .await
    }

    pub async fn handle_mouse(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        event: MouseEvent,
    ) -> Result<()> {
        self.set_active_view(session_id, viewport);
        let presentation = self.prepare_presentation(session_id, viewport).await?;
        let point = Point {
            x: i32::from(event.column),
            y: i32::from(event.row),
        };
        let Some(target_leaf) = self.mouse_target_leaf(&presentation, point).cloned() else {
            return Ok(());
        };

        let settings = self.config.active_script().loaded_config().mouse;
        let target_snapshot = self.client.state().snapshots.get(&target_leaf.buffer_id);
        let mouse_reporting = target_snapshot.is_some_and(|snapshot| snapshot.mouse_reporting);
        let point_in_content = point.y > target_leaf.rect.origin.y;

        match event.kind {
            MouseEventKind::WheelUp | MouseEventKind::WheelDown => {
                if mouse_reporting && settings.wheel_forward && point_in_content {
                    return self
                        .send_bytes_to_buffer(
                            target_leaf.buffer_id,
                            session_id,
                            encode_mouse_event(&target_leaf, event)?,
                        )
                        .await;
                }
                if settings.wheel_scroll && !self.view_is_alternate_screen(target_leaf.node_id) {
                    let delta = match event.kind {
                        MouseEventKind::WheelUp => -(WHEEL_SCROLL_LINES as i64),
                        MouseEventKind::WheelDown => WHEEL_SCROLL_LINES as i64,
                        _ => 0,
                    };
                    return self.scroll_view_by(target_leaf.node_id, delta).await;
                }
                Ok(())
            }
            MouseEventKind::Press(_) | MouseEventKind::Release(_) | MouseEventKind::Drag(_) => {
                if settings.click_focus && !target_leaf.focused {
                    self.focus_node(session_id, target_leaf.node_id).await?;
                }
                if settings.click_forward && mouse_reporting && point_in_content {
                    self.send_bytes_to_buffer(
                        target_leaf.buffer_id,
                        session_id,
                        encode_mouse_event(&target_leaf, event)?,
                    )
                    .await?;
                }
                Ok(())
            }
        }
    }

    pub async fn handle_focus_event(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        focused: bool,
    ) -> Result<()> {
        self.set_active_view(session_id, viewport);
        let presentation = self.prepare_presentation(session_id, viewport).await?;
        let buffer_id = self.resolve_buffer_id(None, &presentation)?;
        if !self
            .client
            .state()
            .snapshots
            .get(&buffer_id)
            .is_some_and(|snapshot| snapshot.focus_reporting)
        {
            return Ok(());
        }

        let bytes = if focused {
            b"\x1b[I".to_vec()
        } else {
            b"\x1b[O".to_vec()
        };
        self.send_bytes_to_buffer(buffer_id, session_id, bytes)
            .await
    }

    pub async fn process_next_event(&mut self) -> Result<ServerEvent> {
        let event = self.next_event().await?;
        self.handle_event(&event).await?;
        Ok(event)
    }

    pub async fn process_next_event_timeout(
        &mut self,
        timeout: std::time::Duration,
    ) -> Result<Option<ServerEvent>> {
        let Some(event) = tokio::time::timeout(timeout, self.client.next_event())
            .await
            .ok()
            .transpose()?
        else {
            return Ok(None);
        };
        self.handle_event(&event).await?;
        Ok(Some(event))
    }

    pub async fn next_event(&mut self) -> Result<ServerEvent> {
        self.client.next_event().await
    }

    pub async fn handle_event(&mut self, event: &ServerEvent) -> Result<()> {
        let previous_render_activity = match event {
            ServerEvent::RenderInvalidated(render) => self
                .client
                .state()
                .buffers
                .get(&render.buffer_id)
                .map(|buffer| buffer.activity),
            _ => None,
        };
        let detached_session_id = matches!(event, ServerEvent::BufferDetached(_))
            .then(|| self.event_session_id(event))
            .flatten();
        self.client.handle_event(event).await?;
        if let ServerEvent::RenderInvalidated(event) = event {
            self.client.refresh_buffer_snapshot(event.buffer_id).await?;
        }
        let session_id = detached_session_id.or_else(|| self.event_session_id(event));

        let mut event_names = vec![event_name(event).to_owned()];
        if let ServerEvent::RenderInvalidated(render) = event
            && previous_render_activity != Some(ActivityState::Bell)
            && self
                .client
                .state()
                .buffers
                .get(&render.buffer_id)
                .is_some_and(|buffer| buffer.activity == ActivityState::Bell)
        {
            event_names.push("buffer_bell".to_owned());
        }

        for event_name in event_names {
            let context = self.context_for(
                session_id,
                self.viewport,
                Some(event_info(&event_name, event, session_id)),
            );
            match self
                .config
                .active_script()
                .dispatch_event(&event_name, context)
            {
                Ok(actions) if !actions.is_empty() => {
                    self.execute_actions(session_id, self.viewport, actions)
                        .await?;
                }
                Ok(_) => {}
                Err(error) => self.record_notification(error.to_string()),
            }
        }
        Ok(())
    }

    pub async fn render_session(
        &mut self,
        session_id: SessionId,
        viewport: Size,
    ) -> Result<RenderGrid> {
        self.set_active_view(session_id, viewport);
        let presentation = self.prepare_presentation(session_id, viewport).await?;
        let mut custom_bars = BTreeMap::<embers_core::NodeId, BarSpec>::new();
        let mut recorded_formatter_error = false;
        for tabs in &presentation.tab_bars {
            let bar_context =
                TabBarContext::from_frame(tabs, self.input_state.current_mode(), viewport.width);
            let result = self.config.active_script().format_tab_bar(bar_context);

            match result {
                Ok(Some(bar)) => {
                    custom_bars.insert(tabs.node_id, bar);
                }
                Ok(None) => {}
                Err(error) if !recorded_formatter_error => {
                    recorded_formatter_error = true;
                    self.record_notification(error.to_string());
                }
                Err(_) => {}
            }
        }

        Ok(self
            .renderer
            .render_with_tab_bars(self.client.state(), &presentation, &custom_bars))
    }

    pub fn reload_config(&mut self) -> Result<()> {
        let current_mode = self.input_state.current_mode().to_owned();
        self.config
            .reload()
            .map_err(|error| MuxError::invalid_input(error.to_string()))?;
        self.finish_config_reload(&current_mode);
        Ok(())
    }

    pub fn reload_config_if_changed(&mut self) -> Result<bool> {
        match self.config.reload_if_changed() {
            Ok(false) => Ok(false),
            Ok(true) => {
                let current_mode = self.input_state.current_mode().to_owned();
                self.finish_config_reload(&current_mode);
                Ok(true)
            }
            Err(error) => {
                let message = error.to_string();
                self.record_notification(message.clone());
                Err(MuxError::invalid_input(message))
            }
        }
    }

    fn finish_config_reload(&mut self, current_mode: &str) {
        if self
            .config
            .active_script()
            .loaded_config()
            .modes
            .contains_key(current_mode)
        {
            self.input_state.clear_pending();
        } else {
            self.input_state.set_mode(NORMAL_MODE);
            self.search_prompt = None;
        }
    }

    async fn execute_actions(
        &mut self,
        session_id: Option<SessionId>,
        viewport: Option<Size>,
        actions: Vec<Action>,
    ) -> Result<()> {
        let mut pending = VecDeque::from(actions);
        let mut expansions = 0usize;
        let mut current_session_id = session_id;
        let current_viewport = viewport;
        while let Some(action) = pending.pop_front() {
            let result = match action {
                Action::Noop => Ok(()),
                Action::Chain(actions) => {
                    prepend_actions_with_limit(&mut pending, actions, &mut expansions)
                }
                Action::RunNamedAction { name } => {
                    match self.config.active_script().run_named_action(
                        &name,
                        self.context_for(current_session_id, current_viewport, None),
                    ) {
                        Ok(actions) => {
                            prepend_actions_with_limit(&mut pending, actions, &mut expansions)
                        }
                        Err(error) => Err(MuxError::invalid_input(error.to_string())),
                    }
                }
                Action::EnterMode { mode } => {
                    let actions = self
                        .transition_mode(mode, current_session_id, current_viewport)
                        .await?;
                    prepend_actions_with_limit(&mut pending, actions, &mut expansions)
                }
                Action::LeaveMode => {
                    let actions = self
                        .transition_mode(
                            NORMAL_MODE.to_owned(),
                            current_session_id,
                            current_viewport,
                        )
                        .await?;
                    prepend_actions_with_limit(&mut pending, actions, &mut expansions)
                }
                Action::ToggleMode { mode } => {
                    let next_mode = if self.input_state.current_mode() == mode {
                        NORMAL_MODE.to_owned()
                    } else {
                        mode
                    };
                    let actions = self
                        .transition_mode(next_mode, current_session_id, current_viewport)
                        .await?;
                    prepend_actions_with_limit(&mut pending, actions, &mut expansions)
                }
                Action::ClearPendingKeys => {
                    self.input_state.clear_pending();
                    Ok(())
                }
                Action::Notify { level, message } => {
                    self.record_notification(format_notification(level, &message));
                    Ok(())
                }
                Action::FocusBuffer { buffer_id } => {
                    let location = Self::buffer_location_from_response(
                        self.client
                            .request_message(ClientMessage::Buffer(BufferRequest::GetLocation {
                                request_id: self.client.next_request_id(),
                                buffer_id,
                            }))
                            .await?,
                        "buffer focus",
                    )?;
                    let (session_id, node_id) =
                        Self::attached_buffer_location(Some(buffer_id), location, "buffer focus")?;
                    self.focus_node_with_shortcut(
                        session_id,
                        node_id,
                        self.active_session_id == Some(session_id),
                    )
                    .await?;
                    current_session_id = Some(session_id);
                    self.active_session_id = Some(session_id);
                    if let Some(viewport) = current_viewport {
                        self.set_active_view(session_id, viewport);
                    }
                    self.client.resync_all_sessions().await
                }
                Action::RevealBuffer { buffer_id } => {
                    let location = Self::buffer_location_from_response(
                        self.client
                            .request_message(ClientMessage::Buffer(BufferRequest::Reveal {
                                request_id: self.client.next_request_id(),
                                buffer_id,
                                client_id: None,
                            }))
                            .await?,
                        "buffer reveal",
                    )?;
                    let (session_id, _) =
                        Self::attached_buffer_location(Some(buffer_id), location, "buffer reveal")?;
                    current_session_id = Some(session_id);
                    self.active_session_id = Some(session_id);
                    if let Some(viewport) = current_viewport {
                        self.set_active_view(session_id, viewport);
                    }
                    self.client.resync_all_sessions().await
                }
                Action::OpenBufferHistory {
                    buffer_id,
                    scope,
                    placement,
                } => {
                    let location = Self::buffer_location_from_response(
                        self.client
                            .request_message(ClientMessage::Buffer(BufferRequest::OpenHistory {
                                request_id: self.client.next_request_id(),
                                buffer_id,
                                scope,
                                placement,
                                client_id: None,
                            }))
                            .await?,
                        "buffer history",
                    )?;
                    let (session_id, _) =
                        Self::attached_buffer_location(None, location, "buffer history")?;
                    current_session_id = Some(session_id);
                    self.active_session_id = Some(session_id);
                    if let Some(viewport) = current_viewport {
                        self.set_active_view(session_id, viewport);
                    }
                    self.client.resync_all_sessions().await
                }
                action => {
                    match self
                        .execute_without_presentation(current_session_id, action)
                        .await
                    {
                        Ok(None) => Ok(()),
                        Ok(Some(action)) => {
                            if matches!(action, Action::UnzoomNode { .. })
                                && current_session_id.is_none()
                            {
                                return Err(MuxError::invalid_input(format!(
                                    "cannot execute action {action:?} without current_session_id"
                                )));
                            }
                            let mut missing = Vec::new();
                            if current_session_id.is_none() {
                                missing.push("current_session_id");
                            }
                            if current_viewport.is_none() {
                                missing.push("current_viewport");
                            }
                            if !missing.is_empty() {
                                return Err(MuxError::invalid_input(format!(
                                    "cannot execute action {action:?} without {}",
                                    missing.join(" and ")
                                )));
                            }
                            let session_id = current_session_id.expect("checked current session");
                            let viewport = current_viewport.expect("checked current viewport");
                            let presentation =
                                self.prepare_presentation(session_id, viewport).await?;
                            self.execute_action(session_id, viewport, &presentation, action)
                                .await
                        }
                        Err(error) => Err(error),
                    }
                }
            };
            if let Err(error) = result {
                self.record_notification(error.to_string());
            }
        }
        Ok(())
    }

    async fn execute_without_presentation(
        &mut self,
        current_session_id: Option<SessionId>,
        action: Action,
    ) -> Result<Option<Action>> {
        match action {
            Action::CloseFloating {
                floating_id: Some(floating_id),
            } => {
                self.client
                    .request_message(ClientMessage::Floating(FloatingRequest::Close {
                        request_id: self.client.next_request_id(),
                        floating_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::CloseView {
                node_id: Some(node_id),
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::Close {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::KillBuffer {
                buffer_id: Some(buffer_id),
            } => {
                self.client
                    .request_message(ClientMessage::Buffer(BufferRequest::Kill {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                        force: false,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::DetachBuffer {
                buffer_id: Some(buffer_id),
            } => {
                self.client
                    .request_message(ClientMessage::Buffer(BufferRequest::Detach {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::MoveBufferToNode { buffer_id, node_id } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveBufferToNode {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                        target_leaf_node_id: node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::ZoomNode {
                node_id: Some(node_id),
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::Zoom {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::UnzoomNode {
                session_id: target_session_id,
            } => {
                let Some(session_id) = target_session_id.or(current_session_id) else {
                    return Ok(Some(Action::UnzoomNode {
                        session_id: target_session_id,
                    }));
                };
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::Unzoom {
                        request_id: self.client.next_request_id(),
                        session_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::ToggleZoomNode {
                node_id: Some(node_id),
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::ToggleZoom {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::SwapSiblingNodes {
                first_node_id: Some(first_node_id),
                second_node_id,
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::SwapSiblings {
                        request_id: self.client.next_request_id(),
                        first_node_id,
                        second_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::BreakNode {
                node_id: Some(node_id),
                destination,
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::BreakNode {
                        request_id: self.client.next_request_id(),
                        node_id,
                        destination,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::JoinBufferAtNode {
                node_id: Some(node_id),
                buffer_id,
                placement,
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::JoinBufferAtNode {
                        request_id: self.client.next_request_id(),
                        node_id,
                        buffer_id,
                        placement,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::MoveNodeBefore {
                node_id: Some(node_id),
                sibling_node_id,
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveNodeBefore {
                        request_id: self.client.next_request_id(),
                        node_id,
                        sibling_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            Action::MoveNodeAfter {
                node_id: Some(node_id),
                sibling_node_id,
            } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveNodeAfter {
                        request_id: self.client.next_request_id(),
                        node_id,
                        sibling_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await?;
                Ok(None)
            }
            other => Ok(Some(other)),
        }
    }

    async fn transition_mode(
        &mut self,
        mode: String,
        session_id: Option<SessionId>,
        viewport: Option<Size>,
    ) -> Result<Vec<Action>> {
        if !self
            .config
            .active_script()
            .loaded_config()
            .modes
            .contains_key(&mode)
        {
            return Err(MuxError::invalid_input(format!("unknown mode '{mode}'")));
        }

        let previous_mode = self.input_state.current_mode().to_owned();
        if previous_mode == mode {
            return Ok(Vec::new());
        }

        let mut actions = self
            .config
            .active_script()
            .run_leave_hook(&previous_mode, self.context_for(session_id, viewport, None))
            .map_err(|error| MuxError::invalid_input(error.to_string()))?;
        self.input_state.set_mode(mode.clone());
        actions.extend(
            self.config
                .active_script()
                .run_enter_hook(&mode, self.context_for(session_id, viewport, None))
                .map_err(|error| {
                    self.input_state.set_mode(previous_mode.clone());
                    MuxError::invalid_input(error.to_string())
                })?,
        );
        Ok(actions)
    }

    async fn execute_action(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        presentation: &PresentationModel,
        action: Action,
    ) -> Result<()> {
        match action {
            Action::FocusDirection { direction } => {
                let Some(node_id) = presentation.focus_target(direction) else {
                    return Ok(());
                };
                self.focus_node(session_id, node_id).await
            }
            Action::ScrollLineUp => {
                let leaf = self.focused_leaf(presentation)?;
                self.scroll_view_by(leaf.node_id, -1).await
            }
            Action::ScrollLineDown => {
                let leaf = self.focused_leaf(presentation)?;
                self.scroll_view_by(leaf.node_id, 1).await
            }
            Action::ScrollPageUp => {
                let leaf = self.focused_leaf(presentation)?;
                let page = self
                    .client
                    .state()
                    .view_state(leaf.node_id)
                    .map(|state| i64::from(state.visible_line_count.max(1)))
                    .unwrap_or(1);
                self.scroll_view_by(leaf.node_id, -page).await
            }
            Action::ScrollPageDown => {
                let leaf = self.focused_leaf(presentation)?;
                let page = self
                    .client
                    .state()
                    .view_state(leaf.node_id)
                    .map(|state| i64::from(state.visible_line_count.max(1)))
                    .unwrap_or(1);
                self.scroll_view_by(leaf.node_id, page).await
            }
            Action::ScrollToTop => {
                let leaf = self.focused_leaf(presentation)?;
                self.set_view_scroll_top(leaf.node_id, 0).await
            }
            Action::ScrollToBottom | Action::FollowOutput => {
                let leaf = self.focused_leaf(presentation)?;
                self.follow_output_for_view(leaf.node_id).await
            }
            Action::EnterSearchMode => {
                self.enter_search_mode(session_id, viewport, presentation)
                    .await
            }
            Action::SearchNext => self.navigate_search(presentation, true).await,
            Action::SearchPrev => self.navigate_search(presentation, false).await,
            Action::CommitSearch => self.commit_search_prompt(session_id, viewport).await,
            Action::CancelSearch => self.cancel_search_prompt(session_id, viewport).await,
            Action::EnterSelect { kind } => {
                self.enter_select_mode(session_id, viewport, presentation, kind)
                    .await
            }
            Action::SelectMove { direction } => {
                let leaf = self.focused_leaf(presentation)?;
                self.move_selection(leaf.node_id, direction).await
            }
            Action::CopySelection => {
                let leaf = self.focused_leaf(presentation)?;
                self.copy_selection(session_id, viewport, leaf.node_id)
                    .await
            }
            Action::CancelSelection => {
                let leaf = self.focused_leaf(presentation)?;
                self.cancel_selection(session_id, viewport, leaf.node_id)
                    .await
            }
            Action::SelectTab {
                tabs_node_id,
                index,
            } => {
                let tabs = self.resolve_tabs_target(presentation, tabs_node_id)?;
                if index >= tabs.tabs.len() {
                    return Err(MuxError::invalid_input(format!(
                        "tab index {index} is out of range for {} tabs",
                        tabs.tabs.len()
                    )));
                }
                let index = u32::try_from(index).map_err(|_| {
                    MuxError::invalid_input(format!("tab index {index} exceeds protocol limits"))
                })?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::SelectTab {
                        request_id: self.client.next_request_id(),
                        tabs_node_id: tabs.node_id,
                        index,
                    }))
                    .await?;
                self.client.resync_session(session_id).await
            }
            Action::NextTab { tabs_node_id } => {
                let tabs = self.resolve_tabs_target(presentation, tabs_node_id)?;
                if tabs.tabs.is_empty() {
                    return Ok(());
                }
                let index = (tabs.active + 1) % tabs.tabs.len();
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::SelectTab {
                        request_id: self.client.next_request_id(),
                        tabs_node_id: tabs.node_id,
                        index: u32::try_from(index).map_err(|_| {
                            MuxError::invalid_input("tab index exceeds protocol limits")
                        })?,
                    }))
                    .await?;
                self.client.resync_session(session_id).await
            }
            Action::PrevTab { tabs_node_id } => {
                let tabs = self.resolve_tabs_target(presentation, tabs_node_id)?;
                if tabs.tabs.is_empty() {
                    return Ok(());
                }
                let index = if tabs.active == 0 {
                    tabs.tabs.len() - 1
                } else {
                    tabs.active - 1
                };
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::SelectTab {
                        request_id: self.client.next_request_id(),
                        tabs_node_id: tabs.node_id,
                        index: u32::try_from(index).map_err(|_| {
                            MuxError::invalid_input("tab index exceeds protocol limits")
                        })?,
                    }))
                    .await?;
                self.client.resync_session(session_id).await
            }
            Action::SplitCurrent {
                direction,
                new_child,
            } => {
                let focused_leaf = presentation
                    .focused_leaf()
                    .ok_or_else(|| MuxError::invalid_input("no focused leaf to split"))?;
                let buffer = self
                    .resolve_tree_buffer(session_id, presentation, new_child)
                    .await?;
                let result = self
                    .client
                    .request_message(ClientMessage::Node(NodeRequest::Split {
                        request_id: self.client.next_request_id(),
                        leaf_node_id: focused_leaf.node_id,
                        direction,
                        new_buffer_id: buffer.id(),
                    }))
                    .await;
                rollback_created_buffer_on_error(
                    self,
                    buffer.created_by_helper(),
                    "split pane",
                    result,
                )
                .await?;
                self.client.resync_all_sessions().await
            }
            Action::OpenFloating { spec } => {
                let buffer = self
                    .resolve_tree_buffer(session_id, presentation, spec.tree)
                    .await?;
                let result = self
                    .client
                    .request_message(ClientMessage::Floating(FloatingRequest::Create {
                        request_id: self.client.next_request_id(),
                        session_id,
                        root_node_id: None,
                        buffer_id: Some(buffer.id()),
                        geometry: resolve_floating_geometry(spec.geometry, viewport),
                        title: spec.title,
                        focus: spec.focus,
                        close_on_empty: spec.close_on_empty,
                    }))
                    .await;
                rollback_created_buffer_on_error(
                    self,
                    buffer.created_by_helper(),
                    "open floating window",
                    result,
                )
                .await?;
                self.client.resync_all_sessions().await
            }
            Action::DetachBuffer { buffer_id } => {
                let buffer_id = self.resolve_buffer_id(buffer_id, presentation)?;
                self.client
                    .request_message(ClientMessage::Buffer(BufferRequest::Detach {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::KillBuffer { buffer_id } => {
                let buffer_id = self.resolve_buffer_id(buffer_id, presentation)?;
                self.client
                    .request_message(ClientMessage::Buffer(BufferRequest::Kill {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                        force: false,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::SendKeys { buffer_id, keys } => {
                let buffer_id = self.resolve_buffer_id(buffer_id, presentation)?;
                self.send_bytes_to_buffer(buffer_id, session_id, sequence_to_bytes(&keys)?)
                    .await
            }
            Action::SendBytes { buffer_id, bytes } => {
                let buffer_id = self.resolve_buffer_id(buffer_id, presentation)?;
                self.send_bytes_to_buffer(buffer_id, session_id, bytes)
                    .await
            }
            Action::CloseFloating { floating_id } => {
                let floating_id = floating_id
                    .or_else(|| presentation.focused_floating_id())
                    .ok_or_else(|| MuxError::invalid_input("no floating window is focused"))?;
                self.client
                    .request_message(ClientMessage::Floating(FloatingRequest::Close {
                        request_id: self.client.next_request_id(),
                        floating_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::CloseView { node_id } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to close"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::Close {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::InsertTabAfter {
                tabs_node_id,
                title,
                child,
            } => {
                let tabs = self.resolve_tabs_target(presentation, tabs_node_id)?;
                let buffer = self
                    .resolve_tree_buffer(session_id, presentation, child)
                    .await?;
                let result = self
                    .client
                    .request_message(ClientMessage::Node(NodeRequest::AddTab {
                        request_id: self.client.next_request_id(),
                        tabs_node_id: tabs.node_id,
                        title: title.unwrap_or_else(|| "tab".to_owned()),
                        buffer_id: Some(buffer.id()),
                        child_node_id: None,
                        index: u32::try_from(tabs.active.saturating_add(1)).map_err(|_| {
                            MuxError::invalid_input("tab index exceeds protocol limits")
                        })?,
                    }))
                    .await;
                rollback_created_buffer_on_error(
                    self,
                    buffer.created_by_helper(),
                    "insert tab",
                    result,
                )
                .await?;
                self.client.resync_all_sessions().await
            }
            Action::InsertTabBefore {
                tabs_node_id,
                title,
                child,
            } => {
                let tabs = self.resolve_tabs_target(presentation, tabs_node_id)?;
                let buffer = self
                    .resolve_tree_buffer(session_id, presentation, child)
                    .await?;
                let result = self
                    .client
                    .request_message(ClientMessage::Node(NodeRequest::AddTab {
                        request_id: self.client.next_request_id(),
                        tabs_node_id: tabs.node_id,
                        title: title.unwrap_or_else(|| "tab".to_owned()),
                        buffer_id: Some(buffer.id()),
                        child_node_id: None,
                        index: u32::try_from(tabs.active).map_err(|_| {
                            MuxError::invalid_input("tab index exceeds protocol limits")
                        })?,
                    }))
                    .await;
                rollback_created_buffer_on_error(
                    self,
                    buffer.created_by_helper(),
                    "insert tab",
                    result,
                )
                .await?;
                self.client.resync_all_sessions().await
            }
            Action::ReplaceNode { node_id, tree } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to replace"))?;
                let buffer = self
                    .resolve_tree_buffer(session_id, presentation, tree)
                    .await?;
                let result = self
                    .client
                    .request_message(ClientMessage::Node(NodeRequest::MoveBufferToNode {
                        request_id: self.client.next_request_id(),
                        buffer_id: buffer.id(),
                        target_leaf_node_id: node_id,
                    }))
                    .await;
                rollback_created_buffer_on_error(
                    self,
                    buffer.created_by_helper(),
                    "replace node buffer",
                    result,
                )
                .await?;
                self.client.resync_all_sessions().await
            }
            Action::MoveBufferToNode { buffer_id, node_id } => {
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveBufferToNode {
                        request_id: self.client.next_request_id(),
                        buffer_id,
                        target_leaf_node_id: node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::MoveBufferToFloating {
                buffer_id,
                geometry,
                title,
                focus,
                close_on_empty,
            } => {
                self.client
                    .request_message(ClientMessage::Floating(FloatingRequest::Create {
                        request_id: self.client.next_request_id(),
                        session_id,
                        root_node_id: None,
                        buffer_id: Some(buffer_id),
                        geometry: resolve_floating_geometry(geometry, viewport),
                        title,
                        focus,
                        close_on_empty,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::ZoomNode { node_id: None } => {
                let node_id = presentation
                    .focused_leaf()
                    .map(|leaf| leaf.node_id)
                    .ok_or_else(|| MuxError::invalid_input("no focused node to zoom"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::Zoom {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::ToggleZoomNode { node_id } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to toggle zoom"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::ToggleZoom {
                        request_id: self.client.next_request_id(),
                        node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::SwapSiblingNodes {
                first_node_id,
                second_node_id,
            } => {
                let first_node_id = first_node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to swap"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::SwapSiblings {
                        request_id: self.client.next_request_id(),
                        first_node_id,
                        second_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::BreakNode {
                node_id,
                destination,
            } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to break"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::BreakNode {
                        request_id: self.client.next_request_id(),
                        node_id,
                        destination,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::JoinBufferAtNode {
                node_id,
                buffer_id,
                placement,
            } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| {
                        MuxError::invalid_input("no focused node to join buffer into")
                    })?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::JoinBufferAtNode {
                        request_id: self.client.next_request_id(),
                        node_id,
                        buffer_id,
                        placement,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::MoveNodeBefore {
                node_id,
                sibling_node_id,
            } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to reorder"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveNodeBefore {
                        request_id: self.client.next_request_id(),
                        node_id,
                        sibling_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::MoveNodeAfter {
                node_id,
                sibling_node_id,
            } => {
                let node_id = node_id
                    .or_else(|| presentation.focused_leaf().map(|leaf| leaf.node_id))
                    .ok_or_else(|| MuxError::invalid_input("no focused node to reorder"))?;
                self.client
                    .request_message(ClientMessage::Node(NodeRequest::MoveNodeAfter {
                        request_id: self.client.next_request_id(),
                        node_id,
                        sibling_node_id,
                    }))
                    .await?;
                self.client.resync_all_sessions().await
            }
            Action::FocusBuffer { .. }
            | Action::RevealBuffer { .. }
            | Action::OpenBufferHistory { .. }
            | Action::UnzoomNode { .. }
            | Action::ZoomNode { node_id: Some(_) } => Err(MuxError::invalid_input(
                "action should be handled before presentation is required",
            )),
            other => Err(MuxError::invalid_input(format!(
                "action '{other:?}' is not supported by the live executor yet"
            ))),
        }
    }

    async fn resolve_tree_buffer(
        &self,
        _session_id: SessionId,
        presentation: &PresentationModel,
        tree: TreeSpec,
    ) -> Result<ResolvedTreeBuffer> {
        match tree {
            TreeSpec::BufferCurrent => presentation
                .focused_buffer_id()
                .map(ResolvedTreeBuffer::Existing)
                .ok_or_else(|| MuxError::invalid_input("no current buffer is focused")),
            TreeSpec::BufferAttach { buffer_id } => Ok(ResolvedTreeBuffer::Existing(buffer_id)),
            TreeSpec::BufferSpawn(spec) => self
                .create_buffer(spec)
                .await
                .map(ResolvedTreeBuffer::NewlySpawned),
            TreeSpec::BufferEmpty => self
                .create_buffer(crate::scripting::BufferSpawnSpec {
                    title: Some("shell".to_owned()),
                    command: default_shell_command(),
                    cwd: None,
                    env: Default::default(),
                })
                .await
                .map(ResolvedTreeBuffer::NewlySpawned),
            other => Err(MuxError::invalid_input(format!(
                "tree '{other:?}' is not supported by the live executor yet"
            ))),
        }
    }

    async fn create_buffer(&self, spec: crate::scripting::BufferSpawnSpec) -> Result<BufferId> {
        let response = self
            .client
            .request_message(ClientMessage::Buffer(BufferRequest::Create {
                request_id: self.client.next_request_id(),
                title: spec.title,
                command: spec.command,
                cwd: spec.cwd,
                env: spec.env,
            }))
            .await?;

        match response {
            ServerResponse::Buffer(BufferResponse { buffer, .. }) => Ok(buffer.id),
            other => Err(MuxError::protocol(format!(
                "expected buffer response, got {other:?}"
            ))),
        }
    }

    async fn rollback_created_buffer(&mut self, buffer_id: BufferId, operation: &str) {
        if let Err(error) = self
            .client
            .request_message(ClientMessage::Buffer(BufferRequest::Detach {
                request_id: self.client.next_request_id(),
                buffer_id,
            }))
            .await
        {
            warn!(
                %buffer_id,
                %error,
                operation,
                "failed to detach created buffer during rollback"
            );
        }
        if let Err(error) = self
            .client
            .request_message(ClientMessage::Buffer(BufferRequest::Kill {
                request_id: self.client.next_request_id(),
                buffer_id,
                force: true,
            }))
            .await
        {
            warn!(
                %buffer_id,
                %error,
                operation,
                "failed to kill created buffer during rollback"
            );
        }
    }

    async fn send_bytes_to_buffer(
        &mut self,
        buffer_id: BufferId,
        session_id: SessionId,
        bytes: Vec<u8>,
    ) -> Result<()> {
        self.client
            .request_message(ClientMessage::Input(InputRequest::Send {
                request_id: self.client.next_request_id(),
                buffer_id,
                bytes,
            }))
            .await?;
        self.client.refresh_buffer_snapshot(buffer_id).await?;
        self.client.resync_session(session_id).await
    }

    fn resolve_buffer_id(
        &self,
        buffer_id: Option<BufferId>,
        presentation: &PresentationModel,
    ) -> Result<BufferId> {
        match buffer_id {
            Some(buffer_id) => Ok(buffer_id),
            None => presentation
                .focused_buffer_id()
                .ok_or_else(|| MuxError::invalid_input("no current buffer is focused")),
        }
    }

    fn resolve_tabs_target(
        &self,
        presentation: &PresentationModel,
        tabs_node_id: Option<embers_core::NodeId>,
    ) -> Result<crate::TabsFrame> {
        match tabs_node_id {
            Some(tabs_node_id) => presentation
                .tab_bars
                .iter()
                .find(|tabs| tabs.node_id == tabs_node_id)
                .cloned()
                .or_else(|| {
                    self.client
                        .state()
                        .nodes
                        .get(&tabs_node_id)
                        .and_then(|node| {
                            node.tabs.as_ref().map(|tabs| crate::TabsFrame {
                                node_id: tabs_node_id,
                                rect: embers_core::Rect::default(),
                                tabs: tabs
                                    .tabs
                                    .iter()
                                    .enumerate()
                                    .map(|(index, tab)| crate::TabItem {
                                        title: tab.title.clone(),
                                        child_id: tab.child_id,
                                        active: usize::try_from(tabs.active).ok() == Some(index),
                                        activity: ActivityState::Idle,
                                        buffer_count: crate::presentation::subtree_buffer_count(
                                            self.client.state(),
                                            tab.child_id,
                                        ),
                                    })
                                    .collect(),
                                active: usize::try_from(tabs.active).unwrap_or(0),
                                is_root: self
                                    .client
                                    .state()
                                    .sessions
                                    .values()
                                    .any(|session| session.root_node_id == tabs_node_id),
                                floating_id: None,
                            })
                        })
                })
                .ok_or_else(|| MuxError::invalid_input(format!("node {tabs_node_id} is not tabs"))),
            None => presentation
                .focused_tabs()
                .cloned()
                .ok_or_else(|| MuxError::invalid_input("no focused tabs to select from")),
        }
    }

    async fn prepare_presentation(
        &mut self,
        session_id: SessionId,
        viewport: Size,
    ) -> Result<PresentationModel> {
        let mut presentation =
            PresentationModel::project(self.client.state(), session_id, viewport)?;
        let invalidated = presentation
            .leaves
            .iter()
            .filter(|leaf| {
                self.client
                    .state()
                    .invalidated_buffers
                    .contains(&leaf.buffer_id)
            })
            .map(|leaf| leaf.buffer_id)
            .collect::<Vec<_>>();
        for buffer_id in invalidated {
            self.client.refresh_buffer_snapshot(buffer_id).await?;
        }
        if !self.client.state().invalidated_buffers.is_empty() {
            presentation = PresentationModel::project(self.client.state(), session_id, viewport)?;
        }
        self.refresh_local_viewports(&presentation).await?;
        Ok(presentation)
    }

    fn context_for(
        &self,
        session_id: Option<SessionId>,
        viewport: Option<Size>,
        event: Option<EventInfo>,
    ) -> Context {
        let context = if let Some((session_id, viewport)) = session_id.zip(viewport)
            && let Ok(presentation) =
                PresentationModel::project(self.client.state(), session_id, viewport)
        {
            Context::from_state_with_mode(
                self.client.state(),
                Some(&presentation),
                self.input_state.current_mode(),
                None,
                None,
                None,
                None,
            )
        } else {
            Context::from_state_with_mode(
                self.client.state(),
                None,
                self.input_state.current_mode(),
                session_id,
                None,
                None,
                None,
            )
        };
        if let Some(event) = event {
            context.with_event(event)
        } else {
            context
        }
    }

    fn event_session_id(&self, event: &ServerEvent) -> Option<SessionId> {
        event.session_id().or_else(|| match event {
            ServerEvent::BufferCreated(event) => self.session_id_for_buffer_record(&event.buffer),
            ServerEvent::BufferPipeChanged(event) => {
                self.session_id_for_buffer_record(&event.buffer)
            }
            ServerEvent::BufferDetached(event) => self.session_id_for_buffer(event.buffer_id),
            ServerEvent::RenderInvalidated(event) => self.session_id_for_buffer(event.buffer_id),
            ServerEvent::SessionCreated(_)
            | ServerEvent::SessionClosed(_)
            | ServerEvent::SessionRenamed(_)
            | ServerEvent::NodeChanged(_)
            | ServerEvent::FloatingChanged(_)
            | ServerEvent::FocusChanged(_) => None,
            ServerEvent::ClientChanged(event) => event.client.current_session_id,
        })
    }

    fn session_id_for_buffer_record(&self, buffer: &BufferRecord) -> Option<SessionId> {
        buffer
            .attachment_node_id
            .and_then(|node_id| self.session_id_for_node(node_id))
            .or_else(|| self.session_id_for_buffer(buffer.id))
    }

    fn session_id_for_buffer(&self, buffer_id: BufferId) -> Option<SessionId> {
        let state = self.client.state();
        state
            .buffers
            .get(&buffer_id)
            .and_then(|buffer| buffer.attachment_node_id)
            .and_then(|node_id| state.nodes.get(&node_id))
            .map(|node| node.session_id)
            .or_else(|| {
                state.nodes.values().find_map(|node| {
                    node.buffer_view
                        .as_ref()
                        .filter(|view| view.buffer_id == buffer_id)
                        .map(|_| node.session_id)
                })
            })
    }

    fn session_id_for_node(&self, node_id: NodeId) -> Option<SessionId> {
        self.client
            .state()
            .nodes
            .get(&node_id)
            .map(|node| node.session_id)
    }

    fn buffer_location_from_response(
        response: ServerResponse,
        context: &str,
    ) -> Result<BufferLocation> {
        match response {
            ServerResponse::BufferLocation(response) => Ok(response.location),
            ServerResponse::BufferWithLocation(response) => {
                let (_, _, location, _) = response.into_parts();
                Ok(location)
            }
            other => Err(MuxError::protocol(format!(
                "expected {context} response, got {other:?}"
            ))),
        }
    }

    fn attached_buffer_location(
        expected_buffer_id: Option<BufferId>,
        location: BufferLocation,
        context: &str,
    ) -> Result<(SessionId, NodeId)> {
        if let Some(expected_buffer_id) = expected_buffer_id
            && location.buffer_id != expected_buffer_id
        {
            return Err(MuxError::protocol(format!(
                "{context} returned location for buffer {} while acting on buffer {expected_buffer_id}",
                location.buffer_id
            )));
        }

        match location.attachment {
            BufferLocationAttachment::Session {
                session_id,
                node_id,
            }
            | BufferLocationAttachment::Floating {
                session_id,
                node_id,
                ..
            } => Ok((session_id, node_id)),
            BufferLocationAttachment::Detached => Err(MuxError::conflict(format!(
                "{context} returned a detached location for buffer {}",
                expected_buffer_id.unwrap_or(location.buffer_id)
            ))),
        }
    }

    fn set_active_view(&mut self, session_id: SessionId, viewport: Size) {
        self.active_session_id = Some(session_id);
        self.viewport = Some(viewport);
    }

    fn current_fallback_policy(&self) -> FallbackPolicy {
        self.config
            .active_script()
            .loaded_config()
            .modes
            .get(self.input_state.current_mode())
            .map(|mode| mode.fallback_policy)
            .unwrap_or(FallbackPolicy::Ignore)
    }

    fn focused_leaf<'a>(&self, presentation: &'a PresentationModel) -> Result<&'a LeafFrame> {
        presentation
            .focused_leaf()
            .ok_or_else(|| MuxError::invalid_input("no focused leaf"))
    }

    fn mouse_target_leaf<'a>(
        &self,
        presentation: &'a PresentationModel,
        point: Point,
    ) -> Option<&'a LeafFrame> {
        if let Some(floating) = presentation.floating_at(point) {
            return presentation.leaves.iter().rev().find(|leaf| {
                leaf.floating_id == Some(floating.floating_id) && leaf.rect.contains(point)
            });
        }
        presentation.leaf_at(point)
    }

    fn view_is_alternate_screen(&self, node_id: NodeId) -> bool {
        self.client
            .state()
            .view_state(node_id)
            .is_some_and(|state| state.alternate_screen)
    }

    fn should_passthrough_binding_in_alternate_screen(
        &self,
        presentation: &PresentationModel,
        actions: &[Action],
    ) -> bool {
        // When the focused pane is acting like a live terminal surface, prefer
        // forwarding local search/select/navigation bindings to the program
        // instead of stealing keys that fullscreen apps expect to receive.
        let Some(leaf) = presentation.focused_leaf() else {
            return false;
        };
        let Some(view_state) = self.client.state().view_state(leaf.node_id) else {
            return false;
        };
        (view_state.alternate_screen && actions.iter().all(action_is_local_terminal_action))
            || (self.input_state.current_mode() == NORMAL_MODE
                && view_state.follow_output
                && view_state.search_state.is_none()
                && view_state.selection_state.is_none()
                && actions.iter().all(action_requires_local_context))
    }

    async fn refresh_local_viewports(&mut self, presentation: &PresentationModel) -> Result<()> {
        let refreshes = presentation
            .leaves
            .iter()
            .filter_map(|leaf| {
                let state = self.client.state().view_state(leaf.node_id)?;
                if state.alternate_screen
                    || state.follow_output
                    || state.visible_line_count == 0
                    || !state.visible_lines.is_empty()
                {
                    return None;
                }
                Some((leaf.node_id, state.scroll_top_line))
            })
            .collect::<Vec<_>>();

        for (node_id, scroll_top_line) in refreshes {
            self.fetch_view_slice(node_id, scroll_top_line).await?;
        }
        Ok(())
    }

    async fn scroll_view_by(&mut self, node_id: NodeId, delta: i64) -> Result<()> {
        let Some(state) = self.client.state().view_state(node_id) else {
            return Ok(());
        };
        if state.alternate_screen {
            return Ok(());
        }
        let current = i128::from(state.scroll_top_line);
        let delta = i128::from(delta);
        // Negative deltas scroll up by subtracting the absolute value; positive deltas
        // scroll down by adding. We do the math in i128 with saturating ops so
        // unsigned_abs/subtraction cannot underflow or overflow before clamping at 0.
        let next = if delta.is_negative() {
            current.saturating_sub(delta.unsigned_abs() as i128)
        } else {
            current.saturating_add(delta)
        };
        let next = next.max(0) as u64;
        self.set_view_scroll_top(node_id, next).await
    }

    async fn set_view_scroll_top(&mut self, node_id: NodeId, scroll_top_line: u64) -> Result<()> {
        let Some(state) = self.client.state().view_state(node_id) else {
            return Ok(());
        };
        if state.alternate_screen {
            return Ok(());
        }
        let bottom = state
            .total_line_count
            .saturating_sub(u64::from(state.visible_line_count));
        let scroll_top_line = scroll_top_line.min(bottom);
        if scroll_top_line == bottom {
            return self.follow_output_for_view(node_id).await;
        }

        self.fetch_view_slice(node_id, scroll_top_line).await?;
        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            state.follow_output = false;
        }
        Ok(())
    }

    async fn follow_output_for_view(&mut self, node_id: NodeId) -> Result<()> {
        let Some(state) = self.client.state().view_state(node_id) else {
            return Ok(());
        };
        if state.alternate_screen {
            return Ok(());
        }
        self.client
            .state_mut()
            .set_view_follow_output(node_id, true);
        Ok(())
    }

    async fn fetch_view_slice(&mut self, node_id: NodeId, scroll_top_line: u64) -> Result<()> {
        let Some(state) = self.client.state().view_state(node_id) else {
            return Ok(());
        };
        if state.visible_line_count == 0 {
            return Ok(());
        }
        let response = self
            .client
            .capture_scrollback_slice(
                state.buffer_id,
                scroll_top_line,
                u32::from(state.visible_line_count),
            )
            .await?;
        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            state.total_line_count = response
                .total_lines
                .max(u64::from(state.visible_line_count));
        }
        self.client
            .state_mut()
            .set_view_visible_lines(node_id, scroll_top_line, response.lines);
        Ok(())
    }

    async fn enter_search_mode(
        &mut self,
        _session_id: SessionId,
        _viewport: Size,
        presentation: &PresentationModel,
    ) -> Result<()> {
        let leaf = self.focused_leaf(presentation)?;
        if self.view_is_alternate_screen(leaf.node_id) {
            return Ok(());
        }

        let query = self
            .client
            .state()
            .view_state(leaf.node_id)
            .and_then(|state| state.search_state.as_ref())
            .map(|state| state.query.clone())
            .unwrap_or_default();
        self.search_prompt = Some(SearchPrompt {
            node_id: leaf.node_id,
            query,
        });
        self.input_state.set_mode(SEARCH_MODE);
        Ok(())
    }

    async fn handle_search_key(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        presentation: &PresentationModel,
        key: KeyEvent,
    ) -> Result<()> {
        if self.search_prompt.is_none() {
            return self
                .enter_search_mode(session_id, viewport, presentation)
                .await;
        }

        match key {
            KeyEvent::Char(ch) => {
                if let Some(prompt) = &mut self.search_prompt {
                    prompt.query.push(ch);
                }
                Ok(())
            }
            KeyEvent::Tab => {
                if let Some(prompt) = &mut self.search_prompt {
                    prompt.query.push('\t');
                }
                Ok(())
            }
            KeyEvent::Backspace => {
                if let Some(prompt) = &mut self.search_prompt {
                    prompt.query.pop();
                }
                Ok(())
            }
            KeyEvent::Enter => {
                self.execute_actions(Some(session_id), Some(viewport), vec![Action::CommitSearch])
                    .await
            }
            KeyEvent::Escape => {
                self.execute_actions(Some(session_id), Some(viewport), vec![Action::CancelSearch])
                    .await
            }
            KeyEvent::Bytes(bytes) => {
                if let Some(prompt) = &mut self.search_prompt {
                    prompt.query.push_str(&String::from_utf8_lossy(&bytes));
                }
                Ok(())
            }
            KeyEvent::Ctrl(_)
            | KeyEvent::Alt(_)
            | KeyEvent::Up
            | KeyEvent::Down
            | KeyEvent::Left
            | KeyEvent::Right
            | KeyEvent::Home
            | KeyEvent::End
            | KeyEvent::Insert
            | KeyEvent::Delete
            | KeyEvent::PageUp
            | KeyEvent::PageDown => Ok(()),
        }
    }

    async fn handle_search_paste(
        &mut self,
        session_id: SessionId,
        viewport: Size,
        bytes: Vec<u8>,
    ) -> Result<()> {
        if self.search_prompt.is_none() {
            return self.cancel_search_prompt(session_id, viewport).await;
        }
        if let Some(prompt) = &mut self.search_prompt {
            prompt.query.push_str(&String::from_utf8_lossy(&bytes));
        }
        Ok(())
    }

    async fn commit_search_prompt(&mut self, session_id: SessionId, viewport: Size) -> Result<()> {
        let Some(prompt) = self.search_prompt.take() else {
            return self.cancel_search_prompt(session_id, viewport).await;
        };
        let Some(buffer_id) = self
            .client
            .state()
            .view_state(prompt.node_id)
            .map(|state| state.buffer_id)
        else {
            return self.cancel_search_prompt(session_id, viewport).await;
        };

        let matches = if prompt.query.is_empty() {
            Vec::new()
        } else {
            let snapshot = self.client.capture_buffer(buffer_id).await?;
            compute_search_matches(&snapshot.lines, &prompt.query)
        };

        if let Some(state) = self.client.state_mut().view_state_mut(prompt.node_id) {
            if prompt.query.is_empty() {
                state.search_state = None;
            } else {
                state.search_state = Some(SearchState {
                    query: prompt.query.clone(),
                    matches,
                    active_match_index: None,
                });
            }
        }

        if self
            .client
            .state()
            .view_state(prompt.node_id)
            .and_then(|state| state.search_state.as_ref())
            .is_some_and(|state| !state.matches.is_empty())
        {
            self.jump_to_search_index(prompt.node_id, 0).await?;
        }
        self.input_state.set_mode(NORMAL_MODE);
        Ok(())
    }

    async fn cancel_search_prompt(
        &mut self,
        _session_id: SessionId,
        _viewport: Size,
    ) -> Result<()> {
        self.search_prompt = None;
        self.input_state.set_mode(NORMAL_MODE);
        Ok(())
    }

    async fn navigate_search(
        &mut self,
        presentation: &PresentationModel,
        forward: bool,
    ) -> Result<()> {
        let leaf = self.focused_leaf(presentation)?;
        if self.view_is_alternate_screen(leaf.node_id) {
            return Ok(());
        }
        let Some(search_state) = self
            .client
            .state()
            .view_state(leaf.node_id)
            .and_then(|state| state.search_state.as_ref())
        else {
            return Ok(());
        };
        if search_state.matches.is_empty() {
            return Ok(());
        }
        let current = search_state.active_match_index.unwrap_or(0);
        let next = if forward {
            (current + 1) % search_state.matches.len()
        } else if current == 0 {
            search_state.matches.len() - 1
        } else {
            current - 1
        };
        self.jump_to_search_index(leaf.node_id, next).await
    }

    async fn jump_to_search_index(&mut self, node_id: NodeId, index: usize) -> Result<()> {
        let Some((selected, current_top, visible_line_count)) =
            self.client.state().view_state(node_id).and_then(|state| {
                state.search_state.as_ref().and_then(|search| {
                    search
                        .matches
                        .get(index)
                        .copied()
                        .map(|selected| (selected, state.scroll_top_line, state.visible_line_count))
                })
            })
        else {
            return Ok(());
        };

        let visible_line_count = u64::from(visible_line_count.max(1));
        let new_top = if selected.line < current_top {
            selected.line
        } else if selected.line >= current_top.saturating_add(visible_line_count) {
            selected
                .line
                .saturating_sub(visible_line_count.saturating_sub(1))
        } else {
            current_top
        };
        if new_top != current_top
            || self
                .client
                .state()
                .view_state(node_id)
                .is_some_and(|state| state.follow_output)
        {
            self.fetch_view_slice(node_id, new_top).await?;
        }

        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            if let Some(search) = &mut state.search_state {
                search.active_match_index = Some(index);
            }
            state.follow_output = false;
        }
        Ok(())
    }

    async fn enter_select_mode(
        &mut self,
        _session_id: SessionId,
        _viewport: Size,
        presentation: &PresentationModel,
        kind: SelectionKind,
    ) -> Result<()> {
        let leaf = self.focused_leaf(presentation)?;
        if self.view_is_alternate_screen(leaf.node_id) {
            return Ok(());
        }

        let start = self.selection_origin(leaf.node_id);
        if let Some(state) = self.client.state_mut().view_state_mut(leaf.node_id) {
            state.selection_state = Some(SelectionState {
                kind,
                anchor: start,
                cursor: start,
            });
        }
        self.input_state.set_mode(SELECT_MODE);
        Ok(())
    }

    fn selection_origin(&self, node_id: NodeId) -> SelectionPoint {
        let Some(state) = self.client.state().view_state(node_id) else {
            return SelectionPoint::default();
        };
        let fallback = SelectionPoint {
            line: state.scroll_top_line,
            column: 0,
        };
        if !state.follow_output {
            return fallback;
        }
        self.client
            .state()
            .snapshots
            .get(&state.buffer_id)
            .and_then(|snapshot| {
                snapshot.cursor.map(|cursor| SelectionPoint {
                    line: state.scroll_top_line + u64::from(cursor.position.row),
                    column: cursor.position.col,
                })
            })
            .unwrap_or(fallback)
    }

    async fn move_selection(
        &mut self,
        node_id: NodeId,
        direction: NavigationDirection,
    ) -> Result<()> {
        let Some((mut selection, total_line_count, scroll_top_line, visible_line_count)) =
            self.client.state().view_state(node_id).and_then(|state| {
                state.selection_state.clone().map(|selection| {
                    (
                        selection,
                        state.total_line_count,
                        state.scroll_top_line,
                        state.visible_line_count,
                    )
                })
            })
        else {
            return Ok(());
        };

        match direction {
            NavigationDirection::Left => {
                selection.cursor.column = selection.cursor.column.saturating_sub(1);
            }
            NavigationDirection::Right => {
                selection.cursor.column = selection.cursor.column.saturating_add(1);
            }
            NavigationDirection::Up => {
                selection.cursor.line = selection.cursor.line.saturating_sub(1);
            }
            NavigationDirection::Down => {
                selection.cursor.line = selection
                    .cursor
                    .line
                    .saturating_add(1)
                    .min(total_line_count.saturating_sub(1));
            }
        }

        let visible_line_count = u64::from(visible_line_count.max(1));
        let mut new_top = scroll_top_line;
        if selection.cursor.line < new_top {
            new_top = selection.cursor.line;
        } else if selection.cursor.line >= new_top.saturating_add(visible_line_count) {
            new_top = selection
                .cursor
                .line
                .saturating_sub(visible_line_count.saturating_sub(1));
        }
        if new_top != scroll_top_line {
            self.fetch_view_slice(node_id, new_top).await?;
        }

        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            state.follow_output = false;
            state.selection_state = Some(selection);
        }
        Ok(())
    }

    async fn copy_selection(
        &mut self,
        _session_id: SessionId,
        _viewport: Size,
        node_id: NodeId,
    ) -> Result<()> {
        let Some((buffer_id, selection_state)) =
            self.client.state().view_state(node_id).and_then(|state| {
                state
                    .selection_state
                    .clone()
                    .map(|selection| (state.buffer_id, selection))
            })
        else {
            return Ok(());
        };

        let snapshot = self.client.capture_buffer(buffer_id).await?;
        let copied = serialize_selection(&snapshot.lines, &selection_state);
        self.enqueue_clipboard(copied);

        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            state.selection_state = None;
        }
        self.input_state.set_mode(NORMAL_MODE);
        Ok(())
    }

    async fn cancel_selection(
        &mut self,
        _session_id: SessionId,
        _viewport: Size,
        node_id: NodeId,
    ) -> Result<()> {
        if let Some(state) = self.client.state_mut().view_state_mut(node_id) {
            state.selection_state = None;
        }
        self.input_state.set_mode(NORMAL_MODE);
        Ok(())
    }

    fn enqueue_clipboard(&mut self, text: String) {
        use base64::Engine as _;

        let encoded = base64::engine::general_purpose::STANDARD.encode(text);
        self.terminal_output
            .push_back(format!("\x1b]52;c;{encoded}\x07").into_bytes());
    }

    async fn focus_node(&mut self, session_id: SessionId, node_id: NodeId) -> Result<()> {
        self.focus_node_with_shortcut(session_id, node_id, true)
            .await
    }

    async fn focus_node_with_shortcut(
        &mut self,
        session_id: SessionId,
        node_id: NodeId,
        allow_same_session_shortcut: bool,
    ) -> Result<()> {
        if allow_same_session_shortcut
            && self
                .client
                .state()
                .sessions
                .get(&session_id)
                .and_then(|session| session.focused_leaf_id)
                == Some(node_id)
        {
            return Ok(());
        }

        let previous_buffer = self
            .active_session_id
            .and_then(|active_session_id| self.focused_buffer_for_session(active_session_id));
        let sent_focus_out = if let Some(buffer_id) = previous_buffer {
            self.maybe_send_focus_sequence(buffer_id, false).await?
        } else {
            false
        };

        self.client
            .request_message(ClientMessage::Node(NodeRequest::Focus {
                request_id: self.client.next_request_id(),
                session_id,
                node_id,
            }))
            .await?;
        self.client.resync_session(session_id).await?;

        let new_buffer = self.focused_buffer_for_session(session_id);
        if new_buffer != previous_buffer {
            let sent_focus_in = if let Some(buffer_id) = new_buffer {
                self.maybe_send_focus_sequence(buffer_id, true).await?
            } else {
                false
            };
            if sent_focus_in && let Some(buffer_id) = new_buffer {
                self.client.refresh_buffer_snapshot(buffer_id).await?;
            }
            if sent_focus_out && let Some(buffer_id) = previous_buffer {
                self.client.refresh_buffer_snapshot(buffer_id).await?;
            }
        }
        Ok(())
    }

    fn focused_buffer_for_session(&self, session_id: SessionId) -> Option<BufferId> {
        self.client
            .state()
            .sessions
            .get(&session_id)
            .and_then(|session| session.focused_leaf_id)
            .and_then(|node_id| self.node_buffer_id(node_id))
    }

    fn node_buffer_id(&self, node_id: NodeId) -> Option<BufferId> {
        self.client
            .state()
            .nodes
            .get(&node_id)
            .and_then(|node| node.buffer_view.as_ref())
            .map(|buffer_view| buffer_view.buffer_id)
    }

    async fn maybe_send_focus_sequence(
        &mut self,
        buffer_id: BufferId,
        focused: bool,
    ) -> Result<bool> {
        if !self
            .client
            .state()
            .snapshots
            .get(&buffer_id)
            .is_some_and(|snapshot| snapshot.focus_reporting)
        {
            return Ok(false);
        }
        let bytes = if focused {
            b"\x1b[I".to_vec()
        } else {
            b"\x1b[O".to_vec()
        };
        self.send_input_only(buffer_id, bytes).await?;
        Ok(true)
    }

    async fn send_input_only(&self, buffer_id: BufferId, bytes: Vec<u8>) -> Result<()> {
        self.client
            .request_message(ClientMessage::Input(InputRequest::Send {
                request_id: self.client.next_request_id(),
                buffer_id,
                bytes,
            }))
            .await?;
        Ok(())
    }

    fn record_notification(&mut self, message: impl Into<String>) {
        let message = message.into();
        warn!("{message}");
        self.notifications.push(message);
        if self.notifications.len() > 64 {
            let overflow = self.notifications.len() - 64;
            self.notifications.drain(0..overflow);
        }
    }
}

fn action_is_local_terminal_action(action: &Action) -> bool {
    match action {
        Action::Chain(actions) => actions.iter().all(action_is_local_terminal_action),
        Action::EnterMode { mode } | Action::ToggleMode { mode } => {
            mode == SEARCH_MODE || mode == SELECT_MODE
        }
        Action::ScrollLineUp
        | Action::ScrollLineDown
        | Action::ScrollPageUp
        | Action::ScrollPageDown
        | Action::ScrollToTop
        | Action::ScrollToBottom
        | Action::FollowOutput
        | Action::EnterSearchMode
        | Action::SearchNext
        | Action::SearchPrev
        | Action::CommitSearch
        | Action::CancelSearch
        | Action::EnterSelect { .. }
        | Action::SelectMove { .. }
        | Action::CopySelection
        | Action::CancelSelection => true,
        _ => false,
    }
}

fn action_requires_local_context(action: &Action) -> bool {
    match action {
        Action::Chain(actions) => actions.iter().all(action_requires_local_context),
        Action::EnterSearchMode
        | Action::SearchNext
        | Action::SearchPrev
        | Action::EnterSelect { .. } => true,
        Action::EnterMode { mode } | Action::ToggleMode { mode } => {
            mode == SEARCH_MODE || mode == SELECT_MODE
        }
        _ => false,
    }
}

fn compute_search_matches(lines: &[String], query: &str) -> Vec<SearchMatch> {
    if query.is_empty() {
        return Vec::new();
    }

    let mut matches = Vec::new();
    for (line_index, line) in lines.iter().enumerate() {
        let mut search_from = 0;
        while let Some(found) = line[search_from..].find(query) {
            let byte_start = search_from + found;
            let byte_end = byte_start + query.len();
            let start_column = display_width(&line[..byte_start]);
            let end_column =
                start_column.saturating_add(display_width(&line[byte_start..byte_end]));
            matches.push(SearchMatch {
                line: u64::try_from(line_index).unwrap_or(u64::MAX),
                start_column,
                end_column,
            });
            search_from = byte_end.max(search_from + 1);
        }
    }
    matches
}

#[doc(hidden)]
pub fn benchmark_search_matches(lines: &[String], query: &str) -> Vec<SearchMatch> {
    compute_search_matches(lines, query)
}

#[doc(hidden)]
pub fn benchmark_serialize_selection(lines: &[String], selection: &SelectionState) -> String {
    serialize_selection(lines, selection)
}

fn serialize_selection(lines: &[String], selection: &SelectionState) -> String {
    match selection.kind {
        SelectionKind::Line => serialize_line_selection(lines, selection),
        SelectionKind::Block => serialize_block_selection(lines, selection),
        SelectionKind::Character => serialize_character_selection(lines, selection),
    }
}

fn serialize_character_selection(lines: &[String], selection: &SelectionState) -> String {
    let (start, end) = ordered_points(selection.anchor, selection.cursor);
    let mut parts = Vec::new();
    for line_index in start.line..=end.line {
        let line = lines
            .get(usize::try_from(line_index).unwrap_or(usize::MAX))
            .map(String::as_str)
            .unwrap_or("");
        let fragment = if start.line == end.line {
            slice_display_range(line, start.column, end.column.saturating_add(1))
        } else if line_index == start.line {
            slice_display_range(line, start.column, u16::MAX)
        } else if line_index == end.line {
            slice_display_range(line, 0, end.column.saturating_add(1))
        } else {
            line.to_owned()
        };
        parts.push(fragment);
    }
    parts.join("\n")
}

fn serialize_line_selection(lines: &[String], selection: &SelectionState) -> String {
    let start_line = selection.anchor.line.min(selection.cursor.line);
    let end_line = selection.anchor.line.max(selection.cursor.line);
    (start_line..=end_line)
        .map(|line_index| {
            lines
                .get(usize::try_from(line_index).unwrap_or(usize::MAX))
                .cloned()
                .unwrap_or_default()
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn serialize_block_selection(lines: &[String], selection: &SelectionState) -> String {
    let start_line = selection.anchor.line.min(selection.cursor.line);
    let end_line = selection.anchor.line.max(selection.cursor.line);
    let start_column = selection.anchor.column.min(selection.cursor.column);
    let end_column = selection
        .anchor
        .column
        .max(selection.cursor.column)
        .saturating_add(1);
    (start_line..=end_line)
        .map(|line_index| {
            let line = lines
                .get(usize::try_from(line_index).unwrap_or(usize::MAX))
                .map(String::as_str)
                .unwrap_or("");
            slice_display_range(line, start_column, end_column)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn ordered_points(left: SelectionPoint, right: SelectionPoint) -> (SelectionPoint, SelectionPoint) {
    if (left.line, left.column) <= (right.line, right.column) {
        (left, right)
    } else {
        (right, left)
    }
}

fn slice_display_range(line: &str, start_column: u16, end_column: u16) -> String {
    if start_column >= end_column {
        return String::new();
    }

    let mut column = 0_u16;
    let mut output = String::new();
    for grapheme in UnicodeSegmentation::graphemes(line, true) {
        let width = display_width(grapheme).max(1);
        let next_column = column.saturating_add(width);
        if next_column > start_column && column < end_column {
            output.push_str(grapheme);
        }
        if column >= end_column {
            break;
        }
        column = next_column;
    }
    output
}

fn encode_mouse_event(leaf: &LeafFrame, event: MouseEvent) -> Result<Vec<u8>> {
    let origin_x = clamp_origin(leaf.rect.origin.x);
    let origin_y = clamp_origin(leaf.rect.origin.y).saturating_add(1);
    let local_column = event
        .column
        .checked_sub(origin_x)
        .ok_or_else(|| MuxError::invalid_input("mouse event fell outside pane bounds"))?;
    let local_row = event
        .row
        .checked_sub(origin_y)
        .ok_or_else(|| MuxError::invalid_input("mouse event fell outside pane content"))?;

    let mut code = 0_u16;
    if event.modifiers.shift {
        code |= 0b00100;
    }
    if event.modifiers.alt {
        code |= 0b01000;
    }
    if event.modifiers.ctrl {
        code |= 0b10000;
    }

    let suffix = match event.kind {
        MouseEventKind::Press(button) => {
            code |= mouse_button_code(button);
            'M'
        }
        MouseEventKind::Release(button) => {
            code |= button.map(mouse_button_code).unwrap_or(0b11);
            'm'
        }
        MouseEventKind::Drag(button) => {
            code |= 0b100000 | mouse_button_code(button);
            'M'
        }
        MouseEventKind::WheelUp => {
            code |= 0b1_000000;
            'M'
        }
        MouseEventKind::WheelDown => {
            code |= 0b1_000001;
            'M'
        }
    };

    Ok(format!(
        "\x1b[<{code};{};{}{suffix}",
        local_column.saturating_add(1),
        local_row.saturating_add(1)
    )
    .into_bytes())
}

fn mouse_button_code(button: MouseButton) -> u16 {
    match button {
        MouseButton::Left => 0,
        MouseButton::Middle => 1,
        MouseButton::Right => 2,
    }
}

fn clamp_origin(value: i32) -> u16 {
    u16::try_from(value.max(0)).unwrap_or(u16::MAX)
}

fn display_width(text: &str) -> u16 {
    u16::try_from(UnicodeWidthStr::width(text)).unwrap_or(u16::MAX)
}

fn prepend_actions(pending: &mut VecDeque<Action>, actions: Vec<Action>) {
    for action in actions.into_iter().rev() {
        pending.push_front(action);
    }
}

fn prepend_actions_with_limit(
    pending: &mut VecDeque<Action>,
    actions: Vec<Action>,
    expansions: &mut usize,
) -> Result<()> {
    *expansions = expansions.saturating_add(actions.len());
    if *expansions > MAX_EXPANDED_ACTIONS {
        return Err(MuxError::invalid_input("action expansion limit reached"));
    }
    prepend_actions(pending, actions);
    Ok(())
}

fn format_notification(level: NotifyLevel, message: &str) -> String {
    match level {
        NotifyLevel::Info => message.to_owned(),
        NotifyLevel::Warn => format!("warn: {message}"),
        NotifyLevel::Error => format!("error: {message}"),
    }
}

fn resolve_floating_geometry(spec: FloatingGeometrySpec, viewport: Size) -> FloatGeometry {
    let width = resolve_floating_size(spec.width, viewport.width);
    let height = resolve_floating_size(spec.height, viewport.height);
    let max_x = viewport.width.saturating_sub(width);
    let max_y = viewport.height.saturating_sub(height);

    let (base_x, base_y) = match spec.anchor {
        FloatingAnchor::Center => (max_x / 2, max_y / 2),
        FloatingAnchor::TopLeft => (0, 0),
        FloatingAnchor::TopRight => (max_x, 0),
        FloatingAnchor::BottomLeft => (0, max_y),
        FloatingAnchor::BottomRight => (max_x, max_y),
    };

    let x = (i32::from(base_x) + i32::from(spec.offset_x)).clamp(0, i32::from(max_x));
    let y = (i32::from(base_y) + i32::from(spec.offset_y)).clamp(0, i32::from(max_y));

    FloatGeometry::new(x as u16, y as u16, width, height)
}

fn resolve_floating_size(size: FloatingSize, max: u16) -> u16 {
    match size {
        FloatingSize::Cells(cells) => cells.min(max.max(1)),
        FloatingSize::Percent(percent) => {
            let max = u32::from(max.max(1));
            let percent = u32::from(percent.clamp(1, 100));
            ((max * percent) / 100).max(1) as u16
        }
    }
}

fn default_shell_command() -> Vec<String> {
    vec![std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned())]
}

fn key_event_to_token(key: KeyEvent) -> Result<KeyToken> {
    match key {
        KeyEvent::Char(ch) => Ok(KeyToken::Char(ch)),
        KeyEvent::Enter => Ok(KeyToken::Enter),
        KeyEvent::Tab => Ok(KeyToken::Tab),
        KeyEvent::Backspace => Ok(KeyToken::Backspace),
        KeyEvent::Escape => Ok(KeyToken::Escape),
        KeyEvent::Ctrl(ch) => Ok(KeyToken::Ctrl(ch.to_ascii_lowercase())),
        KeyEvent::Alt(ch) => Ok(KeyToken::Alt(ch.to_ascii_lowercase())),
        KeyEvent::Up => Ok(KeyToken::Up),
        KeyEvent::Down => Ok(KeyToken::Down),
        KeyEvent::Left => Ok(KeyToken::Left),
        KeyEvent::Right => Ok(KeyToken::Right),
        KeyEvent::Home => Ok(KeyToken::Home),
        KeyEvent::End => Ok(KeyToken::End),
        KeyEvent::Insert => Ok(KeyToken::Insert),
        KeyEvent::Delete => Ok(KeyToken::Delete),
        KeyEvent::PageUp => Ok(KeyToken::PageUp),
        KeyEvent::PageDown => Ok(KeyToken::PageDown),
        KeyEvent::Bytes(_) => Err(MuxError::invalid_input("raw bytes are handled separately")),
    }
}

fn sequence_to_bytes(sequence: &[KeyToken]) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    for token in sequence {
        match token {
            KeyToken::Char(ch) => {
                let mut encoded = [0; 4];
                bytes.extend_from_slice(ch.encode_utf8(&mut encoded).as_bytes());
            }
            KeyToken::Space => bytes.push(b' '),
            KeyToken::Tab => bytes.push(b'\t'),
            KeyToken::Enter => bytes.push(b'\r'),
            KeyToken::Backspace => bytes.push(0x7f),
            KeyToken::Escape => bytes.push(0x1b),
            KeyToken::Ctrl(ch) => bytes.push(ctrl_byte(*ch)?),
            KeyToken::Alt(ch) => {
                bytes.push(0x1b);
                bytes.extend(sequence_to_bytes(&[KeyToken::Char(*ch)])?);
            }
            KeyToken::Up => bytes.extend_from_slice(b"\x1b[A"),
            KeyToken::Down => bytes.extend_from_slice(b"\x1b[B"),
            KeyToken::Left => bytes.extend_from_slice(b"\x1b[D"),
            KeyToken::Right => bytes.extend_from_slice(b"\x1b[C"),
            KeyToken::Home => bytes.extend_from_slice(b"\x1b[H"),
            KeyToken::End => bytes.extend_from_slice(b"\x1b[F"),
            KeyToken::Insert => bytes.extend_from_slice(b"\x1b[2~"),
            KeyToken::Delete => bytes.extend_from_slice(b"\x1b[3~"),
            KeyToken::PageUp => bytes.extend_from_slice(b"\x1b[5~"),
            KeyToken::PageDown => bytes.extend_from_slice(b"\x1b[6~"),
            KeyToken::Leader => {
                return Err(MuxError::invalid_input(
                    "leader placeholders cannot be sent directly",
                ));
            }
        }
    }
    Ok(bytes)
}

fn ctrl_byte(ch: char) -> Result<u8> {
    if !ch.is_ascii() {
        return Err(MuxError::invalid_input("control keys must be ASCII"));
    }
    Ok((ch.to_ascii_lowercase() as u8) & 0x1f)
}

async fn rollback_created_buffer_on_error<T, U>(
    configured: &mut ConfiguredClient<U>,
    buffer_id: Option<BufferId>,
    operation: &str,
    result: Result<T>,
) -> Result<T>
where
    U: Transport,
{
    match result {
        Ok(value) => Ok(value),
        Err(error) => {
            if let Some(buffer_id) = buffer_id {
                configured
                    .rollback_created_buffer(buffer_id, operation)
                    .await;
            }
            Err(error)
        }
    }
}

fn event_name(event: &ServerEvent) -> &'static str {
    match event {
        ServerEvent::SessionCreated(_) => "session_created",
        ServerEvent::SessionClosed(_) => "session_closed",
        ServerEvent::SessionRenamed(_) => "session_renamed",
        ServerEvent::BufferCreated(_) => "buffer_created",
        ServerEvent::BufferPipeChanged(_) => "buffer_pipe_changed",
        ServerEvent::BufferDetached(_) => "buffer_detached",
        ServerEvent::NodeChanged(_) => "node_changed",
        ServerEvent::FloatingChanged(_) => "floating_changed",
        ServerEvent::FocusChanged(_) => "focus_changed",
        ServerEvent::RenderInvalidated(_) => "render_invalidated",
        ServerEvent::ClientChanged(_) => "client_changed",
    }
}

fn event_info(
    name: &str,
    event: &ServerEvent,
    fallback_session_id: Option<SessionId>,
) -> EventInfo {
    let mut info = base_event_info(name);
    match event {
        ServerEvent::SessionCreated(event) => info.session_id = Some(event.session.id),
        ServerEvent::SessionClosed(event) => info.session_id = Some(event.session_id),
        ServerEvent::SessionRenamed(event) => info.session_id = Some(event.session_id),
        ServerEvent::BufferCreated(event) => {
            info.buffer_id = Some(event.buffer.id);
            info.node_id = event.buffer.attachment_node_id;
        }
        ServerEvent::BufferPipeChanged(event) => {
            info.session_id = event.session_id;
            info.buffer_id = Some(event.buffer.id);
            info.node_id = event.buffer.attachment_node_id;
        }
        ServerEvent::BufferDetached(event) => info.buffer_id = Some(event.buffer_id),
        ServerEvent::NodeChanged(event) => info.session_id = Some(event.session_id),
        ServerEvent::FloatingChanged(event) => {
            info.session_id = Some(event.session_id);
            info.floating_id = event.floating_id;
        }
        ServerEvent::FocusChanged(event) => {
            info.session_id = Some(event.session_id);
            info.node_id = event.focused_leaf_id;
            info.floating_id = event.focused_floating_id;
        }
        ServerEvent::RenderInvalidated(event) => info.buffer_id = Some(event.buffer_id),
        ServerEvent::ClientChanged(event) => {
            info.session_id = event.client.current_session_id;
            info.previous_session_id = event.previous_session_id;
            info.client_id = Some(event.client.id);
        }
    }
    if info.session_id.is_none() {
        info.session_id = fallback_session_id;
    }
    info
}

fn base_event_info(name: &str) -> EventInfo {
    EventInfo {
        name: name.to_owned(),
        session_id: None,
        previous_session_id: None,
        client_id: None,
        buffer_id: None,
        node_id: None,
        floating_id: None,
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use embers_core::{ActivityState, BufferId, FloatingId, NodeId, PtySize, RequestId, SessionId};
    use embers_protocol::{
        BufferLocation, BufferLocationAttachment, BufferRecord, BufferRecordKind,
        BufferRecordState, BufferWithLocationResponse, FloatingChangedEvent, ServerEvent,
        ServerResponse,
    };
    use tempfile::tempdir;

    use super::{ConfiguredClient, SearchPrompt, event_info};
    use crate::client::MuxClient;
    use crate::config::{ConfigDiscoveryOptions, ConfigManager};
    use crate::input::NORMAL_MODE;
    use crate::testing::FakeTransport;

    #[test]
    fn attached_buffer_location_accepts_session_and_floating_locations() {
        assert_eq!(
            ConfiguredClient::<crate::testing::FakeTransport>::attached_buffer_location(
                Some(BufferId(7)),
                BufferLocation::session(BufferId(7), SessionId(2), NodeId(5)),
                "buffer focus",
            )
            .expect("session attachment should validate"),
            (SessionId(2), NodeId(5))
        );
        assert_eq!(
            ConfiguredClient::<crate::testing::FakeTransport>::attached_buffer_location(
                Some(BufferId(7)),
                BufferLocation::floating(BufferId(7), SessionId(2), NodeId(5), FloatingId(9)),
                "buffer reveal",
            )
            .expect("floating attachment should validate"),
            (SessionId(2), NodeId(5))
        );
    }

    #[test]
    fn attached_buffer_location_accepts_history_helper_locations_without_source_id_match() {
        assert_eq!(
            ConfiguredClient::<crate::testing::FakeTransport>::attached_buffer_location(
                None,
                BufferLocation::session(BufferId(8), SessionId(2), NodeId(5)),
                "buffer history",
            )
            .expect("history helper attachment should validate"),
            (SessionId(2), NodeId(5))
        );
    }

    #[test]
    fn attached_buffer_location_rejects_mismatched_or_detached_locations() {
        let mismatch = ConfiguredClient::<crate::testing::FakeTransport>::attached_buffer_location(
            Some(BufferId(7)),
            BufferLocation::session(BufferId(8), SessionId(2), NodeId(5)),
            "buffer focus",
        )
        .expect_err("mismatched buffer ids should fail");
        let detached = ConfiguredClient::<crate::testing::FakeTransport>::attached_buffer_location(
            Some(BufferId(7)),
            BufferLocation {
                buffer_id: BufferId(7),
                attachment: BufferLocationAttachment::Detached,
            },
            "buffer reveal",
        )
        .expect_err("detached locations should fail");

        assert!(
            mismatch
                .to_string()
                .contains("returned location for buffer 8")
        );
        assert!(detached.to_string().contains("detached location"));
    }

    #[test]
    fn buffer_location_from_response_accepts_buffer_with_location() {
        let location = BufferLocation::session(BufferId(8), SessionId(2), NodeId(5));
        let response = ServerResponse::BufferWithLocation(
            BufferWithLocationResponse::new(
                RequestId(1),
                BufferRecord {
                    id: BufferId(8),
                    title: "helper".to_owned(),
                    command: Vec::new(),
                    cwd: None,
                    kind: BufferRecordKind::Helper,
                    state: BufferRecordState::Created,
                    pid: None,
                    attachment_node_id: Some(NodeId(5)),
                    read_only: true,
                    helper_source_buffer_id: Some(BufferId(7)),
                    helper_scope: Some(embers_protocol::BufferHistoryScope::Visible),
                    pty_size: PtySize::new(80, 24),
                    activity: ActivityState::Idle,
                    last_snapshot_seq: 0,
                    exit_code: None,
                    pipe: None,
                    env: Default::default(),
                },
                location,
                false,
            )
            .expect("buffer and location ids should match"),
        );

        assert_eq!(
            ConfiguredClient::<crate::testing::FakeTransport>::buffer_location_from_response(
                response,
                "buffer history",
            )
            .expect("buffer-with-location response should validate"),
            location
        );
    }

    #[test]
    fn floating_changed_event_info_includes_floating_id() {
        let info = event_info(
            "floating_changed",
            &ServerEvent::FloatingChanged(FloatingChangedEvent {
                session_id: SessionId(2),
                floating_id: Some(FloatingId(9)),
            }),
            None,
        );

        assert_eq!(info.session_id, Some(SessionId(2)));
        assert_eq!(info.floating_id, Some(FloatingId(9)));
    }

    #[test]
    fn finish_config_reload_clears_search_prompt_when_mode_falls_back_to_normal() {
        let tempdir = tempdir().expect("create tempdir");
        let config_path = tempdir.path().join("config.rhai");
        fs::write(&config_path, "define_mode(\"custom\");").expect("write custom config");
        let config = ConfigManager::load(
            ConfigDiscoveryOptions::default().with_project_config_dir(tempdir.path()),
        )
        .expect("load config");
        let client = MuxClient::new(FakeTransport::default());
        let mut configured = ConfiguredClient::new(client, config);
        configured.input_state.set_mode("custom".to_owned());
        configured.search_prompt = Some(SearchPrompt {
            node_id: NodeId(7),
            query: "stale".to_owned(),
        });

        fs::write(&config_path, "").expect("rewrite config without custom mode");

        configured.reload_config().expect("reload config");

        assert_eq!(configured.input_state.current_mode(), NORMAL_MODE);
        assert_eq!(configured.search_prompt, None);
    }
}