computer-use-linux 0.7.12

Linux desktop control over MCP — AT-SPI accessibility tree, multi-compositor window targeting (GNOME, KWin, Hyprland, i3, COSMIC), screencast portal screenshots, and ydotool input synthesis. Wayland-first, X11 best-effort.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
use crate::atspi_tree::{
    focused_element_summary_in_app, list_accessible_apps, object_ref_owner_pid,
    perform_action as invoke_accessibility_action, perform_named_action, probe_focused_element,
    set_element_value, snapshot_accessibility_tree, AccessibilityAction, AccessibilityNode,
    AccessibleAppSummary, Bounds, FocusProbe, FocusedElementSummary, ValueSetInvocation,
};
use crate::diagnostics::{doctor_report, setup_accessibility_report, DoctorReport, SetupReport};
use crate::gnome_extension::{setup_window_targeting_report, WindowTargetingSetupReport};
use crate::remote_desktop::{
    click as portal_click, drag as portal_drag, keysyms_for_text, press_key_chord,
    press_keycode_chord, scroll as portal_scroll, start_portal_keyboard_session,
    start_portal_pointer_session, type_text_with_keysyms, PointerButton, PortalKey,
    PortalKeyboardSession, PortalPointerSession, ScrollDirection,
};
use crate::screenshot::{
    capture_screenshot_raw, prepare_screenshot_payload, RawScreenshotCapture, ScreenshotCapture,
    ScreenshotOutputFormat, ScreenshotPayloadOptions,
};
use crate::terminal::{terminal_paste_shortcut, TerminalPasteShortcut};
use crate::windowing::registry;
use crate::windows::{
    focus_window_target, focused_window, list_windows, resolve_window_target,
    window_permission_hint, WindowFocusResult, WindowInfo, WindowTarget,
    GNOME_SHELL_EXTENSION_BACKEND, GNOME_SHELL_INTROSPECT_BACKEND, KWIN_BACKEND,
};
use crate::ydotool;
use anyhow::{Context, Result};
use rmcp::{
    handler::server::wrapper::{Json, Parameters},
    model::{CallToolResult, ContentBlock},
    schemars::JsonSchema,
    tool, tool_handler, tool_router, ErrorData, ServerHandler, ServiceExt,
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    env,
    ffi::OsString,
    future::Future,
    path::Path,
    process::{Command, Output, Stdio},
    sync::{Arc, Mutex},
    time::Duration,
};
use tokio::{
    process::Command as TokioCommand,
    time::{sleep, timeout},
};
use xkeysym::key as xkey;
use zbus::{Connection as ZbusConnection, Proxy as ZbusProxy};

const INPUT_COMMAND_TIMEOUT: Duration = Duration::from_secs(10);
const MAX_SCROLL_PAGES: f64 = 100.0;
const YDOTOOL_TYPE_CHARS_PER_SECOND: u64 = 20;
const KDE_CLIPBOARD_DBUS_TIMEOUT: Duration = Duration::from_secs(3);
const KDE_KLIPPER_SERVICE: &str = "org.kde.klipper";
const KDE_KLIPPER_PATH: &str = "/klipper";
const KDE_KLIPPER_INTERFACE: &str = "org.kde.klipper.klipper";
const SHELL_ENABLE_ENV: &str = "COMPUTER_USE_LINUX_ENABLE_SHELL";
const COMPLETION_ENABLE_ENV: &str = "COMPUTER_USE_LINUX_NOTIFY_ON_COMPLETE";
const SHELL_DEFAULT_TIMEOUT_SECS: u64 = 30;
const SHELL_MAX_TIMEOUT_SECS: u64 = 120;
const SHELL_MAX_COMMAND_BYTES: usize = 64 * 1024;
const SHELL_MAX_CWD_BYTES: usize = 4096;
const SHELL_MAX_ENV_ENTRIES: usize = 64;
const SHELL_MAX_ENV_BYTES: usize = 64 * 1024;
const SHELL_RESPONSE_STREAM_BYTES: usize = 512 * 1024;

/// Nodes and their app ownership must come from the same snapshot.
#[derive(Default)]
struct CachedAccessibilitySnapshot {
    nodes: Vec<AccessibilityNode>,
    pid: Option<u32>,
}

#[derive(Clone, Default)]
pub struct ComputerUseLinux {
    last_snapshot: Arc<Mutex<CachedAccessibilitySnapshot>>,
    portal_pointer_session: Arc<Mutex<Option<PortalPointerSession>>>,
    portal_keyboard_session: Arc<Mutex<Option<PortalKeyboardSession>>>,
    /// Lazily-created uinput absolute pointer (preferred coordinate backend).
    abs_pointer: Arc<Mutex<Option<crate::abs_pointer::AbsPointer>>>,
    portal_session_init_lock: Arc<tokio::sync::Mutex<()>>,
    input_operation_lock: Arc<tokio::sync::Mutex<()>>,
    kde_clipboard_lock: Arc<tokio::sync::Mutex<()>>,
    /// Cached physical desktop size from the most recent full-frame capture;
    /// used for off-screen warnings and portal logical-coordinate mapping.
    desktop_size: Arc<Mutex<Option<(u32, u32)>>>,
    /// On-screen activity overlay (opt-out via `COMPUTER_USE_LINUX_INDICATOR=0`).
    indicator: Arc<crate::indicator::Indicator>,
}

fn sanitize_unsigned_integer_formats(value: &mut serde_json::Value) {
    let serde_json::Value::Object(object) = value else {
        return;
    };

    let has_unsigned_format = matches!(
        object.get("format").and_then(serde_json::Value::as_str),
        Some("uint" | "uint8" | "uint16" | "uint32" | "uint64" | "usize")
    );
    if has_unsigned_format {
        object.remove("format");
    }

    for nested in object.values_mut() {
        match nested {
            serde_json::Value::Object(_) => sanitize_unsigned_integer_formats(nested),
            serde_json::Value::Array(items) => {
                for item in items {
                    sanitize_unsigned_integer_formats(item);
                }
            }
            _ => {}
        }
    }
}

impl ComputerUseLinux {
    fn mcp_tool_router(&self) -> rmcp::handler::server::router::tool::ToolRouter<Self> {
        self.router_with_completion(env::var(COMPLETION_ENABLE_ENV).as_deref() == Ok("1"))
    }

    fn router_with_completion(
        &self,
        enabled: bool,
    ) -> rmcp::handler::server::router::tool::ToolRouter<Self> {
        let mut router = Self::tool_router();
        if !enabled {
            router.map.remove("complete_interaction");
        }
        if !shell_execution_enabled() {
            router.map.remove("run_shell");
        }
        for route in router.map.values_mut() {
            let input_schema = Arc::make_mut(&mut route.attr.input_schema);
            for value in input_schema.values_mut() {
                sanitize_unsigned_integer_formats(value);
            }
            if let Some(output_schema) = route.attr.output_schema.as_mut() {
                for value in Arc::make_mut(output_schema).values_mut() {
                    sanitize_unsigned_integer_formats(value);
                }
            }
        }
        router
    }
}

#[tool_router]
impl ComputerUseLinux {
    #[tool(
        name = "complete_interaction",
        description = "Send a desktop notification that this agent has finished its interaction. Available only with COMPUTER_USE_LINUX_NOTIFY_ON_COMPLETE=1. This cue does not acquire or release an exclusive desktop lock. Delivery is best effort and bounded; it may be suppressed by desktop notification settings.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn complete_interaction(&self) -> Json<CompletionOutput> {
        if env::var(COMPLETION_ENABLE_ENV).as_deref() != Ok("1") {
            return Json(CompletionOutput {
                ok: true,
                cue: "skipped".into(),
                note: "Completion notifications are disabled.".into(),
            });
        }
        Json(send_completion_notification(Path::new("notify-send"), Duration::from_secs(2)).await)
    }

    #[tool(
        name = "doctor",
        description = "Report Linux Computer Use desktop integration readiness.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn doctor(&self) -> Json<DoctorReport> {
        Json(
            tokio::task::spawn_blocking(doctor_report)
                .await
                .expect("diagnostics task panicked"),
        )
    }

    #[tool(
        name = "setup_accessibility",
        description = "Enable GNOME accessibility through gsettings so Linux Computer Use can read AT-SPI trees.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn setup_accessibility(&self) -> Json<SetupReport> {
        Json(
            tokio::task::spawn_blocking(setup_accessibility_report)
                .await
                .expect("accessibility setup task panicked"),
        )
    }

    #[tool(
        name = "setup_window_targeting",
        description = "Install and enable the optional GNOME Shell extension used for exact window list/focus targeting when GNOME blocks native introspection.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = false
        )
    )]
    async fn setup_window_targeting(&self) -> Json<WindowTargetingSetupReport> {
        Json(setup_window_targeting_report().await)
    }

    #[tool(
        name = "list_apps",
        description = "List running Linux desktop app candidates visible to the Computer Use backend.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn list_apps(&self) -> Json<ListAppsOutput> {
        let (accessible_apps, accessibility_error) = match list_accessible_apps(50).await {
            Ok(apps) => (apps, None),
            Err(error) => (Vec::new(), Some(format!("{error:#}"))),
        };

        Json(ListAppsOutput {
            apps: list_process_apps(),
            accessible_apps,
            accessibility_error,
            note: "Linux Computer Use lists process candidates plus AT-SPI application roots when accessibility is enabled.".to_string(),
        })
    }

    #[tool(
        name = "list_windows",
        description = "List compositor windows with title, app id, class, focus state, client type, and known bounds.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn list_windows(&self) -> Json<ListWindowsOutput> {
        Json(window_list_output().await)
    }

    #[tool(
        name = "focused_window",
        description = "Return the compositor window that currently has keyboard focus.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn focused_window(&self) -> Json<FocusedWindowOutput> {
        match focused_window().await {
            Ok(window) => {
                let backend = window_backend(window.as_ref().into_iter());
                Json(FocusedWindowOutput {
                    backend,
                    focused_window: window,
                    error: None,
                    permissions_hint: None,
                    message:
                        "Focused window query completed through the available compositor window backend."
                            .to_string(),
                })
            }
            Err(error) => {
                let error = format!("{error:#}");
                Json(FocusedWindowOutput {
                    backend: GNOME_SHELL_INTROSPECT_BACKEND.to_string(),
                    focused_window: None,
                    permissions_hint: window_permission_hint(&error),
                    error: Some(error),
                    message: "Focused window query failed; targeted keyboard input is unavailable until window introspection works.".to_string(),
                })
            }
        }
    }

    #[tool(
        name = "activate_window",
        description = "Focus a Linux desktop window by window_id, pid, app_id, wm_class, title, or terminal selectors when the compositor permits it.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn activate_window(
        &self,
        Parameters(params): Parameters<ActivateWindowParams>,
    ) -> Json<ActivateWindowOutput> {
        let target = params.into_target();
        let received = Some(serde_json::json!(target.clone()));
        self.indicator.action("activate_window").await;
        match focus_window_target(&target).await {
            Ok(focus) => {
                let ok = focus_satisfies_target(&focus, &target);
                Json(ActivateWindowOutput {
                    ok,
                    implemented: true,
                    backend: focus.backend.clone(),
                    focus: Some(focus),
                    error: None,
                    permissions_hint: None,
                    received,
                })
            }
            Err(error) => {
                let error = format!("{error:#}");
                Json(ActivateWindowOutput {
                    ok: false,
                    implemented: true,
                    backend: GNOME_SHELL_INTROSPECT_BACKEND.to_string(),
                    focus: None,
                    permissions_hint: window_permission_hint(&error),
                    error: Some(error),
                    received,
                })
            }
        }
    }

    #[tool(
        name = "get_app_state",
        output_schema = rmcp::handler::server::tool::schema_for_output::<GetAppStateOutput>()
            .expect("get_app_state output schema"),
        description = "Start an app use session if needed, then get a size-bounded screenshot and accessibility state for a Linux app. Scope the accessibility tree with app_name_or_bundle_identifier or a window_id/pid/app_id/wm_class/title target; omitting a target returns the whole desktop tree and can flood context. Screenshot results include coordinate_width, coordinate_height, scale, format, and quality when the returned image is downscaled or compressed; callers can request jpeg/quality for compression before resizing.",
        annotations(
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn get_app_state(
        &self,
        Parameters(params): Parameters<GetAppStateParams>,
    ) -> Result<CallToolResult, ErrorData> {
        let captures = params.include_screenshot.unwrap_or(true);
        let result = self.app_state(params).await;
        if captures {
            self.indicator.action("get_app_state").await;
        }
        result
    }

    async fn app_state(&self, params: GetAppStateParams) -> Result<CallToolResult, ErrorData> {
        let verbose = params.verbose.unwrap_or(false);
        let mut diagnostics = tokio::task::spawn_blocking(doctor_report)
            .await
            .expect("diagnostics task panicked");
        let (window_context, window_error, window_permissions_hint) =
            self.resolve_window_context(&params).await;
        let (max_nodes, max_depth) =
            crate::atspi_tree::snapshot_limits(params.max_nodes, params.max_depth);
        let include_screenshot = params.include_screenshot.unwrap_or(true);
        let screenshot_options = params.screenshot_options();
        let screenshot_target_requested = params.window_target().has_target();
        // Whether the caller asked for any scope at all. `tree_scoped` can still
        // come back false when a target matched no AT-SPI root, and that case
        // needs different advice than "pass a target".
        let accessibility_target_requested = screenshot_target_requested
            || params
                .app_name_or_bundle_identifier
                .as_deref()
                .is_some_and(|name| !name.trim().is_empty());
        let app_filter = self
            .resolve_accessibility_app_filter(&params, window_context.as_ref())
            .await;
        let (screenshot, screenshot_error) = if include_screenshot {
            let result: Result<ScreenshotCapture> = async {
                let _pipeline = crate::screenshot_impl::screenshot_pipeline_permit().await?;
                let raw = {
                    let _hold = self.indicator.hold_for_capture().await?;
                    capture_screenshot_raw().await
                };
                diagnostics.readiness.record_screenshot_result(raw.is_ok());
                let raw = raw?;
                self.cache_desktop_size(raw.width, raw.height);
                let crop = if let Some(window) = window_context.as_ref() {
                    ensure_readonly_screenshot_target_is_visible(window)?;
                    Some(self.window_crop_rect_for_capture(window, &raw).await?)
                } else {
                    None
                };
                crate::screenshot_impl::run_image_task(move || {
                    prepare_app_state_screenshot(
                        raw,
                        crop,
                        screenshot_target_requested,
                        screenshot_options,
                    )
                })
                .await
            }
            .await;
            match result {
                Ok(capture) => (Some(capture), None),
                Err(error) => (None, Some(format!("{error:#}"))),
            }
        } else {
            (None, None)
        };
        let mut tree_scoped = false;
        let mut tree_root_pid = None;
        let mut accessibility_tree_truncated = false;
        let (accessibility_tree, accessibility_tree_raw_count, accessibility_error) =
            if diagnostics.readiness.can_build_accessibility_tree {
                let target_pid = window_context.as_ref().and_then(|window| window.pid);
                match snapshot_accessibility_tree(
                    app_filter.as_deref(),
                    target_pid,
                    max_nodes,
                    max_depth,
                )
                .await
                {
                    Ok(snapshot) => {
                        tree_scoped = snapshot.scoped;
                        tree_root_pid = snapshot.root_pid;
                        accessibility_tree_truncated = snapshot.truncated;
                        let raw_count = snapshot.nodes.len();
                        (compact_accessibility_tree(snapshot.nodes), raw_count, None)
                    }
                    Err(error) => (Vec::new(), 0, Some(format!("{error:#}"))),
                }
            } else {
                (
                    Vec::new(),
                    0,
                    Some(
                        "GNOME accessibility is disabled; call setup_accessibility first."
                            .to_string(),
                    ),
                )
            };
        if accessibility_error.is_none() {
            // Record a pid only when the tree's roots were matched by it. A pid
            // with no AT-SPI root falls back to every app, or to an app-name
            // match that can be another app entirely; recording the pid then
            // would let that app's index pass the target check instead of
            // taking the per-node owner lookup.
            self.commit_snapshot(&accessibility_tree, tree_root_pid)
                .await;
        } else {
            self.commit_snapshot(&[], None).await;
        }
        let mut message = if let Some(error) = &accessibility_error {
            format!("MCP registration is working, but AT-SPI tree extraction failed: {error}")
        } else if let Some(capture) = &screenshot {
            format!(
                "MCP registration, screenshot capture, and AT-SPI tree extraction are working. Captured {} accessibility nodes (compacted from {}) and a screenshot through {}.",
                accessibility_tree.len(),
                accessibility_tree_raw_count,
                capture.source
            )
        } else if let Some(error) = &screenshot_error {
            format!(
                "MCP registration and AT-SPI tree extraction are working. Captured {} accessibility nodes (compacted from {}). Screenshot capture failed: {error}",
                accessibility_tree.len(),
                accessibility_tree_raw_count,
            )
        } else {
            format!(
                "MCP registration and AT-SPI tree extraction are working. Captured {} accessibility nodes (compacted from {}). Screenshot capture was not requested.",
                accessibility_tree.len(),
                accessibility_tree_raw_count,
            )
        };
        if let Some(window) = &window_context {
            message.push_str(&format!(
                " Window target resolved to window_id {}.",
                window.window_id
            ));
        } else if let Some(error) = &window_error {
            message.push_str(&format!(" Window target resolution failed: {error}"));
        }
        if let Some(warning) = unscoped_accessibility_tree_warning(
            tree_scoped,
            accessibility_error.is_none(),
            accessibility_target_requested,
        ) {
            message.push(' ');
            message.push_str(warning);
        }
        if let Some(note) = truncated_accessibility_tree_note(accessibility_tree_truncated) {
            message.push(' ');
            message.push_str(note);
        }

        // Full diagnostics are huge (portal/process dumps); emit them only on
        // request. The compact readiness block always travels, and failures get
        // a pointer to verbose=true instead of an automatic dump.
        let readiness = diagnostics.readiness.clone();
        let include_full = verbose;
        if !include_full
            && (accessibility_error.is_some()
                || screenshot_error.is_some()
                || window_error.is_some())
        {
            message.push_str(" Pass verbose=true for full diagnostics.");
        }
        let output = GetAppStateOutput {
            app_name_or_bundle_identifier: params.app_name_or_bundle_identifier,
            window_context,
            window_error,
            window_permissions_hint,
            backend: "linux-atspi".to_string(),
            screenshot: screenshot.as_ref().map(ScreenshotSummary::from),
            screenshot_error,
            accessibility_tree,
            accessibility_tree_raw_count,
            tree_scoped,
            accessibility_tree_truncated,
            accessibility_error,
            readiness,
            diagnostics: include_full.then_some(diagnostics),
            message,
        };
        app_state_result(output, screenshot)
    }

    #[tool(
        name = "screenshot",
        description = "Capture the screen and return it as a viewable, size-bounded image. Optionally target a window (window_id/pid/wm_class/title/app_id): the window is raised to the front and the image is cropped before any resize. Returns the image plus a short caption with returned dimensions, coordinate dimensions, scale, format, quality, source, and crop bounds; callers can request jpeg/quality for compression before resizing.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn screenshot(&self, Parameters(params): Parameters<ScreenshotParams>) -> CallToolResult {
        let result = match self.capture_screenshot(params).await {
            Ok(result) => result,
            Err(error) => CallToolResult::error(vec![ContentBlock::text(format!("{error:#}"))]),
        };
        self.indicator.action("screenshot").await;
        result
    }

    async fn capture_screenshot(&self, params: ScreenshotParams) -> Result<CallToolResult> {
        let _pipeline = crate::screenshot_impl::screenshot_pipeline_permit().await?;
        let target = params.window_target();
        let target_window = match target.as_ref() {
            Some(target) => Some(
                self.resolve_screenshot_window(target, params.raise_window.unwrap_or(true))
                    .await
                    .context("targeted screenshot failed")?,
            ),
            None => None,
        };
        let crop_window = (!params.full_screen.unwrap_or(false))
            .then_some(target_window.as_ref())
            .flatten();
        let window_label = target_window
            .as_ref()
            .and_then(|window| window.title.clone());

        let raw_capture = {
            let _hold = self.indicator.hold_for_capture().await?;
            capture_screenshot_raw()
                .await
                .context("screenshot failed")?
        };
        self.cache_desktop_size(raw_capture.width, raw_capture.height);

        // Warn when the target window extends past the visible desktop: the
        // portal only captures on-screen pixels, so the crop silently loses the
        // off-screen region while coordinate metadata still claims full size.
        let off_screen_note = match crop_window.and_then(|window| window.bounds.as_ref()) {
            Some(bounds) => self.off_screen_note_for_bounds(bounds).await,
            None => None,
        };

        let (capture, cropped) = match crop_window {
            Some(window) => {
                let (x, y, width, height) = self
                    .window_crop_rect_for_capture(window, &raw_capture)
                    .await
                    .context("targeted screenshot crop failed")?;
                let (raw_capture, bytes, width, height) =
                    crate::screenshot_impl::run_image_task(move || {
                        let (bytes, width, height) =
                            crop_png(&raw_capture.bytes, x, y, width, height).map_err(|error| {
                                anyhow::anyhow!("targeted screenshot crop failed: {error}")
                            })?;
                        Ok((raw_capture, bytes, width, height))
                    })
                    .await?;
                (
                    RawScreenshotCapture {
                        mime_type: raw_capture.mime_type,
                        bytes,
                        source: raw_capture.source,
                        width,
                        height,
                    },
                    true,
                )
            }
            None => (raw_capture, false),
        };
        let options = params.screenshot_options();
        let capture = crate::screenshot_impl::run_image_task(move || {
            prepare_screenshot_payload(capture, options)
        })
        .await
        .context("screenshot resize failed")?;

        let mut caption = serde_json::json!({
            "width": capture.width,
            "height": capture.height,
            "coordinate_width": capture.coordinate_width,
            "coordinate_height": capture.coordinate_height,
            "scale": capture.scale,
            "resized": capture.resized,
            "bytes": capture.bytes,
            "original_bytes": capture.original_bytes,
            "max_bytes": capture.max_bytes,
            "format": capture.format,
            "quality": capture.quality,
            "source": capture.source,
            "cropped_to_window": cropped,
            "window_title": window_label,
        });
        if let Some(note) = off_screen_note {
            caption["window_off_screen"] = serde_json::json!(true);
            caption["off_screen_note"] = serde_json::json!(note);
        }
        Ok(CallToolResult::success(vec![
            ContentBlock::image(data_url_payload(&capture.data_url), capture.mime_type),
            ContentBlock::text(caption.to_string()),
        ]))
    }

    /// Lazily create the uinput absolute pointer, sizing its ABS range to the
    /// full capture's pixel dimensions. This preserves the coordinate space
    /// used by click/scroll/drag, independently of logical monitor scaling.
    /// Explicit backend overrides skip this device, including a cached one.
    async fn ensure_abs_pointer(&self) -> bool {
        if env_flag_enabled("CU_DISABLE_ABS_POINTER")
            || env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER")
            || (env_flag_enabled("COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER")
                && self.is_wayland_session())
        {
            return false;
        }
        if self
            .abs_pointer
            .lock()
            .map(|g| g.is_some())
            .unwrap_or(false)
        {
            return true;
        }
        let Ok(cap) = capture_screenshot_raw().await else {
            return false;
        };
        self.cache_desktop_size(cap.width, cap.height);
        match tokio::task::spawn_blocking(move || {
            crate::abs_pointer::AbsPointer::create(cap.width as i32, cap.height as i32)
        })
        .await
        {
            Ok(Ok(pointer)) => {
                if let Ok(mut guard) = self.abs_pointer.lock() {
                    *guard = Some(pointer);
                    return true;
                }
                false
            }
            _ => false,
        }
    }

    #[tool(
        name = "click",
        description = "Click an element by index, semantic selector, or desktop coordinate pixels from screenshot metadata. Plain left activation prefers a native AT-SPI click/press/toggle action, avoiding toolkit coordinate scaling. Entry activate and slider jump actions are not substituted for pointer clicks. Explicit coordinates, right clicks, and multi-clicks retain pointer semantics.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn click(&self, Parameters(mut params): Parameters<ClickParams>) -> Json<ActionOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let mut portal_target_point = None;
        let mut target_pid = params.pid;
        // Raise the target window first (if specified) so the click lands on the
        // intended app rather than whatever is stacked on top at that pixel.
        let window_target = params.window_target();
        if params.relative == Some(true) && window_target.is_none() {
            return Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "click".to_string(),
                message: "Relative coordinate clicks require a window target.".to_string(),
                received,
            });
        }
        if let Some(target) = window_target {
            let focus = match self.focus_target_for_input(&target).await {
                Ok(focus) => focus,
                Err(message) => {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "click".to_string(),
                        message,
                        received,
                    });
                }
            };
            target_pid = focus_target_pid(focus.as_ref()).or(target_pid);
            tokio::time::sleep(Duration::from_millis(120)).await;
            // Window-relative coordinates: translate by the window's top-left so
            // the agent can click the pixel it saw in a window-cropped screenshot.
            if params.relative == Some(true) {
                let Some(focus) = focus.as_ref() else {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "click".to_string(),
                        message: "Relative coordinate clicks require verified target-window focus."
                            .to_string(),
                        received,
                    });
                };
                let coordinate_map = match self.focused_window_coordinate_map(focus).await {
                    Ok(mapping) => mapping,
                    Err(message) => {
                        return Json(ActionOutput {
                            ok: false,
                            implemented: true,
                            action: "click".to_string(),
                            message,
                            received,
                        });
                    }
                };
                if let Err(message) = apply_window_relative_click_coordinates(
                    &mut params,
                    coordinate_map.capture_rect,
                ) {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "click".to_string(),
                        message,
                        received,
                    });
                }
                portal_target_point = params
                    .x
                    .zip(params.y)
                    .and_then(|(x, y)| coordinate_map.portal_point(x, y));
            }
        }
        if params.x.zip(params.y).is_none() {
            if let Some(node) = self.cached_node_for(
                params.element_index,
                &params.selector(),
                ElementResolvePurpose::Click,
            ) {
                if let Err(message) = self.check_node_target(&node, target_pid).await {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "click".to_string(),
                        message,
                        received,
                    });
                }
            }
        }
        let target = match self.resolve_click_target(&params) {
            Ok(target) => target,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "click".to_string(),
                    message,
                    received,
                });
            }
        };
        if let ClickTarget::PrimaryAction {
            object_ref,
            action_name,
            action_index,
        } = target
        {
            self.indicator.action("click").await;
            let invocation = if let Some(name) = action_name
                .as_deref()
                .filter(|name| !name.trim().is_empty())
            {
                perform_named_action(&object_ref, name).await
            } else {
                invoke_accessibility_action(&object_ref, Some(&action_index.to_string())).await
            };
            return match invocation {
                Ok(invocation) => Json(ActionOutput {
                    ok: invocation.ok,
                    implemented: true,
                    action: "click".to_string(),
                    message: if invocation.ok {
                        format!(
                            "Invoked the primary AT-SPI action{} without pointer-coordinate conversion.",
                            action_name
                                .as_deref()
                                .filter(|name| !name.is_empty())
                                .map(|name| format!(" ({name})"))
                                .unwrap_or_default()
                        )
                    } else {
                        format!(
                            "The primary AT-SPI action{} returned false.",
                            action_name
                                .as_deref()
                                .filter(|name| !name.is_empty())
                                .map(|name| format!(" ({name})"))
                                .unwrap_or_default()
                        )
                    },
                    received,
                }),
                Err(error) => Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "click".to_string(),
                    message: error.to_string(),
                    received,
                }),
            };
        }
        let ClickTarget::Coordinates(x, y) = target else {
            unreachable!("click target must resolve to coordinates or an AT-SPI action");
        };
        self.indicator
            .pointer("click", (x, y), self.indicator_space())
            .await;
        let button = mouse_button_code(params.button.as_deref());
        let click_count = params.click_count.unwrap_or(1).clamp(1, 10).to_string();
        // Preferred backend: the uinput absolute pointer. Unlike ydotool's
        // relative-only device (faked `--absolute` via pin-to-corner + relative
        // move, which acceleration + fractional scaling distort) and unlike the
        // portal (per-monitor coordinate scaling + an approval dialog), the
        // absolute pointer uses screenshot-pixel coordinates directly and
        // reports the point it emitted after desktop-edge clamping.
        if let Some(abs_button) =
            crate::abs_pointer::PointerButton::from_name(params.button.as_deref())
        {
            if self.ensure_abs_pointer().await {
                let pointer = Arc::clone(&self.abs_pointer);
                let count = params.click_count.unwrap_or(1).clamp(1, 10);
                let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                    run_abs_pointer(pointer, move |pointer| {
                        pointer.click(x, y, abs_button, count)
                    })
                    .await
                })
                .await;
                let _input_guard = input_guard;
                return Json(abs_pointer_result("click", result, received));
            }
        }
        let off_screen_note = self.off_screen_note_for_point(x, y).await;
        if let Some(session) = self.cached_portal_pointer_session() {
            let Some((portal_x, portal_y)) =
                portal_target_point.or_else(|| self.logical_portal_point(&session, x, y))
            else {
                self.clear_portal_pointer_session(&session);
                return Json(with_notes(
                    portal_coordinate_error("click", received),
                    off_screen_note.clone(),
                ));
            };
            match portal_click(
                &session,
                portal_x,
                portal_y,
                PointerButton::from_name(params.button.as_deref()),
                params.click_count.unwrap_or(1).clamp(1, 10),
            )
            .await
            {
                Ok(()) => {
                    return Json(with_notes(
                        ActionOutput {
                            ok: true,
                            implemented: true,
                            action: "click".to_string(),
                            message: "Action sent through the remote desktop portal.".to_string(),
                            received,
                        },
                        off_screen_note.clone(),
                    ));
                }
                Err(error) => {
                    self.clear_portal_pointer_session(&session);
                    return Json(with_notes(
                        portal_action_error("click", error, received),
                        off_screen_note.clone(),
                    ));
                }
            }
        } else if self.should_prefer_portal_pointer_backend().await {
            match self.ensure_portal_pointer_session().await {
                Ok(Some(session)) => {
                    let Some((portal_x, portal_y)) =
                        portal_target_point.or_else(|| self.logical_portal_point(&session, x, y))
                    else {
                        self.clear_portal_pointer_session(&session);
                        return Json(with_notes(
                            portal_coordinate_error("click", received),
                            off_screen_note.clone(),
                        ));
                    };
                    match portal_click(
                        &session,
                        portal_x,
                        portal_y,
                        PointerButton::from_name(params.button.as_deref()),
                        params.click_count.unwrap_or(1).clamp(1, 10),
                    )
                    .await
                    {
                        Ok(()) => {
                            return Json(with_notes(
                                ActionOutput {
                                    ok: true,
                                    implemented: true,
                                    action: "click".to_string(),
                                    message: "Action sent through the remote desktop portal."
                                        .to_string(),
                                    received,
                                },
                                off_screen_note.clone(),
                            ));
                        }
                        Err(error) => {
                            self.clear_portal_pointer_session(&session);
                            return Json(with_notes(
                                portal_action_error("click", error, received),
                                off_screen_note.clone(),
                            ));
                        }
                    }
                }
                Ok(None) => {}
                Err(error) => return Json(portal_start_error("click", error, received)),
            }
        }
        if self.is_wayland_session() {
            return Json(unsafe_pointer_fallback("click", received));
        }
        if self.should_prefer_xdotool_pointer() {
            if let Some(xdotool_args) = xdotool_pointer_click_args(
                x,
                y,
                params.click_count.unwrap_or(1).clamp(1, 10),
                params.button.as_deref(),
            ) {
                let ydotool_commands = vec![
                    absolute_mousemove_args(x, y),
                    vec![
                        "click".to_string(),
                        "--repeat".to_string(),
                        click_count.clone(),
                        button.clone(),
                    ],
                ];
                let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                    run_xdotool_pointer_or_fallback(Path::new("xdotool"), &xdotool_args, || async {
                        run_ydotool_sequence(&ydotool_commands).await
                    })
                    .await
                })
                .await;
                let _input_guard = input_guard;
                let used_xdotool = result
                    .as_ref()
                    .is_ok_and(|result| result.backend == KeyboardCommandBackend::Xdotool);
                let mut output =
                    action_result("click", result.map(|result| result.outputs), received);
                if output.ok && used_xdotool {
                    output.message = "Action sent through xdotool (X11 XTEST).".to_string();
                }
                return Json(with_notes(output, off_screen_note));
            }
        }
        let commands = vec![
            absolute_mousemove_args(x, y),
            vec![
                "click".to_string(),
                "--repeat".to_string(),
                click_count,
                button,
            ],
        ];
        let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
            run_ydotool_sequence(&commands).await
        })
        .await;
        let _input_guard = input_guard;
        Json(with_notes(
            action_result("click", result, received),
            off_screen_note,
        ))
    }

    #[tool(
        name = "perform_action",
        description = "Invoke an accessibility action exposed by an element selected by index, identifier, or semantic selector. Defaults to the primary action unless action is provided.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn perform_action(
        &self,
        Parameters(params): Parameters<ActionParams>,
    ) -> Json<ActionOutput> {
        let requested_action = requested_or_primary_action(params.action.as_deref());
        self.perform_element_action(&params, Some(requested_action))
            .await
    }

    #[tool(
        name = "set_value",
        description = "Set the value of a settable accessibility element selected by index, identifier, or semantic selector.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn set_value(
        &self,
        Parameters(params): Parameters<SetValueParams>,
    ) -> Json<ActionOutput> {
        let _input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let received = Some(serde_json::json!(params.clone()));
        let object_ref = match self.resolve_object_ref(
            params.element_index,
            params.element_identifier.as_deref(),
            &params.selector(),
            ElementResolvePurpose::SetValue,
        ) {
            Ok(object_ref) => object_ref,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "set_value".to_string(),
                    message,
                    received,
                });
            }
        };

        self.indicator.text("set_value", &params.value, false).await;
        match set_element_value(&object_ref, &params.value).await {
            Ok(ValueSetInvocation::Numeric { value }) => Json(ActionOutput {
                ok: true,
                implemented: true,
                action: "set_value".to_string(),
                message: format!("AT-SPI numeric value set to {value}."),
                received,
            }),
            Ok(ValueSetInvocation::EditableText) => Json(ActionOutput {
                ok: true,
                implemented: true,
                action: "set_value".to_string(),
                message: "AT-SPI editable text contents set.".to_string(),
                received,
            }),
            Err(error) => Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "set_value".to_string(),
                message: error.to_string(),
                received,
            }),
        }
    }

    #[tool(
        name = "scroll",
        description = "Scroll an element in a direction by a number of pages. With a window target and no x/y/element_index, scrolls at the centre of the targeted window.",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn scroll(&self, Parameters(mut params): Parameters<ScrollParams>) -> Json<ActionOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let units = match scroll_units(params.pages) {
            Ok(units) => units,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "scroll".to_string(),
                    message,
                    received,
                });
            }
        };
        let input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let mut portal_target_point = None;
        let mut target_pid = params.pid;
        // Raise/focus the target window first (parity with click) so wheel
        // events land on the intended app.
        let window_target = params.window_target();
        if params.relative == Some(true) && window_target.is_none() {
            return Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "scroll".to_string(),
                message: "Relative scroll coordinates require a window target.".to_string(),
                received,
            });
        }
        if let Some(target) = window_target {
            let focus = match self.focus_target_for_input(&target).await {
                Ok(focus) => focus,
                Err(message) => {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message,
                        received,
                    });
                }
            };
            target_pid = focus_target_pid(focus.as_ref()).or(target_pid);
            tokio::time::sleep(Duration::from_millis(120)).await;
            if params.relative == Some(true) {
                let Some(focus) = focus.as_ref() else {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message:
                            "Relative scroll coordinates require verified target-window focus."
                                .to_string(),
                        received,
                    });
                };
                let coordinate_map = match self.focused_window_coordinate_map(focus).await {
                    Ok(mapping) => mapping,
                    Err(message) => {
                        return Json(ActionOutput {
                            ok: false,
                            implemented: true,
                            action: "scroll".to_string(),
                            message,
                            received,
                        });
                    }
                };
                if let Err(message) = apply_window_relative_scroll_coordinates(
                    &mut params,
                    coordinate_map.capture_rect,
                ) {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message,
                        received,
                    });
                }
                portal_target_point = params
                    .x
                    .zip(params.y)
                    .and_then(|(x, y)| coordinate_map.portal_point(x, y));
            } else if params.x.is_none() && params.y.is_none() && params.element_index.is_none() {
                // A window target without a point would otherwise scroll
                // whatever happens to sit under the pointer: focusing does not
                // move the cursor, and the wheel path never repositions it.
                // Default to the centre of the resolved target window.
                let Some(focus) = focus.as_ref() else {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message: "Window-targeted scroll requires verified target-window focus."
                            .to_string(),
                        received,
                    });
                };
                let coordinate_map = match self.focused_window_coordinate_map(focus).await {
                    Ok(mapping) => mapping,
                    Err(message) => {
                        return Json(ActionOutput {
                            ok: false,
                            implemented: true,
                            action: "scroll".to_string(),
                            message,
                            received,
                        });
                    }
                };
                if let Err(message) =
                    apply_window_center_scroll_point(&mut params, coordinate_map.capture_rect)
                {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message,
                        received,
                    });
                }
                portal_target_point = params
                    .x
                    .zip(params.y)
                    .and_then(|(x, y)| coordinate_map.portal_point(x, y));
            }
        }
        if params.x.is_none() && params.y.is_none() {
            if let Some(node) = self.cached_node_for(
                params.element_index,
                &ElementSelector::default(),
                ElementResolvePurpose::Click,
            ) {
                if let Err(message) = self.check_node_target(&node, target_pid).await {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message,
                        received,
                    });
                }
            }
        }
        let target_point =
            match self.resolve_optional_target_point(params.x, params.y, params.element_index) {
                Ok(point) => point,
                Err(message) => {
                    return Json(ActionOutput {
                        ok: false,
                        implemented: true,
                        action: "scroll".to_string(),
                        message,
                        received,
                    });
                }
            };
        let direction = match params.direction.to_ascii_lowercase().as_str() {
            "up" => ScrollDirection::Up,
            "down" => ScrollDirection::Down,
            "left" => ScrollDirection::Left,
            "right" => ScrollDirection::Right,
            _ => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "scroll".to_string(),
                    message: "Unsupported scroll direction; expected up, down, left, or right."
                        .to_string(),
                    received,
                });
            }
        };
        match target_point {
            Some(point) => {
                self.indicator
                    .pointer("scroll", point, self.indicator_space())
                    .await
            }
            None => self.indicator.action("scroll").await,
        }
        let (dx, dy) = match direction {
            ScrollDirection::Up => (0, units),
            ScrollDirection::Down => (0, -units),
            ScrollDirection::Left => (units, 0),
            ScrollDirection::Right => (-units, 0),
        };
        // Coordinate scroll needs the same accurate positioning as click. Keep
        // the move and wheel event under one cancellation-safe input guard.
        if let Some((x, y)) = target_point {
            if self.is_wayland_session()
                && ydotool_backend_available().await
                && self.ensure_abs_pointer().await
            {
                let pointer = Arc::clone(&self.abs_pointer);
                let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                    let landing =
                        run_abs_pointer(pointer, move |pointer| pointer.move_to(x, y)).await?;
                    sleep(Duration::from_millis(35)).await;
                    run_ydotool(&wheel_mousemove_args(dx, dy)).await?;
                    Ok(landing)
                })
                .await;
                let _input_guard = input_guard;
                return Json(abs_pointer_result("scroll", result, received));
            }
        }
        let off_screen_note = match target_point {
            Some((x, y)) => self.off_screen_note_for_point(x, y).await,
            None => None,
        };
        if let Some(session) = self.cached_portal_pointer_session() {
            let mapped_target = match (portal_target_point, target_point) {
                (Some(point), _) => Some(Some(point)),
                (None, Some((x, y))) => self.logical_portal_point(&session, x, y).map(Some),
                (None, None) => Some(None),
            };
            let Some(portal_target_point) = mapped_target else {
                self.clear_portal_pointer_session(&session);
                return Json(with_notes(
                    portal_coordinate_error("scroll", received),
                    off_screen_note.clone(),
                ));
            };
            match portal_scroll(&session, portal_target_point, direction, units).await {
                Ok(()) => {
                    return Json(with_notes(
                        ActionOutput {
                            ok: true,
                            implemented: true,
                            action: "scroll".to_string(),
                            message: "Action sent through the remote desktop portal.".to_string(),
                            received,
                        },
                        off_screen_note.clone(),
                    ));
                }
                Err(error) => {
                    self.clear_portal_pointer_session(&session);
                    return Json(with_notes(
                        portal_action_error("scroll", error, received),
                        off_screen_note.clone(),
                    ));
                }
            }
        } else if self.should_prefer_portal_pointer_backend().await {
            match self.ensure_portal_pointer_session().await {
                Ok(Some(session)) => {
                    let mapped_target = match (portal_target_point, target_point) {
                        (Some(point), _) => Some(Some(point)),
                        (None, Some((x, y))) => self.logical_portal_point(&session, x, y).map(Some),
                        (None, None) => Some(None),
                    };
                    let Some(portal_target_point) = mapped_target else {
                        self.clear_portal_pointer_session(&session);
                        return Json(with_notes(
                            portal_coordinate_error("scroll", received),
                            off_screen_note.clone(),
                        ));
                    };
                    match portal_scroll(&session, portal_target_point, direction, units).await {
                        Ok(()) => {
                            return Json(with_notes(
                                ActionOutput {
                                    ok: true,
                                    implemented: true,
                                    action: "scroll".to_string(),
                                    message: "Action sent through the remote desktop portal."
                                        .to_string(),
                                    received,
                                },
                                off_screen_note.clone(),
                            ));
                        }
                        Err(error) => {
                            self.clear_portal_pointer_session(&session);
                            return Json(with_notes(
                                portal_action_error("scroll", error, received),
                                off_screen_note.clone(),
                            ));
                        }
                    }
                }
                Ok(None) => {}
                Err(error)
                    if target_point.is_some()
                        || env_flag_enabled("COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER") =>
                {
                    return Json(portal_start_error("scroll", error, received));
                }
                Err(_) => {}
            }
        }
        if target_point.is_some() && self.is_wayland_session() {
            return Json(unsafe_pointer_fallback("scroll", received));
        }
        let mut sequence = Vec::new();
        if let Some((x, y)) = target_point {
            sequence.push(absolute_mousemove_args(x, y));
        }
        sequence.push(wheel_mousemove_args(dx, dy));
        // X11: ydotool's absolute move warps through (0, 0), so the pointer
        // re-enters the window. GTK 3 resets its XI2 scroll valuators on that
        // enter, and the one wheel event only re-baselines them (delta 0).
        // The call reported success while the window did not move (#184).
        // XTEST wheel buttons have no valuators, so GTK takes the button path.
        if self.should_prefer_xdotool_pointer() {
            let repeat = u32::try_from(units.max(1)).unwrap_or(1);
            let xdotool_args = xdotool_pointer_scroll_args(target_point, repeat, direction);
            let command_timeout = xdotool_scroll_timeout(repeat, XDOTOOL_SCROLL_CLICK_DELAY_MS);
            let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                run_xdotool_pointer_or_fallback_with_timeout(
                    Path::new("xdotool"),
                    &xdotool_args,
                    command_timeout,
                    || async { run_ydotool_sequence(&sequence).await },
                )
                .await
            })
            .await;
            let _input_guard = input_guard;
            let used_xdotool = result
                .as_ref()
                .is_ok_and(|result| result.backend == KeyboardCommandBackend::Xdotool);
            let mut output = action_result("scroll", result.map(|result| result.outputs), received);
            if output.ok && used_xdotool {
                output.message = "Action sent through xdotool (X11 XTEST).".to_string();
            }
            return Json(with_notes(output, off_screen_note));
        }
        let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
            run_ydotool_sequence(&sequence).await
        })
        .await;
        let _input_guard = input_guard;
        Json(with_notes(
            action_result("scroll", result, received),
            off_screen_note,
        ))
    }

    #[tool(
        name = "drag",
        description = "Drag from one point to another using pixel coordinates.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn drag(&self, Parameters(params): Parameters<DragParams>) -> Json<ActionOutput> {
        let received = Some(serde_json::json!(params));
        let input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let space = self.indicator_space();
        self.indicator
            .pointer("drag", (params.start_x, params.start_y), space)
            .await;
        // Lead the overlay cursor to the drop point while the drag runs.
        self.indicator
            .follow("drag", (params.end_x, params.end_y), space)
            .await;
        // Preferred backend: the uinput absolute pointer (accurate landing).
        if self.ensure_abs_pointer().await {
            let pointer = Arc::clone(&self.abs_pointer);
            let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                run_abs_pointer(pointer, move |pointer| {
                    pointer.drag(
                        (params.start_x, params.start_y),
                        (params.end_x, params.end_y),
                        crate::abs_pointer::PointerButton::Left,
                    )
                })
                .await
            })
            .await;
            let _input_guard = input_guard;
            return Json(match result {
                Ok(()) => ActionOutput {
                    ok: true,
                    implemented: true,
                    action: "drag".to_string(),
                    message: "Action sent through the uinput absolute pointer.".to_string(),
                    received,
                },
                Err(error) => abs_pointer_error("drag", error, received),
            });
        }
        if let Some(session) = self.cached_portal_pointer_session() {
            let _ = self.capture_space_rect().await;
            let Some((start_x, start_y)) =
                self.logical_portal_point(&session, params.start_x, params.start_y)
            else {
                self.clear_portal_pointer_session(&session);
                return Json(portal_coordinate_error("drag", received));
            };
            let Some((end_x, end_y)) =
                self.logical_portal_point(&session, params.end_x, params.end_y)
            else {
                self.clear_portal_pointer_session(&session);
                return Json(portal_coordinate_error("drag", received));
            };
            match portal_drag(&session, start_x, start_y, end_x, end_y).await {
                Ok(()) => {
                    return Json(ActionOutput {
                        ok: true,
                        implemented: true,
                        action: "drag".to_string(),
                        message: "Action sent through the remote desktop portal.".to_string(),
                        received,
                    });
                }
                Err(error) => {
                    self.clear_portal_pointer_session(&session);
                    return Json(portal_action_error("drag", error, received));
                }
            }
        } else if self.should_prefer_portal_pointer_backend().await {
            let _ = self.capture_space_rect().await;
            match self.ensure_portal_pointer_session().await {
                Ok(Some(session)) => {
                    let Some((start_x, start_y)) =
                        self.logical_portal_point(&session, params.start_x, params.start_y)
                    else {
                        self.clear_portal_pointer_session(&session);
                        return Json(portal_coordinate_error("drag", received));
                    };
                    let Some((end_x, end_y)) =
                        self.logical_portal_point(&session, params.end_x, params.end_y)
                    else {
                        self.clear_portal_pointer_session(&session);
                        return Json(portal_coordinate_error("drag", received));
                    };
                    match portal_drag(&session, start_x, start_y, end_x, end_y).await {
                        Ok(()) => {
                            return Json(ActionOutput {
                                ok: true,
                                implemented: true,
                                action: "drag".to_string(),
                                message: "Action sent through the remote desktop portal."
                                    .to_string(),
                                received,
                            });
                        }
                        Err(error) => {
                            self.clear_portal_pointer_session(&session);
                            return Json(portal_action_error("drag", error, received));
                        }
                    }
                }
                Ok(None) => {}
                Err(error) => return Json(portal_start_error("drag", error, received)),
            }
        }
        if self.is_wayland_session() {
            return Json(unsafe_pointer_fallback("drag", received));
        }
        let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
            run_ydotool_drag(params.start_x, params.start_y, params.end_x, params.end_y).await
        })
        .await;
        let _input_guard = input_guard;
        Json(action_result("drag", result, received))
    }

    #[tool(
        name = "press_key",
        description = "Press a key or key-combination on the keyboard, optionally after focusing a target window or terminal selector. Key grammar (case-insensitive; hyphens/spaces ignored): combos join with '+', e.g. Ctrl+L or Ctrl+Shift+T. Modifiers: ctrl/control, alt/option, shift, meta/super/cmd/command. Named keys: enter/return, escape/esc, tab, backspace, delete/del, space, home, end, pageup, pagedown, arrowleft/left, arrowright/right, arrowup/up, arrowdown/down, f1-f12. Plus single US letters a-z and digits 0-9. Anything else returns an error (never silently dropped). On Wayland, chords are sent through an active remote desktop portal keyboard session when one is available (or when ydotool is absent), falling back to ydotool otherwise. Portal chords send modifiers and named keys as keysyms so remapped keys (e.g. Caps Lock swapped with Control) follow the active keymap; letters and digits are physical US positions. Note: compositor-level shortcuts (e.g. Super+Up) may be consumed by GNOME before reaching the app.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn press_key(
        &self,
        Parameters(params): Parameters<PressKeyParams>,
    ) -> Json<ActionOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let focus = match self.focus_target_for_input(&params.window_target()).await {
            Ok(focus) => focus,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "press_key".to_string(),
                    message,
                    received,
                });
            }
        };
        let Some((chord_modifiers, chord_key)) = key_chord(&params.key) else {
            return Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "press_key".to_string(),
                message: "Unsupported key. Use names like Enter, Escape, Tab, ArrowLeft, Super, Ctrl+L, or a single US keyboard letter/digit.".to_string(),
                received,
            });
        };
        let secret = self.typing_into_secret(focus.as_ref()).await;
        self.indicator.keys(&params.key, secret).await;
        if self.should_prefer_portal_keyboard_for_chords().await {
            match self.ensure_portal_keyboard_session().await {
                Ok(Some(session)) => {
                    // KDE keeps physical keycodes: its keysym path is untested
                    // here, and #191 was reported against mutter.
                    let (modifiers, key) = portal_key_chord(&params.key)
                        .filter(|_| !self.is_kde_wayland_session())
                        .unwrap_or_else(|| {
                            let modifiers = chord_modifiers
                                .iter()
                                .map(|m| PortalKey::Keycode(i32::from(*m)))
                                .collect();
                            (modifiers, PortalKey::Keycode(i32::from(chord_key)))
                        });
                    match press_key_chord(&session, &modifiers, key).await {
                        Ok(()) => {
                            let notes = self.input_landing_notes(focus.as_ref(), false).await;
                            return Json(with_notes(
                                successful_action_with_focus(
                                    "press_key",
                                    "Action sent through the remote desktop portal.",
                                    received,
                                    focus,
                                ),
                                notes,
                            ));
                        }
                        Err(error) => {
                            self.clear_portal_keyboard_session(&session);
                            return Json(action_result_with_focus(
                                "press_key",
                                Err(format!("{error:#}")),
                                received,
                                focus,
                            ));
                        }
                    }
                }
                Ok(None) => {}
                Err(_) => {}
            }
        }
        let Some(key_events) = key_sequence(&params.key) else {
            return Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "press_key".to_string(),
                message: "Unsupported key. Use names like Enter, Escape, Tab, ArrowLeft, Super, Ctrl+L, or a single US keyboard letter/digit.".to_string(),
                received,
            });
        };
        // X11: prefer xdotool/XTEST. ydotool's raw evdev scancodes get
        // re-mapped by the active XKB layout on X11, so named keys and chords
        // arrive as stray glyphs instead of real key events (issue #58).
        if self.should_prefer_xdotool_keyboard() {
            if let Some(spec) = xdotool_key_spec(&params.key) {
                let xdotool_args = vec!["key".to_string(), "--clearmodifiers".to_string(), spec];
                let ydotool_args =
                    ydotool_key_args(key_events.clone(), !chord_modifiers.is_empty());
                let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                    run_xdotool_or_fallback(Path::new("xdotool"), &xdotool_args, || {
                        run_ydotool(&ydotool_args)
                    })
                    .await
                })
                .await;
                let _input_guard = input_guard;
                let used_xdotool = result
                    .as_ref()
                    .is_ok_and(|result| result.backend == KeyboardCommandBackend::Xdotool);
                let mut output = action_result_with_focus(
                    "press_key",
                    result.map(|result| vec![result.output]),
                    received,
                    focus.clone(),
                );
                if used_xdotool {
                    output.message = "Action sent through xdotool (X11 XTEST).".to_string();
                }
                if output.ok && focus.is_some() {
                    let notes = self.input_landing_notes(focus.as_ref(), false).await;
                    output = with_notes(output, notes);
                }
                return Json(output);
            }
        }
        let args = ydotool_key_args(key_events, !chord_modifiers.is_empty());
        let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
            run_ydotool(&args).await.map(|output| vec![output])
        })
        .await;
        let _input_guard = input_guard;
        let mut output = action_result_with_focus("press_key", result, received, focus.clone());
        if output.ok && focus.is_some() {
            let notes = self.input_landing_notes(focus.as_ref(), false).await;
            output = with_notes(output, notes);
        }
        Json(output)
    }

    #[tool(
        name = "run_shell",
        description = "Execute one explicitly approved /bin/sh command with same-user host authority. This tool is absent unless the server operator starts computer-use-linux with COMPUTER_USE_LINUX_ENABLE_SHELL=1. It is not sandboxed: the command can read or modify files and use the network with the server user's permissions. The inherited environment is cleared to a small desktop/runtime allowlist; pass any additional variables explicitly. Execution time and output are bounded: returned streams are truncated to 512 KiB, while a stream exceeding the 8 MiB collection ceiling fails the call without returning partial output. An audit digest is written to server stderr.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn run_shell(
        &self,
        Parameters(params): Parameters<RunShellParams>,
    ) -> Json<RunShellOutput> {
        Json(execute_shell(params).await)
    }

    #[tool(
        name = "type_text",
        description = "Type literal text using keyboard input, optionally after focusing a target window or terminal selector.",
        annotations(
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn type_text(
        &self,
        Parameters(params): Parameters<TypeTextParams>,
    ) -> Json<ActionOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let window_target = params.window_target();
        let focus = match self.focus_target_for_input(&window_target).await {
            Ok(focus) => focus,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "type_text".to_string(),
                    message,
                    received,
                });
            }
        };
        let secret = self.typing_into_secret(focus.as_ref()).await;
        self.indicator.text("type_text", &params.text, secret).await;
        if self.should_prefer_kde_clipboard_text_backend() {
            match self.ensure_portal_keyboard_session().await {
                Ok(Some(session)) => {
                    let kde_focus = if window_target.has_target() {
                        match self.focus_target_for_input(&window_target).await {
                            Ok(focus) => focus,
                            Err(message) => {
                                return Json(ActionOutput {
                                    ok: false,
                                    implemented: true,
                                    action: "type_text".to_string(),
                                    message,
                                    received,
                                });
                            }
                        }
                    } else {
                        focus.clone()
                    };
                    let paste_shortcut =
                        kde_clipboard_paste_shortcut(&window_target, kde_focus.as_ref()).await;
                    let _clipboard_guard = self.kde_clipboard_lock.lock().await;
                    match run_kde_clipboard_paste_text(&session, &params.text, paste_shortcut).await
                    {
                        Ok(message) => {
                            let notes = self.input_landing_notes(kde_focus.as_ref(), true).await;
                            return Json(with_notes(
                                successful_action_with_focus(
                                    "type_text",
                                    &message,
                                    received,
                                    kde_focus,
                                ),
                                notes,
                            ));
                        }
                        Err(error) => {
                            if error.clear_portal_keyboard_session {
                                self.clear_portal_keyboard_session(&session);
                            }
                            if !error.can_fallback_to_ydotool {
                                return Json(action_result_with_focus(
                                    "type_text",
                                    Err(error.message),
                                    received,
                                    kde_focus,
                                ));
                            }
                        }
                    }
                }
                Ok(None) => {}
                Err(_) => {}
            }
        }
        if self.should_prefer_portal_text_backend() {
            let result = async {
                let keysyms = keysyms_for_text(&params.text)
                    .map_err(|error| format!("Literal text was not sent: {error:#}"))?;
                if self.is_gnome_wayland_session() {
                    crate::keyboard_keymap::validate_gnome_text(&params.text, &keysyms)
                        .await
                        .map_err(|error| format!("Literal text was not sent: {error:#}"))?;
                }
                let session = self
                    .ensure_portal_keyboard_session()
                    .await
                    .map_err(|error| format!("Portal text input could not start: {error:#}"))?
                    .ok_or_else(|| {
                        "Portal keyboard input is unavailable; no text was sent.".to_string()
                    })?;
                type_text_with_keysyms(&session, &keysyms)
                    .await
                    .map_err(|error| {
                        self.clear_portal_keyboard_session(&session);
                        format!("{error:#}")
                    })
            }
            .await;
            return Json(match result {
                Ok(()) => {
                    let notes = self.input_landing_notes(focus.as_ref(), true).await;
                    with_notes(
                        successful_action_with_focus(
                            "type_text",
                            "Action sent through the remote desktop portal.",
                            received,
                            focus,
                        ),
                        notes,
                    )
                }
                Err(error) => action_result_with_focus("type_text", Err(error), received, focus),
            });
        }
        // X11: xdotool type resolves keysyms against the live XKB layout.
        // ydotool's raw scancodes get re-mapped by X11 and mangle symbols and
        // digits (`_` → `%`, `1` → `+`) even on a plain US layout (issue #58).
        if self.should_prefer_xdotool_keyboard() {
            let delay_ms = xdotool_type_delay_ms();
            let args = xdotool_type_args_with_delay(&params.text, delay_ms);
            let command_timeout = xdotool_type_timeout(&params.text, delay_ms);
            let text = params.text.clone();
            let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                run_xdotool_or_fallback_with_timeout(
                    Path::new("xdotool"),
                    &args,
                    command_timeout,
                    || run_ydotool_type_text(&text),
                )
                .await
            })
            .await;
            let _input_guard = input_guard;
            let used_xdotool = result
                .as_ref()
                .is_ok_and(|result| result.backend == KeyboardCommandBackend::Xdotool);
            let mut output = action_result_with_focus(
                "type_text",
                result.map(|result| vec![result.output]),
                received,
                focus.clone(),
            );
            if used_xdotool {
                output.message = "Action sent through xdotool (X11 XTEST).".to_string();
            }
            if output.ok && focus.is_some() {
                let notes = self.input_landing_notes(focus.as_ref(), true).await;
                output = with_notes(output, notes);
            }
            return Json(output);
        }
        if self.should_prefer_wtype_keyboard() {
            let text = params.text.clone();
            let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
                run_wtype_type_text_or_fallback(Path::new("wtype"), &text, || {
                    run_ydotool_type_text(&text)
                })
                .await
            })
            .await;
            let _input_guard = input_guard;
            let used_wtype = result
                .as_ref()
                .is_ok_and(|result| result.backend == KeyboardCommandBackend::Wtype);
            let mut output = action_result_with_focus(
                "type_text",
                result.map(|result| vec![result.output]),
                received,
                focus.clone(),
            );
            if used_wtype {
                output.message =
                    "Action sent through wtype (Wayland virtual-keyboard protocol).".to_string();
            }
            if output.ok && focus.is_some() {
                let notes = self.input_landing_notes(focus.as_ref(), true).await;
                output = with_notes(output, notes);
            }
            return Json(output);
        }
        let text = params.text.clone();
        let (input_guard, result) = run_cancellation_safe_input(input_guard, async move {
            run_ydotool_type_text(&text)
                .await
                .map(|output| vec![output])
        })
        .await;
        let _input_guard = input_guard;
        let mut output = action_result_with_focus("type_text", result, received, focus.clone());
        if output.ok && focus.is_some() {
            let notes = self.input_landing_notes(focus.as_ref(), true).await;
            output = with_notes(output, notes);
        }
        Json(output)
    }

    #[tool(
        name = "move_window",
        description = "Move a window to a new desktop position (frame top-left in desktop coordinates). Useful to recover windows that are partially off-screen. Works through the computer-use-linux GNOME Shell extension or a generic X11/EWMH window manager (wmctrl).",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn move_window(
        &self,
        Parameters(params): Parameters<MoveWindowParams>,
    ) -> Json<WindowGeometryOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let target = params.target.clone().into_target();
        self.indicator.action("move_window").await;
        self.window_geometry_op(received, &target, |window| async move {
            registry::move_window(&window, params.x, params.y).await
        })
        .await
    }

    #[tool(
        name = "resize_window",
        description = "Resize a window to a new frame width/height in desktop pixels, unmaximizing it first if needed. Useful to fit a window fully on-screen. Works through the computer-use-linux GNOME Shell extension or a generic X11/EWMH window manager (wmctrl).",
        annotations(
            read_only_hint = false,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn resize_window(
        &self,
        Parameters(params): Parameters<ResizeWindowParams>,
    ) -> Json<WindowGeometryOutput> {
        let received = Some(serde_json::json!(params.clone()));
        let target = params.target.clone().into_target();
        self.indicator.action("resize_window").await;
        self.window_geometry_op(received, &target, |window| async move {
            registry::resize_window(&window, params.width, params.height).await
        })
        .await
    }
}

#[tool_handler(
    router = self.mcp_tool_router(),
    name = "computer-use-linux",
    // NOTE: keep in lockstep with Cargo.toml + package.json on every release.
    // The rmcp tool_handler macro only accepts a string literal here, so this
    // can't be env!("CARGO_PKG_VERSION"); the MCP safety check (CI) fails the
    // build if it drifts from the Cargo version.
    version = "0.7.12",
    instructions = "Begin every turn that uses Computer Use by calling get_app_state. If diagnostics report disabled GNOME accessibility, call setup_accessibility before asking the user to retry. Use list_windows/focused_window before targeted keyboard input. If diagnostics report windowing.can_list_windows=false on GNOME, call setup_window_targeting to install the optional GNOME Shell extension backend, then ask the user to log out and back in if the setup report says a shell reload is required. This Linux backend can capture size-bounded screenshots through GNOME Shell or XDG Desktop Portal, read AT-SPI trees with action/value metadata, invoke native AT-SPI actions, set AT-SPI values or editable text, list/focus compositor windows through registered Linux window backends when the session permits it, attach best-effort terminal tty/process metadata to terminal windows, send coordinate or element-targeted click/scroll/drag input through the Wayland remote desktop portal when available, and send literal type_text through KDE clipboard integration on Plasma Wayland, wtype on compatible Wayland compositors, or portal keysyms on other Wayland sessions. Portal text startup or conversion failures return an error without replaying through ydotool. GNOME preflights the complete text against every configured keyboard layout group because the background reader cannot establish the active group; rejected text can use set_value on an editable field. Raw ydotool text is limited to printable ASCII, tab and newline, uses US physical key positions, and requires checking the resulting field contents under the active layout. Screenshot results include width/height for the returned image plus coordinate_width/coordinate_height and scale for desktop coordinate conversion; request more detail with max_width, max_height, max_bytes, format=jpeg, quality, or a smaller target/crop instead of relying on unbounded screenshots. Tools with readOnlyHint=false may mutate local desktop or application state; hosts should require approval for actions that can submit, delete, send, purchase, or overwrite data. For element-targeted actions, prefer element_index from the latest get_app_state result; click, perform_action, and set_value can also use semantic role/name/text/states selectors when the target is unique. type_text and press_key accept optional window_id, pid, app_id, wm_class, title, tty, terminal_pid, terminal_command, or terminal_cwd selectors and refuse targeted input if focus cannot be verified. After targeted keyboard input, results append focused-element feedback from AT-SPI (role, name, editable) and warn when no editable element holds focus. Treat that warning as the input not landing. Screenshot, click, and input results warn when the target window or coordinate is partially or fully off-screen; use move_window/resize_window (GNOME Shell extension backend) to bring a window fully on-screen before retrying. scroll accepts the same window targeting and relative coordinates as click. get_app_state returns a compact readiness block by default; pass verbose=true for the full diagnostics dump. Scope get_app_state with app_name_or_bundle_identifier or a window target (window_id, pid, app_id, wm_class, title); without one it returns the whole desktop AT-SPI tree, reports tree_scoped=false, and warns in message, which can flood context. accessibility_tree_truncated=true means the node, depth, or read budget stopped traversal with unread elements left; recover by scoping to a narrower app or window target and raising max_nodes or max_depth (hard caps 2000 and 64), not by lowering max_nodes. Electron apps expose no AT-SPI tree unless launched with --force-renderer-accessibility."
)]
impl ServerHandler for ComputerUseLinux {}

fn shell_execution_enabled() -> bool {
    shell_execution_enabled_value(env::var(SHELL_ENABLE_ENV).ok().as_deref())
}

#[derive(Debug, Serialize, JsonSchema)]
struct CompletionOutput {
    ok: bool,
    cue: String,
    note: String,
}

async fn send_completion_notification(program: &Path, limit: Duration) -> CompletionOutput {
    let mut command = TokioCommand::new(program);
    command.args([
        "--app-name=computer-use-linux",
        "--expire-time=3000",
        "Desktop interaction finished",
        "The agent has finished its desktop interaction.",
    ]);
    let sent =
        crate::command_runner::output_with_timeout(command, "send completion notification", limit)
            .await;
    match sent {
        Ok(output) if output.status.success() => CompletionOutput {
            ok: true,
            cue: "notification".into(),
            note: "Notification submitted; desktop settings control whether it is displayed."
                .into(),
        },
        _ => CompletionOutput {
            ok: true,
            cue: "skipped".into(),
            note:
                "Notification unavailable, failed, or timed out. The interaction is still complete."
                    .into(),
        },
    }
}

fn shell_execution_enabled_value(value: Option<&str>) -> bool {
    value == Some("1")
}

fn valid_environment_name(name: &str) -> bool {
    let mut chars = name.chars();
    chars
        .next()
        .is_some_and(|first| first == '_' || first.is_ascii_alphabetic())
        && chars.all(|character| character == '_' || character.is_ascii_alphanumeric())
}

fn inherited_shell_environment_allowed(name: &str) -> bool {
    matches!(
        name,
        "PATH"
            | "HOME"
            | "USER"
            | "LOGNAME"
            | "LANG"
            | "TERM"
            | "XDG_RUNTIME_DIR"
            | "DISPLAY"
            | "WAYLAND_DISPLAY"
            | "DBUS_SESSION_BUS_ADDRESS"
    ) || name.starts_with("LC_")
}

fn inherited_shell_environment(
    variables: impl IntoIterator<Item = (OsString, OsString)>,
) -> Vec<(String, String)> {
    variables
        .into_iter()
        .filter_map(|(name, value)| {
            let (Ok(name), Ok(value)) = (name.into_string(), value.into_string()) else {
                return None;
            };
            inherited_shell_environment_allowed(&name).then_some((name, value))
        })
        .collect()
}

fn shell_command_sha256(command: &str) -> String {
    Sha256::digest(command.as_bytes())
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn bounded_shell_stream(bytes: &[u8]) -> (String, bool) {
    let truncated = bytes.len() > SHELL_RESPONSE_STREAM_BYTES;
    let visible = if truncated {
        &bytes[..SHELL_RESPONSE_STREAM_BYTES]
    } else {
        bytes
    };
    (String::from_utf8_lossy(visible).into_owned(), truncated)
}

fn shell_error_output(
    command_sha256: String,
    cwd: String,
    timeout_seconds: u64,
    error: impl Into<String>,
) -> RunShellOutput {
    RunShellOutput {
        ok: false,
        command_sha256,
        cwd,
        timeout_seconds,
        exit_code: None,
        stdout: String::new(),
        stderr: String::new(),
        stdout_truncated: false,
        stderr_truncated: false,
        error: Some(error.into()),
    }
}

async fn execute_shell(params: RunShellParams) -> RunShellOutput {
    let command_sha256 = shell_command_sha256(&params.command);
    let timeout_seconds = params.timeout_seconds.unwrap_or(SHELL_DEFAULT_TIMEOUT_SECS);
    let requested_cwd = params.cwd.as_deref().unwrap_or(".").to_string();

    if !shell_execution_enabled() {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!(
                "shell execution is disabled; restart the MCP server with {SHELL_ENABLE_ENV}=1 to opt in"
            ),
        );
    }
    if params.command.trim().is_empty() {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            "command must not be empty",
        );
    }
    if params.command.len() > SHELL_MAX_COMMAND_BYTES {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!("command exceeds the {SHELL_MAX_COMMAND_BYTES}-byte limit"),
        );
    }
    if requested_cwd.len() > SHELL_MAX_CWD_BYTES {
        return shell_error_output(
            command_sha256,
            "<rejected: cwd too long>".to_string(),
            timeout_seconds,
            format!("cwd exceeds the {SHELL_MAX_CWD_BYTES}-byte limit"),
        );
    }
    if !(1..=SHELL_MAX_TIMEOUT_SECS).contains(&timeout_seconds) {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!("timeout_seconds must be between 1 and {SHELL_MAX_TIMEOUT_SECS}"),
        );
    }
    if params.env.len() > SHELL_MAX_ENV_ENTRIES {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!("env contains more than {SHELL_MAX_ENV_ENTRIES} entries"),
        );
    }
    let env_bytes = params
        .env
        .iter()
        .map(|(name, value)| name.len().saturating_add(value.len()))
        .sum::<usize>();
    if env_bytes > SHELL_MAX_ENV_BYTES {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!("env exceeds the {SHELL_MAX_ENV_BYTES}-byte limit"),
        );
    }
    if let Some(invalid) = params.env.keys().find(|name| !valid_environment_name(name)) {
        return shell_error_output(
            command_sha256,
            requested_cwd,
            timeout_seconds,
            format!("invalid environment variable name: {invalid}"),
        );
    }

    let cwd = match std::fs::canonicalize(&requested_cwd) {
        Ok(path) if path.is_dir() => path,
        Ok(_) => {
            return shell_error_output(
                command_sha256,
                requested_cwd,
                timeout_seconds,
                "cwd is not a directory",
            )
        }
        Err(error) => {
            return shell_error_output(
                command_sha256,
                requested_cwd,
                timeout_seconds,
                format!("failed to resolve cwd: {error}"),
            )
        }
    };
    let cwd_display = cwd.display().to_string();

    let mut child = TokioCommand::new("/bin/sh");
    // Do not use a login shell: profile scripts could reintroduce credentials
    // after env_clear() and contaminate or prevent the requested command.
    child.args(["-c", &params.command]);
    child.current_dir(&cwd);
    child.env_clear();
    let mut inherited_path = false;
    for (name, value) in inherited_shell_environment(env::vars_os()) {
        inherited_path |= name == "PATH";
        child.env(name, value);
    }
    if !inherited_path {
        child.env("PATH", "/usr/local/bin:/usr/bin:/bin");
    }
    child.envs(&params.env);

    eprintln!(
        "[computer-use-linux] run_shell start sha256={command_sha256} cwd={cwd_display:?} timeout_seconds={timeout_seconds}"
    );
    match crate::command_runner::output_with_timeout(
        child,
        "run approved shell command",
        Duration::from_secs(timeout_seconds),
    )
    .await
    {
        Ok(output) => {
            let exit_code = output.status.code();
            let (stdout, stdout_truncated) = bounded_shell_stream(&output.stdout);
            let (stderr, stderr_truncated) = bounded_shell_stream(&output.stderr);
            eprintln!(
                "[computer-use-linux] run_shell finish sha256={command_sha256} exit_code={exit_code:?} stdout_bytes={} stderr_bytes={} stdout_truncated={stdout_truncated} stderr_truncated={stderr_truncated}",
                output.stdout.len(),
                output.stderr.len()
            );
            RunShellOutput {
                ok: output.status.success(),
                command_sha256,
                cwd: cwd_display,
                timeout_seconds,
                exit_code,
                stdout,
                stderr,
                stdout_truncated,
                stderr_truncated,
                error: None,
            }
        }
        Err(error) => {
            let error = format!("{error:#}");
            eprintln!(
                "[computer-use-linux] run_shell error sha256={command_sha256} error={error:?}"
            );
            shell_error_output(command_sha256, cwd_display, timeout_seconds, error)
        }
    }
}

pub async fn serve_mcp() -> Result<()> {
    let server = ComputerUseLinux::default();
    let indicator = Arc::clone(&server.indicator);
    let running = server.serve(rmcp::transport::stdio()).await?;
    // The handshake is done, so the peer knows the client's name.
    indicator.attach(running.peer().clone());
    running.waiting().await?;
    Ok(())
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct ListAppsOutput {
    apps: Vec<AppCandidate>,
    accessible_apps: Vec<AccessibleAppSummary>,
    accessibility_error: Option<String>,
    note: String,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct ListWindowsOutput {
    backend: String,
    windows: Vec<WindowInfo>,
    error: Option<String>,
    permissions_hint: Option<String>,
    note: String,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct FocusedWindowOutput {
    backend: String,
    focused_window: Option<WindowInfo>,
    error: Option<String>,
    permissions_hint: Option<String>,
    message: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct ActivateWindowParams {
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    tty: Option<String>,
    #[serde(default)]
    terminal_pid: Option<u32>,
    #[serde(default)]
    terminal_command: Option<String>,
    #[serde(default)]
    terminal_cwd: Option<String>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    title: Option<String>,
}

impl ActivateWindowParams {
    fn into_target(self) -> WindowTarget {
        WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: self.tty,
            terminal_pid: self.terminal_pid,
            terminal_command: self.terminal_command,
            terminal_cwd: self.terminal_cwd,
            app_id: self.app_id,
            wm_class: self.wm_class,
            title: self.title,
        }
    }
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct ActivateWindowOutput {
    ok: bool,
    implemented: bool,
    backend: String,
    focus: Option<WindowFocusResult>,
    error: Option<String>,
    permissions_hint: Option<String>,
    // Echo of the request for debugging. `serde_json::Value` has no fixed JSON
    // schema, which strict MCP clients (Claude Code) reject in `outputSchema` —
    // and one invalid tool fails the whole tool list. Keep it in the runtime
    // response (serde) but omit it from the generated schema.
    #[schemars(skip)]
    received: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct MoveWindowParams {
    #[serde(flatten)]
    target: ActivateWindowParams,
    /// New frame-left in desktop coordinates.
    x: i32,
    /// New frame-top in desktop coordinates.
    y: i32,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct ResizeWindowParams {
    #[serde(flatten)]
    target: ActivateWindowParams,
    /// New frame width in desktop pixels.
    width: i32,
    /// New frame height in desktop pixels.
    height: i32,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct WindowGeometryOutput {
    ok: bool,
    implemented: bool,
    backend: String,
    /// Post-operation window info (compositor-final geometry).
    window: Option<WindowInfo>,
    message: String,
    permissions_hint: Option<String>,
    #[schemars(skip)]
    received: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct RunShellParams {
    /// Shell program text passed to `/bin/sh -c` without loading login profiles.
    command: String,
    /// Existing directory in which to start the shell. Symlinks are resolved.
    #[serde(default)]
    cwd: Option<String>,
    /// Additional environment entries. The ambient process environment is not inherited wholesale.
    #[serde(default)]
    env: BTreeMap<String, String>,
    /// Wall-clock timeout in seconds (default 30, maximum 120).
    #[serde(default)]
    timeout_seconds: Option<u64>,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct RunShellOutput {
    ok: bool,
    command_sha256: String,
    cwd: String,
    timeout_seconds: u64,
    exit_code: Option<i32>,
    stdout: String,
    stderr: String,
    stdout_truncated: bool,
    stderr_truncated: bool,
    error: Option<String>,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct AppCandidate {
    name: String,
    pid: u32,
    command: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct GetAppStateParams {
    /// App name or AT-SPI id that limits the accessibility tree. Omit only when
    /// you need the whole desktop tree; unscoped results can flood context.
    #[serde(default)]
    app_name_or_bundle_identifier: Option<String>,
    /// Compositor window id. Also scopes the accessibility tree to that window's
    /// application when possible.
    #[serde(default)]
    window_id: Option<u64>,
    /// Process id. Also scopes the accessibility tree to that process when it
    /// exposes AT-SPI.
    #[serde(default)]
    pid: Option<u32>,
    /// Terminal tty device (for example /dev/pts/3). Resolves a window target
    /// and scopes the tree when possible.
    #[serde(default)]
    tty: Option<String>,
    /// Terminal emulator pid. Resolves a window target and scopes the tree when
    /// possible.
    #[serde(default)]
    terminal_pid: Option<u32>,
    /// Terminal command substring. Resolves a window target and scopes the tree
    /// when possible.
    #[serde(default)]
    terminal_command: Option<String>,
    /// Terminal working directory. Resolves a window target and scopes the tree
    /// when possible.
    #[serde(default)]
    terminal_cwd: Option<String>,
    /// Application id. Also scopes the accessibility tree when it matches an
    /// AT-SPI root.
    #[serde(default)]
    app_id: Option<String>,
    /// Window manager class. Also scopes the accessibility tree when it matches
    /// an AT-SPI root.
    #[serde(default)]
    wm_class: Option<String>,
    /// Window title substring. Also scopes the accessibility tree when it
    /// matches an AT-SPI root.
    #[serde(default)]
    title: Option<String>,
    /// Maximum raw AT-SPI nodes to inspect before compaction (default 1000, hard max 2000).
    #[serde(default)]
    max_nodes: Option<usize>,
    /// Maximum AT-SPI traversal depth (default 32, hard max 64).
    #[serde(default)]
    max_depth: Option<u32>,
    /// Include a size-bounded screenshot (default true). Set false when the
    /// accessibility tree is enough.
    #[serde(default)]
    include_screenshot: Option<bool>,
    /// Maximum returned screenshot width in pixels (default 1920, hard-capped).
    #[serde(default)]
    max_width: Option<u32>,
    /// Maximum returned screenshot height in pixels (default 1920, hard-capped).
    #[serde(default)]
    max_height: Option<u32>,
    /// Maximum returned screenshot image bytes before base64 (default 2 MiB, hard-capped).
    #[serde(default)]
    max_bytes: Option<usize>,
    /// Additional downscale factor from 0.0 to 1.0, applied before max dimensions.
    #[serde(default)]
    scale: Option<f32>,
    /// Output image format (default png). Use jpeg with quality to trade exact pixels for smaller payloads.
    #[serde(default)]
    format: Option<ScreenshotOutputFormat>,
    /// JPEG quality from 1 to 95 (default 80). Ignored for png.
    #[serde(default)]
    #[schemars(range(min = 1, max = 95))]
    quality: Option<u8>,
    /// Include the full diagnostics report (large). Default false: only the
    /// compact readiness block is returned.
    #[serde(default)]
    verbose: Option<bool>,
}

impl GetAppStateParams {
    fn window_target(&self) -> WindowTarget {
        WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: self.tty.clone(),
            terminal_pid: self.terminal_pid,
            terminal_command: self.terminal_command.clone(),
            terminal_cwd: self.terminal_cwd.clone(),
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.title.clone(),
        }
    }

    fn screenshot_options(&self) -> ScreenshotPayloadOptions {
        ScreenshotPayloadOptions {
            max_width: self.max_width,
            max_height: self.max_height,
            max_bytes: self.max_bytes,
            scale: self.scale,
            format: self.format,
            quality: self.quality,
        }
    }
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
struct ScreenshotParams {
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    title: Option<String>,
    /// Raise the targeted window before capture (default true). Ignored without
    /// a window target.
    #[serde(default)]
    raise_window: Option<bool>,
    /// Capture the whole desktop even when a window is targeted (default false).
    #[serde(default)]
    full_screen: Option<bool>,
    /// Maximum returned screenshot width in pixels (default 1920, hard-capped).
    #[serde(default)]
    max_width: Option<u32>,
    /// Maximum returned screenshot height in pixels (default 1920, hard-capped).
    #[serde(default)]
    max_height: Option<u32>,
    /// Maximum returned screenshot image bytes before base64 (default 2 MiB, hard-capped).
    #[serde(default)]
    max_bytes: Option<usize>,
    /// Additional downscale factor from 0.0 to 1.0, applied before max dimensions.
    #[serde(default)]
    scale: Option<f32>,
    /// Output image format (default png). Use jpeg with quality to trade exact pixels for smaller payloads.
    #[serde(default)]
    format: Option<ScreenshotOutputFormat>,
    /// JPEG quality from 1 to 95 (default 80). Ignored for png.
    #[serde(default)]
    #[schemars(range(min = 1, max = 95))]
    quality: Option<u8>,
}

impl ScreenshotParams {
    fn window_target(&self) -> Option<WindowTarget> {
        if self.window_id.is_none()
            && self.pid.is_none()
            && self.app_id.is_none()
            && self.wm_class.is_none()
            && self.title.is_none()
        {
            return None;
        }
        Some(WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: None,
            terminal_pid: None,
            terminal_command: None,
            terminal_cwd: None,
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.title.clone(),
        })
    }

    fn screenshot_options(&self) -> ScreenshotPayloadOptions {
        ScreenshotPayloadOptions {
            max_width: self.max_width,
            max_height: self.max_height,
            max_bytes: self.max_bytes,
            scale: self.scale,
            format: self.format,
            quality: self.quality,
        }
    }
}

/// Screenshot metadata for `get_app_state`. The image bytes travel as a
/// separate `image` content block (issue #145), not inside this JSON.
#[derive(Debug, Clone, Serialize, JsonSchema)]
struct ScreenshotSummary {
    mime_type: String,
    source: String,
    /// Width of the returned image payload.
    width: u32,
    /// Height of the returned image payload.
    height: u32,
    /// Coordinate-space width before payload downscaling.
    coordinate_width: u32,
    /// Coordinate-space height before payload downscaling.
    coordinate_height: u32,
    /// Returned pixels per coordinate-space pixel.
    scale: f32,
    resized: bool,
    bytes: usize,
    original_bytes: usize,
    max_bytes: usize,
    format: ScreenshotOutputFormat,
    quality: Option<u8>,
}

impl From<&ScreenshotCapture> for ScreenshotSummary {
    fn from(capture: &ScreenshotCapture) -> Self {
        Self {
            mime_type: capture.mime_type.clone(),
            source: capture.source.clone(),
            width: capture.width,
            height: capture.height,
            coordinate_width: capture.coordinate_width,
            coordinate_height: capture.coordinate_height,
            scale: capture.scale,
            resized: capture.resized,
            bytes: capture.bytes,
            original_bytes: capture.original_bytes,
            max_bytes: capture.max_bytes,
            format: capture.format,
            quality: capture.quality,
        }
    }
}

/// Assemble the `get_app_state` result: the screenshot (when present) as a
/// structured image block first, then the JSON report as text and as
/// `structured_content`. Inlining the base64 in the JSON made hosts treat a
/// 100KB+ image as plain text tokens (issue #145).
fn app_state_result(
    output: GetAppStateOutput,
    screenshot: Option<ScreenshotCapture>,
) -> Result<CallToolResult, ErrorData> {
    let value = serde_json::to_value(&output).map_err(|error| {
        ErrorData::internal_error(
            format!("failed to serialize get_app_state output: {error}"),
            None,
        )
    })?;
    let mut result = CallToolResult::structured(value);
    if let Some(capture) = screenshot {
        result.content.insert(
            0,
            ContentBlock::image(data_url_payload(&capture.data_url), capture.mime_type),
        );
    }
    Ok(result)
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct GetAppStateOutput {
    app_name_or_bundle_identifier: Option<String>,
    window_context: Option<WindowInfo>,
    window_error: Option<String>,
    window_permissions_hint: Option<String>,
    backend: String,
    /// Screenshot metadata; the image itself is the first `image` content block.
    screenshot: Option<ScreenshotSummary>,
    screenshot_error: Option<String>,
    accessibility_tree: Vec<AccessibilityNode>,
    accessibility_tree_raw_count: usize,
    /// True when the snapshot was limited to selected app roots rather than the
    /// full desktop AT-SPI registry.
    tree_scoped: bool,
    /// True when max_nodes, max_depth, or the child read budget stopped
    /// traversal with unread elements left, so the tree is incomplete. Recover
    /// by scoping to a narrower app or window target and raising max_nodes or
    /// max_depth (hard caps 2000 and 64). Failed element reads do not count.
    accessibility_tree_truncated: bool,
    accessibility_error: Option<String>,
    /// Compact readiness summary (always present).
    readiness: crate::diagnostics::ReadinessReport,
    /// Full diagnostics; populated only when verbose=true.
    #[serde(skip_serializing_if = "Option::is_none")]
    diagnostics: Option<DoctorReport>,
    message: String,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
struct ClickParams {
    #[serde(default)]
    element_index: Option<u32>,
    #[serde(default)]
    role: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    text: Option<String>,
    #[serde(default)]
    states: Vec<String>,
    #[serde(default)]
    x: Option<i32>,
    #[serde(default)]
    y: Option<i32>,
    #[serde(default)]
    button: Option<String>,
    #[serde(default)]
    click_count: Option<u32>,
    // Optional window target: when set, the window is raised/focused before the
    // click so a coordinate click reliably lands on the intended app rather than
    // whatever window happens to be stacked on top at that pixel.
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    window_title: Option<String>,
    /// Interpret `x`/`y` from the clipped window screenshot crop origin, in
    /// coordinate pixels before preview resizing. Divide preview pixels by its
    /// scale first. This is not a toolkit widget or raw GDK surface origin.
    /// Requires a window target; missing targets are rejected.
    #[serde(default)]
    relative: Option<bool>,
}

impl ClickParams {
    /// A window target if any window-identifying field was supplied.
    fn window_target(&self) -> Option<WindowTarget> {
        if self.window_id.is_none()
            && self.pid.is_none()
            && self.app_id.is_none()
            && self.wm_class.is_none()
            && self.window_title.is_none()
        {
            return None;
        }
        Some(WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: None,
            terminal_pid: None,
            terminal_command: None,
            terminal_cwd: None,
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.window_title.clone(),
        })
    }

    fn selector(&self) -> ElementSelector<'_> {
        ElementSelector {
            role: self.role.as_deref(),
            name: self.name.as_deref(),
            text: self.text.as_deref(),
            states: &self.states,
        }
    }
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
struct ActionParams {
    #[serde(default)]
    element_index: Option<u32>,
    #[serde(default)]
    element_identifier: Option<String>,
    #[serde(default)]
    role: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    text: Option<String>,
    #[serde(default)]
    states: Vec<String>,
    #[serde(default)]
    action: Option<String>,
}

impl ActionParams {
    fn selector(&self) -> ElementSelector<'_> {
        ElementSelector {
            role: self.role.as_deref(),
            name: self.name.as_deref(),
            text: self.text.as_deref(),
            states: &self.states,
        }
    }
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
struct SetValueParams {
    #[serde(default)]
    element_index: Option<u32>,
    #[serde(default)]
    element_identifier: Option<String>,
    #[serde(default)]
    role: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    text: Option<String>,
    #[serde(default)]
    states: Vec<String>,
    value: String,
}

impl SetValueParams {
    fn selector(&self) -> ElementSelector<'_> {
        ElementSelector {
            role: self.role.as_deref(),
            name: self.name.as_deref(),
            text: self.text.as_deref(),
            states: &self.states,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct ScrollParams {
    #[serde(default)]
    element_index: Option<u32>,
    #[serde(default)]
    x: Option<i32>,
    #[serde(default)]
    y: Option<i32>,
    direction: String,
    #[serde(default)]
    #[schemars(range(min = -100, max = 100))]
    pages: Option<f64>,
    // Optional window target (parity with click): the window is raised/focused
    // before scrolling so the wheel events land on the intended app.
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    window_title: Option<String>,
    /// Interpret `x`/`y` from the clipped window screenshot crop origin, in
    /// coordinate pixels before preview resizing. Divide preview pixels by its
    /// scale first. This is not a toolkit widget or raw GDK surface origin.
    /// Requires a window target; missing targets are rejected.
    #[serde(default)]
    relative: Option<bool>,
}

fn scroll_units(pages: Option<f64>) -> std::result::Result<i32, String> {
    let pages = pages.unwrap_or(1.0);
    if !pages.is_finite() || pages.abs() > MAX_SCROLL_PAGES {
        return Err(format!(
            "pages must be finite and have an absolute value no greater than {MAX_SCROLL_PAGES}"
        ));
    }
    Ok(((pages.abs().max(0.1) * 5.0).round() as i32).max(1))
}

impl ScrollParams {
    /// A window target if any window-identifying field was supplied.
    fn window_target(&self) -> Option<WindowTarget> {
        if self.window_id.is_none()
            && self.pid.is_none()
            && self.app_id.is_none()
            && self.wm_class.is_none()
            && self.window_title.is_none()
        {
            return None;
        }
        Some(WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: None,
            terminal_pid: None,
            terminal_command: None,
            terminal_cwd: None,
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.window_title.clone(),
        })
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct DragParams {
    start_x: i32,
    start_y: i32,
    end_x: i32,
    end_y: i32,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct PressKeyParams {
    key: String,
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    tty: Option<String>,
    #[serde(default)]
    terminal_pid: Option<u32>,
    #[serde(default)]
    terminal_command: Option<String>,
    #[serde(default)]
    terminal_cwd: Option<String>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    title: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
struct TypeTextParams {
    text: String,
    #[serde(default)]
    window_id: Option<u64>,
    #[serde(default)]
    pid: Option<u32>,
    #[serde(default)]
    tty: Option<String>,
    #[serde(default)]
    terminal_pid: Option<u32>,
    #[serde(default)]
    terminal_command: Option<String>,
    #[serde(default)]
    terminal_cwd: Option<String>,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    wm_class: Option<String>,
    #[serde(default)]
    title: Option<String>,
}

impl PressKeyParams {
    fn window_target(&self) -> WindowTarget {
        WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: self.tty.clone(),
            terminal_pid: self.terminal_pid,
            terminal_command: self.terminal_command.clone(),
            terminal_cwd: self.terminal_cwd.clone(),
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.title.clone(),
        }
    }
}

impl TypeTextParams {
    fn window_target(&self) -> WindowTarget {
        WindowTarget {
            window_id: self.window_id,
            pid: self.pid,
            tty: self.tty.clone(),
            terminal_pid: self.terminal_pid,
            terminal_command: self.terminal_command.clone(),
            terminal_cwd: self.terminal_cwd.clone(),
            app_id: self.app_id.clone(),
            wm_class: self.wm_class.clone(),
            title: self.title.clone(),
        }
    }
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
struct ActionOutput {
    ok: bool,
    implemented: bool,
    action: String,
    message: String,
    // See ActivateWindowOutput: kept in the response, omitted from the schema
    // because `serde_json::Value` produces a non-object schema strict MCP
    // clients reject.
    #[schemars(skip)]
    received: Option<serde_json::Value>,
}

impl ComputerUseLinux {
    fn is_wayland_session(&self) -> bool {
        crate::diagnostics::hydrate_session_bus_env();
        let session_type = env::var("XDG_SESSION_TYPE").ok();
        let wayland_display = env::var("WAYLAND_DISPLAY").ok();
        session_is_wayland(session_type.as_deref(), wayland_display.as_deref())
    }

    // The Wayland remote-desktop portal is a fallback for input: when a
    // compatible ydotool CLI and working `ydotoold` socket are present we
    // prefer ydotool, because it injects input without a permission prompt.
    // Without `COMPUTER_USE_LINUX_PERSIST_REMOTE_DESKTOP=1` the portal asks
    // again on every new process. That opt-in sends `persist_mode=2` on
    // RemoteDesktop SelectDevices and reuses the single-use restore token.
    // `COMPUTER_USE_LINUX_FORCE_YDOTOOL_*=1` always uses ydotool;
    // `COMPUTER_USE_LINUX_FORCE_PORTAL_*=1` always uses the portal.
    async fn should_prefer_portal_pointer_backend(&self) -> bool {
        if env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER") {
            return false;
        }
        if env_flag_enabled("COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER") {
            return self.is_wayland_session();
        }
        should_prefer_portal_backend_by_default(
            self.is_wayland_session(),
            ydotool_backend_available().await,
        )
    }

    // Literal text needs a layout-aware route even when raw ydotool input is
    // available. KDE uses clipboard paste; compatible compositors use wtype.
    fn should_prefer_portal_text_backend(&self) -> bool {
        prefer_portal_text_backend(
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD"),
            self.should_prefer_xdotool_keyboard(),
            self.is_wayland_session(),
            self.is_kde_wayland_session(),
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_PORTAL_KEYBOARD"),
            self.should_prefer_wtype_keyboard(),
        )
    }

    /// Portal keyboard policy for `press_key` chords. Unlike literal text
    /// (where KDE prefers the clipboard paste backend), key chords have no
    /// clipboard route, so the portal keyboard session is preferred on ANY
    /// Wayland session — including Plasma. An already-active keyboard
    /// session (e.g. established by a KDE clipboard paste) is reused even
    /// when ydotool is available, so the consent the user already granted
    /// keeps covering key chords; otherwise the portal is preferred only
    /// when ydotool is absent or the portal is forced.
    async fn should_prefer_portal_keyboard_for_chords(&self) -> bool {
        if env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD") {
            return false;
        }
        if self.should_prefer_xdotool_keyboard() {
            return false;
        }
        if !self.is_wayland_session() {
            return false;
        }
        if self.cached_portal_keyboard_session().is_some()
            || env_flag_enabled("COMPUTER_USE_LINUX_FORCE_PORTAL_KEYBOARD")
        {
            return true;
        }
        !ydotool_backend_available().await
    }

    fn should_prefer_kde_clipboard_text_backend(&self) -> bool {
        !env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD")
            && !self.should_prefer_xdotool_keyboard()
            && self.is_kde_wayland_session()
    }

    /// Keyboard policy for X11 sessions: prefer `xdotool` (XTEST).
    ///
    /// ydotool writes raw evdev scancodes to a virtual uinput device. On X11
    /// the server then re-interprets them through the active XKB layout, so
    /// `press_key "Return"` and chords like `ctrl+a` land as stray characters,
    /// and literal text can mangle symbols/digits (issue #58). XTEST resolves
    /// keysyms against the live layout instead.
    ///
    /// `COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD=1` opts out;
    /// `COMPUTER_USE_LINUX_FORCE_XDOTOOL_KEYBOARD=1` forces it on.
    fn should_prefer_xdotool_keyboard(&self) -> bool {
        prefer_xdotool_keyboard(
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD"),
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_XDOTOOL_KEYBOARD"),
            self.is_wayland_session(),
            env_var_non_empty("DISPLAY"),
            xdotool_available(),
        )
    }

    fn should_prefer_wtype_keyboard(&self) -> bool {
        prefer_wtype_keyboard(
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD"),
            self.is_wayland_session(),
            crate::diagnostics::wtype_compatible_wayland_desktop(
                env::var("XDG_CURRENT_DESKTOP").ok().as_deref(),
            ),
            wtype_available(),
        )
    }

    fn should_prefer_xdotool_pointer(&self) -> bool {
        crate::diagnostics::hydrate_session_bus_env();
        prefer_xdotool_pointer(
            env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER"),
            env::var("XDG_SESSION_TYPE").ok().as_deref(),
            env_var_non_empty("DISPLAY"),
            env::var("WAYLAND_DISPLAY").ok().as_deref(),
            xdotool_available(),
        )
    }

    fn is_gnome_wayland_session(&self) -> bool {
        self.is_wayland_session()
            && (env_contains("XDG_CURRENT_DESKTOP", "gnome")
                || env_contains("DESKTOP_SESSION", "gnome")
                || env_contains("XDG_SESSION_DESKTOP", "gnome"))
    }

    fn is_kde_wayland_session(&self) -> bool {
        self.is_wayland_session()
            && (env_contains("XDG_CURRENT_DESKTOP", "kde")
                || env_contains("DESKTOP_SESSION", "plasma"))
    }

    fn cached_portal_pointer_session(&self) -> Option<PortalPointerSession> {
        if env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER") {
            return None;
        }
        let mut cached = self.portal_pointer_session.lock().ok()?;
        if cached.as_ref().is_some_and(|session| !session.is_valid()) {
            *cached = None;
        }
        cached.clone()
    }

    fn clear_portal_pointer_session(&self, failed: &PortalPointerSession) {
        failed.invalidate_and_close();
        if let Ok(mut cached) = self.portal_pointer_session.lock() {
            if cached
                .as_ref()
                .is_some_and(|session| session.same_session(failed))
            {
                *cached = None;
            }
        }
    }

    fn cached_portal_keyboard_session(&self) -> Option<PortalKeyboardSession> {
        let mut cached = self.portal_keyboard_session.lock().ok()?;
        if cached.as_ref().is_some_and(|session| !session.is_valid()) {
            *cached = None;
        }
        cached.clone()
    }

    fn clear_portal_keyboard_session(&self, failed: &PortalKeyboardSession) {
        failed.invalidate_and_close();
        if let Ok(mut cached) = self.portal_keyboard_session.lock() {
            if cached
                .as_ref()
                .is_some_and(|session| session.same_session(failed))
            {
                *cached = None;
            }
        }
    }

    async fn ensure_portal_pointer_session(&self) -> Result<Option<PortalPointerSession>> {
        if !self.should_prefer_portal_pointer_backend().await {
            return Ok(None);
        }
        if let Some(session) = self.cached_portal_pointer_session() {
            return Ok(Some(session));
        }

        let _guard = self.portal_session_init_lock.lock().await;
        if let Some(session) = self.cached_portal_pointer_session() {
            return Ok(Some(session));
        }

        let session = start_portal_pointer_session().await?;
        if let Ok(mut cached) = self.portal_pointer_session.lock() {
            *cached = Some(session.clone());
        }
        Ok(Some(session))
    }

    async fn ensure_portal_keyboard_session(&self) -> Result<Option<PortalKeyboardSession>> {
        if env_flag_enabled("COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD")
            || !self.is_wayland_session()
        {
            return Ok(None);
        }
        if let Some(session) = self.cached_portal_keyboard_session() {
            return Ok(Some(session));
        }

        let _guard = self.portal_session_init_lock.lock().await;
        if let Some(session) = self.cached_portal_keyboard_session() {
            return Ok(Some(session));
        }

        let session = start_portal_keyboard_session().await?;
        if let Ok(mut cached) = self.portal_keyboard_session.lock() {
            *cached = Some(session.clone());
        }
        Ok(Some(session))
    }

    async fn resolve_window_context(
        &self,
        params: &GetAppStateParams,
    ) -> (Option<WindowInfo>, Option<String>, Option<String>) {
        let target = params.window_target();
        if !target.has_target() {
            return (None, None, None);
        }

        match list_windows().await {
            Ok(windows) => match resolve_window_target(&windows, &target) {
                Ok(window) => (Some(window.clone()), None, None),
                Err(error) => (None, Some(format!("{error:#}")), None),
            },
            Err(error) => {
                let error = format!("{error:#}");
                let hint = window_permission_hint(&error);
                (None, Some(error), hint)
            }
        }
    }

    async fn resolve_screenshot_window(
        &self,
        target: &WindowTarget,
        raise_window: bool,
    ) -> Result<WindowInfo> {
        let window = if raise_window {
            let focus = focus_window_target(target).await?;
            if !focus.exact_window_focused {
                anyhow::bail!(
                    "the requested window could not be focused exactly; refusing to capture unrelated desktop pixels"
                );
            }
            sleep(Duration::from_millis(250)).await;
            focus
                .focused_window
                .filter(|window| window.window_id == focus.requested_window.window_id)
                .ok_or_else(|| anyhow::anyhow!("focused-window verification returned no window"))?
        } else {
            let windows = list_windows().await?;
            let window = resolve_window_target(&windows, target)?.clone();
            if !window.focused {
                anyhow::bail!(
                    "raise_window=false requires the requested window to already be focused"
                );
            }
            window
        };
        if window.hidden {
            anyhow::bail!("the requested window is hidden or minimized");
        }
        Ok(window)
    }

    async fn window_crop_rect_for_capture(
        &self,
        window: &WindowInfo,
        raw: &RawScreenshotCapture,
    ) -> Result<(i32, i32, u32, u32)> {
        self.window_crop_rect_for_dimensions(window, raw.width, raw.height)
            .await
    }

    async fn window_crop_rect_for_dimensions(
        &self,
        window: &WindowInfo,
        capture_width: u32,
        capture_height: u32,
    ) -> Result<(i32, i32, u32, u32)> {
        Ok(self
            .window_coordinate_map_for_dimensions(window, capture_width, capture_height)
            .await?
            .capture_rect)
    }

    async fn window_coordinate_map_for_dimensions(
        &self,
        window: &WindowInfo,
        capture_width: u32,
        capture_height: u32,
    ) -> Result<WindowCoordinateMap> {
        let bounds = window.bounds.as_ref().ok_or_else(|| {
            anyhow::anyhow!(
                "targeted screenshot requires window bounds; refusing to return the full desktop"
            )
        })?;
        let logical_rect = window_crop_rect(bounds).ok_or_else(|| {
            anyhow::anyhow!(
                "targeted screenshot has unusable window bounds; refusing to return the full desktop"
            )
        })?;
        let monitors = if window.backend == GNOME_SHELL_EXTENSION_BACKEND {
            Some(
                crate::windowing::backends::gnome::extension_monitor_layout()
                    .await
                    .map_err(|error| {
                        anyhow::anyhow!(
                            "GNOME targeted screenshot requires logical monitor geometry: {error:#}"
                        )
                    })?
                    .into_iter()
                    .map(|monitor| (monitor.x, monitor.y, monitor.width, monitor.height))
                    .collect(),
            )
        } else if window.backend == GNOME_SHELL_INTROSPECT_BACKEND {
            crate::windowing::backends::gnome::extension_monitor_layout()
                .await
                .ok()
                .map(|monitors| {
                    monitors
                        .into_iter()
                        .map(|monitor| (monitor.x, monitor.y, monitor.width, monitor.height))
                        .collect()
                })
        } else if window.backend == KWIN_BACKEND {
            Some(vec![
                crate::windowing::backends::kwin::logical_desktop_rect()
                    .await
                    .map_err(|error| {
                        anyhow::anyhow!(
                            "KWin targeted screenshot requires logical workspace geometry: {error:#}"
                        )
                    })?,
            ])
        } else {
            None
        };
        let (full_capture_rect, portal_rect) = match monitors {
            Some(monitors) => (
                logical_window_crop_rect(bounds, &monitors, capture_width, capture_height)?,
                Some(logical_rect),
            ),
            None => (logical_rect, None),
        };
        Ok(WindowCoordinateMap {
            capture_rect: clip_capture_rect(full_capture_rect, capture_width, capture_height)?,
            full_capture_rect,
            portal_rect,
        })
    }

    async fn focused_window_coordinate_map(
        &self,
        focus: &WindowFocusResult,
    ) -> std::result::Result<WindowCoordinateMap, String> {
        let window = focus
            .focused_window
            .as_ref()
            .unwrap_or(&focus.requested_window);
        if !matches!(
            window.backend.as_str(),
            GNOME_SHELL_EXTENSION_BACKEND | GNOME_SHELL_INTROSPECT_BACKEND | KWIN_BACKEND
        ) {
            let full_capture_rect = window
                .bounds
                .as_ref()
                .and_then(window_crop_rect)
                .ok_or_else(|| {
                    "Window-relative coordinates require usable target-window bounds.".to_string()
                })?;
            let capture_rect = self
                .desktop_size
                .lock()
                .ok()
                .and_then(|guard| *guard)
                .map(|(width, height)| {
                    clip_capture_rect(full_capture_rect, width, height).map_err(|error| {
                        format!("Could not map target-window coordinates: {error:#}")
                    })
                })
                .transpose()?
                .unwrap_or(full_capture_rect);
            return Ok(WindowCoordinateMap {
                capture_rect,
                full_capture_rect,
                portal_rect: None,
            });
        }
        let (_, _, width, height) = self.capture_space_rect().await.ok_or_else(|| {
            "Could not determine screenshot dimensions for window-relative coordinates.".to_string()
        })?;
        self.window_coordinate_map_for_dimensions(window, width as u32, height as u32)
            .await
            .map_err(|error| format!("Could not map target-window coordinates: {error:#}"))
    }

    async fn resolve_accessibility_app_filter(
        &self,
        params: &GetAppStateParams,
        window_context: Option<&WindowInfo>,
    ) -> Option<String> {
        if let Some(explicit) = trimmed_nonempty(params.app_name_or_bundle_identifier.as_deref()) {
            return Some(explicit.to_string());
        }

        let target_pid = window_context.and_then(|window| window.pid).or(params.pid);
        let candidates = accessibility_filter_candidates(window_context);

        if let Some(target_pid) = target_pid {
            if let Ok(apps) = list_accessible_apps(200).await {
                if let Some(object_ref) =
                    select_accessibility_object_ref(&apps, target_pid, &candidates)
                {
                    return Some(object_ref);
                }
            }
        }

        candidates.into_iter().next()
    }

    async fn focus_target_for_input(
        &self,
        target: &WindowTarget,
    ) -> std::result::Result<Option<WindowFocusResult>, String> {
        if !target.has_target() {
            return Ok(None);
        }

        let focus = focus_window_target(target).await.map_err(|error| {
            let error = format!("{error:#}");
            if let Some(hint) = window_permission_hint(&error) {
                format!("Did not send input because the target window could not be focused: {error}. {hint}")
            } else {
                format!("Did not send input because the target window could not be focused: {error}")
            }
        })?;

        if focus_satisfies_target(&focus, target) {
            Ok(Some(focus))
        } else {
            let required = if target.requires_exact_focus() {
                "exact target-window focus"
            } else {
                "app-level focus"
            };
            Err(format!(
                "Did not send input because {required} verification failed after activating the target window. Focus result: requested window_id {}, focused window_id {:?}.",
                focus.requested_window.window_id,
                focus.focused_window.as_ref().map(|window| window.window_id)
            ))
        }
    }

    fn cache_desktop_size(&self, width: u32, height: u32) {
        if width == 0 || height == 0 {
            return;
        }
        if let Ok(mut guard) = self.desktop_size.lock() {
            *guard = Some((width, height));
        }
    }

    fn logical_portal_point(
        &self,
        session: &PortalPointerSession,
        x: i32,
        y: i32,
    ) -> Option<(i32, i32)> {
        let capture_size = self.desktop_size.lock().ok().and_then(|guard| *guard);
        session.logical_point_from_capture(x, y, capture_size)
    }

    /// Capture size for overlay points, which are in screenshot pixels like
    /// the click tools. Only a cached size is used: capturing the screen just
    /// for the overlay could prompt the user.
    fn indicator_space(&self) -> Option<(u32, u32)> {
        *self.desktop_size.lock().ok()?
    }

    /// Whether typed text would land in a password field. Unknown counts as
    /// yes, so the overlay never shows a secret it could not rule out.
    async fn typing_into_secret(&self, focus: Option<&WindowFocusResult>) -> bool {
        if !self.indicator.enabled() {
            return true;
        }
        let focused_role = async {
            let pid = match focus {
                Some(focus) => focus
                    .focused_window
                    .as_ref()
                    .and_then(|window| window.pid)
                    .or(focus.requested_window.pid),
                None => focused_window().await.ok().flatten()?.pid,
            };
            // Only the app that receives the keystrokes can tell where they
            // land; another app may keep a stale focused element.
            match probe_focused_element(Some(pid?)).await.ok()? {
                FocusProbe::Found(element) => Some(element.role),
                _ => None,
            }
        };
        timeout(Duration::from_millis(250), focused_role)
            .await
            .ok()
            .flatten()
            .is_none_or(|role| crate::indicator::is_secret_role(&role))
    }

    /// COORDINATE SPACES: window bounds (list_windows / extension frame rects)
    /// and the extension monitor layout are in LOGICAL pixels, while click/
    /// scroll coordinates and screenshot captures are in PHYSICAL capture
    /// pixels. On fractionally-scaled displays the two differ, so each check
    /// below only ever compares values from the same space.
    ///
    /// Logical monitor rectangles from the GNOME Shell extension, for checks
    /// against logical window bounds. None when the extension is unavailable.
    async fn logical_monitor_rects(&self) -> Option<Vec<(i32, i32, i32, i32)>> {
        let monitors = crate::windowing::backends::gnome::extension_monitor_layout()
            .await
            .ok()?;
        (!monitors.is_empty()).then(|| {
            monitors
                .iter()
                .map(|m| (m.x, m.y, m.width, m.height))
                .collect()
        })
    }

    /// Physical capture-space desktop rectangle (union of monitors as captured
    /// by the screenshot pipeline), for checks against click coordinates.
    /// Best-effort; None disables the check.
    async fn capture_space_rect(&self) -> Option<(i32, i32, i32, i32)> {
        let cached = self.desktop_size.lock().ok().and_then(|guard| *guard);
        if let Some((w, h)) = cached {
            return Some((0, 0, w as i32, h as i32));
        }
        // One-time prime: a full-frame capture reveals the desktop size when
        // no prior capture is available.
        let raw = capture_screenshot_raw().await.ok()?;
        self.cache_desktop_size(raw.width, raw.height);
        (raw.width > 0 && raw.height > 0).then_some((0, 0, raw.width as i32, raw.height as i32))
    }

    /// Warn when a targeted window pokes outside every monitor: clicks and
    /// screenshots silently truncate to visible pixels there, which reads as
    /// "success" while landing nowhere.
    async fn off_screen_note_for_bounds(
        &self,
        bounds: &crate::windowing::WindowBounds,
    ) -> Option<String> {
        let (x, y) = bounds.x.zip(bounds.y)?;
        if bounds.width == 0 || bounds.height == 0 {
            return None;
        }
        // Window bounds are logical pixels: prefer the extension's logical
        // monitor layout (same space). The physical capture rect is a safe
        // fallback — on scaled displays it is at least as large as the logical
        // union, so it can only under-warn, never false-positive.
        let rects = match self.logical_monitor_rects().await {
            Some(rects) => rects,
            None => vec![self.capture_space_rect().await?],
        };
        let (w, h) = (bounds.width as i64, bounds.height as i64);
        let window_area = w * h;
        let mut visible_area = 0_i64;
        for (mx, my, mw, mh) in &rects {
            let ix = (x as i64).max(*mx as i64);
            let iy = (y as i64).max(*my as i64);
            let ix2 = (x as i64 + w).min(*mx as i64 + *mw as i64);
            let iy2 = (y as i64 + h).min(*my as i64 + *mh as i64);
            if ix2 > ix && iy2 > iy {
                // Overlapping monitors are rare; treating them as additive keeps
                // this a cheap best-effort heuristic.
                visible_area += (ix2 - ix) * (iy2 - iy);
            }
        }
        let visible_pct = (visible_area.min(window_area) * 100) / window_area.max(1);
        if visible_pct >= 100 {
            return None;
        }
        Some(format!(
            "WARNING: the target window (bounds {x},{y} {w}x{h}) is only ~{visible_pct}% on-screen; off-screen regions are missing from screenshots and unreachable by coordinate input. Use move_window/resize_window to bring it fully on-screen."
        ))
    }

    /// Warn when a click/scroll coordinate is outside the captured desktop.
    /// Click coordinates are physical capture-space pixels, so compare ONLY
    /// against the capture rect — the extension's logical layout is a
    /// different space on scaled displays and would false-positive. This is a
    /// best-effort warning: never request a new capture (and possibly consent)
    /// merely to decide whether to append a note to an input result.
    async fn off_screen_note_for_point(&self, x: i32, y: i32) -> Option<String> {
        let (mw, mh) = self.desktop_size.lock().ok().and_then(|guard| *guard)?;
        let visible = x >= 0 && y >= 0 && (x as u32) < mw && (y as u32) < mh;
        if visible {
            return None;
        }
        Some(format!(
            "WARNING: coordinate {x},{y} is outside the captured desktop ({mw}x{mh}); the input landed on no visible pixel."
        ))
    }

    /// Post-input feedback: which AT-SPI element holds keyboard focus in the
    /// target app, and whether it is editable. Guards against the blind-typing
    /// trap where verified *window* focus still sends keystrokes nowhere.
    async fn focused_element_feedback(
        &self,
        focus: Option<&WindowFocusResult>,
        expects_editable: bool,
    ) -> Option<String> {
        let focus = focus?;
        let pid = focus
            .focused_window
            .as_ref()
            .and_then(|window| window.pid)
            .or(focus.requested_window.pid);
        match timeout(Duration::from_millis(1500), probe_focused_element(pid)).await {
            Ok(Ok(probe)) => Some(focus_probe_feedback(&probe, expects_editable)),
            Ok(Err(error)) => Some(format!(
                "Focused-element feedback unavailable ({}).",
                first_line(&format!("{error:#}"))
            )),
            Err(_) => {
                Some("Focused-element feedback unavailable (AT-SPI probe timed out).".to_string())
            }
        }
    }

    /// Shared move/resize plumbing: resolve the window target, run the GNOME
    /// Shell extension operation, then re-query bounds to report the result.
    async fn window_geometry_op<F, Fut>(
        &self,
        received: Option<serde_json::Value>,
        target: &WindowTarget,
        op: F,
    ) -> Json<WindowGeometryOutput>
    where
        F: FnOnce(crate::windowing::WindowInfo) -> Fut,
        Fut: Future<Output = Result<String>>,
    {
        let windows = match list_windows().await {
            Ok(windows) => windows,
            Err(error) => {
                let error = format!("{error:#}");
                return Json(WindowGeometryOutput {
                    ok: false,
                    implemented: true,
                    backend: "unknown".to_string(),
                    window: None,
                    message: format!("Window listing failed: {error}"),
                    permissions_hint: window_permission_hint(&error),
                    received,
                });
            }
        };
        let window = match resolve_window_target(&windows, target) {
            Ok(window) => window.clone(),
            Err(error) => {
                return Json(WindowGeometryOutput {
                    ok: false,
                    implemented: true,
                    backend: "unknown".to_string(),
                    window: None,
                    message: format!("{error:#}"),
                    permissions_hint: None,
                    received,
                });
            }
        };
        let backend = window.backend.clone();
        let window_id = window.window_id;
        match op(window).await {
            Ok(message) => {
                // Re-query so the caller sees the compositor-final geometry
                // (tiling constraints, minimum sizes, etc. may adjust it).
                let window = list_windows().await.ok().and_then(|windows| {
                    windows
                        .into_iter()
                        .find(|window| window.window_id == window_id)
                });
                let mut message = message;
                if let Some(bounds) = window.as_ref().and_then(|window| window.bounds.as_ref()) {
                    if let Some(note) = self.off_screen_note_for_bounds(bounds).await {
                        message = format!("{message} {note}");
                    }
                }
                Json(WindowGeometryOutput {
                    ok: true,
                    implemented: true,
                    backend,
                    window,
                    message,
                    permissions_hint: None,
                    received,
                })
            }
            Err(error) => {
                let error = format!("{error:#}");
                Json(WindowGeometryOutput {
                    ok: false,
                    implemented: true,
                    backend,
                    window: None,
                    permissions_hint: window_permission_hint(&error),
                    message: error,
                    received,
                })
            }
        }
    }

    /// Notes appended after targeted keyboard input: off-screen window warning
    /// plus focused-element feedback.
    async fn input_landing_notes(
        &self,
        focus: Option<&WindowFocusResult>,
        expects_editable: bool,
    ) -> Vec<String> {
        let mut notes = Vec::new();
        if let Some(focus) = focus {
            let bounds = focus
                .focused_window
                .as_ref()
                .and_then(|window| window.bounds.as_ref())
                .or(focus.requested_window.bounds.as_ref());
            if let Some(bounds) = bounds {
                if let Some(note) = self.off_screen_note_for_bounds(bounds).await {
                    notes.push(note);
                }
            }
        }
        if let Some(note) = self.focused_element_feedback(focus, expects_editable).await {
            notes.push(note);
        }
        notes
    }

    #[cfg(test)]
    fn cache_nodes(&self, nodes: &[AccessibilityNode]) {
        self.cache_snapshot(nodes, None);
    }

    /// Readers may perform slow AT-SPI owner checks before resolving a click
    /// or scroll again. Keep the snapshot stable for the whole input operation;
    /// otherwise a checked index could resolve to a different app's node.
    async fn commit_snapshot(&self, nodes: &[AccessibilityNode], target_pid: Option<u32>) {
        let _input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        self.cache_snapshot(nodes, target_pid);
    }

    fn cache_snapshot(&self, nodes: &[AccessibilityNode], target_pid: Option<u32>) {
        if let Ok(mut cached) = self.last_snapshot.lock() {
            *cached = CachedAccessibilitySnapshot {
                nodes: nodes.to_vec(),
                pid: target_pid,
            };
        }
    }

    #[cfg(test)]
    fn clear_cached_nodes(&self) {
        self.cache_snapshot(&[], None);
    }

    /// Reject an element-targeted action whose cached node belongs to a
    /// different app than the action's target. A snapshot taken for a pid is
    /// judged by that pid; an untargeted snapshot mixes apps, so its node is
    /// judged by the pid that owns it on the accessibility bus. When neither is
    /// known the action proceeds, since a mismatch cannot be shown.
    async fn check_node_target(
        &self,
        node: &AccessibilityNode,
        target_pid: Option<u32>,
    ) -> std::result::Result<(), String> {
        let Some(target_pid) = target_pid else {
            return Ok(());
        };
        let snapshot_pid = self
            .last_snapshot
            .lock()
            .ok()
            .and_then(|snapshot| snapshot.pid);
        let owner_pid = match snapshot_pid {
            Some(pid) => Some(pid),
            None => object_ref_owner_pid(&node.object_ref).await.ok().flatten(),
        };
        match owner_pid {
            Some(owner_pid) if owner_pid != target_pid => Err(node_target_mismatch_message(
                node.index, owner_pid, target_pid,
            )),
            _ => Ok(()),
        }
    }

    /// The cached node an element-targeted click or scroll would act on, when
    /// the call names one. Resolution errors are left to the tool's own path.
    fn cached_node_for(
        &self,
        element_index: Option<u32>,
        selector: &ElementSelector<'_>,
        purpose: ElementResolvePurpose,
    ) -> Option<AccessibilityNode> {
        if element_index.is_none() && selector.is_empty() {
            return None;
        }
        self.resolve_cached_node(element_index, selector, purpose)
            .ok()
    }

    fn resolve_optional_target_point(
        &self,
        x: Option<i32>,
        y: Option<i32>,
        element_index: Option<u32>,
    ) -> std::result::Result<Option<(i32, i32)>, String> {
        match (x.zip(y), element_index) {
            (Some(point), _) => Ok(Some(point)),
            (None, Some(index)) => self
                .center_for_cached_node(index)
                .map(Some)
                .ok_or_else(|| {
                    format!(
                        "No clickable bounds cached for element_index {index}. Call get_app_state first and choose a node with positive width and height."
                    )
                }),
            (None, None) => Ok(None),
        }
    }

    fn resolve_click_target(
        &self,
        params: &ClickParams,
    ) -> std::result::Result<ClickTarget, String> {
        if let Some((x, y)) = params.x.zip(params.y) {
            return Ok(ClickTarget::Coordinates(x, y));
        }

        let selector = params.selector();
        let node = self.resolve_cached_node(
            params.element_index,
            &selector,
            ElementResolvePurpose::Click,
        )?;

        // Toolkit extents are not necessarily in the pointer backend's space
        // (GTK3 X11 HiDPI, GTK4 zero-origin bounds). Ordinary activation does
        // not need a guessed scale when the target exposes its native action.
        if is_plain_left_click(params.button.as_deref(), params.click_count) {
            if let Some(action) =
                primary_action(&node.actions).filter(|action| click_equivalent_action(action))
            {
                return Ok(ClickTarget::PrimaryAction {
                    object_ref: node.object_ref.clone(),
                    action_name: Some(action.name.clone()),
                    action_index: action.index,
                });
            }
        }

        if let Some((x, y)) = node.bounds.as_ref().and_then(bounds_center) {
            return Ok(ClickTarget::Coordinates(x, y));
        }

        if !is_plain_left_click(params.button.as_deref(), params.click_count) {
            return Err(format!(
                "No clickable bounds cached for element_index {}. Call get_app_state first and choose a node with positive width and height.",
                node.index
            ));
        }

        Err(format!(
            "No clickable bounds or click-equivalent AT-SPI action for element_index {}. Use perform_action explicitly for other actions.",
            node.index
        ))
    }

    fn center_for_cached_node(&self, element_index: u32) -> Option<(i32, i32)> {
        let cached = self.last_snapshot.lock().ok()?;
        let node = cached
            .nodes
            .iter()
            .find(|node| node.index == element_index)?;
        bounds_center(node.bounds.as_ref()?)
    }

    fn resolve_object_ref(
        &self,
        element_index: Option<u32>,
        element_identifier: Option<&str>,
        selector: &ElementSelector<'_>,
        purpose: ElementResolvePurpose,
    ) -> std::result::Result<String, String> {
        if let Some(element_identifier) = element_identifier
            .map(str::trim)
            .filter(|value| !value.is_empty())
        {
            return Ok(element_identifier.to_string());
        }

        self.resolve_cached_node(element_index, selector, purpose)
            .map(|node| node.object_ref)
    }

    fn resolve_cached_node(
        &self,
        element_index: Option<u32>,
        selector: &ElementSelector<'_>,
        purpose: ElementResolvePurpose,
    ) -> std::result::Result<AccessibilityNode, String> {
        let cached = self.last_snapshot.lock().map_err(|_| {
            "Could not read cached accessibility nodes. Call get_app_state and retry.".to_string()
        })?;

        if let Some(element_index) = element_index {
            return cached.nodes
                .iter()
                .find(|node| node.index == element_index)
                .cloned()
                .ok_or_else(|| {
                    format!(
                        "No cached accessibility node for element_index {element_index}. Call get_app_state first."
                    )
                });
        }

        if selector.is_empty() {
            return Err(
                "Pass element_index, element_identifier, or a semantic selector such as role/name/text/states from the latest get_app_state result."
                    .to_string(),
            );
        }

        resolve_semantic_node(cached.nodes.as_slice(), selector, purpose)
    }

    async fn perform_element_action(
        &self,
        params: &ActionParams,
        requested_action: Option<&str>,
    ) -> Json<ActionOutput> {
        let _input_guard = Arc::clone(&self.input_operation_lock).lock_owned().await;
        let received = Some(serde_json::json!(params.clone()));
        let object_ref = match self.resolve_object_ref(
            params.element_index,
            params.element_identifier.as_deref(),
            &params.selector(),
            ElementResolvePurpose::Action,
        ) {
            Ok(object_ref) => object_ref,
            Err(message) => {
                return Json(ActionOutput {
                    ok: false,
                    implemented: true,
                    action: "perform_action".to_string(),
                    message,
                    received,
                });
            }
        };

        self.indicator.action("perform_action").await;
        match invoke_accessibility_action(&object_ref, requested_action).await {
            Ok(invocation) => Json(ActionOutput {
                ok: invocation.ok,
                implemented: true,
                action: "perform_action".to_string(),
                message: if invocation.ok {
                    format!(
                        "AT-SPI action {} ({}) invoked.",
                        invocation.action_index,
                        invocation
                            .action_name
                            .as_deref()
                            .filter(|name| !name.is_empty())
                            .unwrap_or("unnamed")
                    )
                } else {
                    format!(
                        "AT-SPI action {} ({}) returned false.",
                        invocation.action_index,
                        invocation
                            .action_name
                            .as_deref()
                            .filter(|name| !name.is_empty())
                            .unwrap_or("unnamed")
                    )
                },
                received,
            }),
            Err(error) => Json(ActionOutput {
                ok: false,
                implemented: true,
                action: "perform_action".to_string(),
                message: error.to_string(),
                received,
            }),
        }
    }
}

#[derive(Debug)]
enum ClickTarget {
    Coordinates(i32, i32),
    PrimaryAction {
        object_ref: String,
        action_name: Option<String>,
        action_index: i32,
    },
}

#[derive(Debug, Clone, Copy)]
enum ElementResolvePurpose {
    Click,
    Action,
    SetValue,
}

#[derive(Debug, Clone, Copy, Default)]
struct ElementSelector<'a> {
    role: Option<&'a str>,
    name: Option<&'a str>,
    text: Option<&'a str>,
    states: &'a [String],
}

impl ElementSelector<'_> {
    fn is_empty(&self) -> bool {
        [self.role, self.name, self.text]
            .into_iter()
            .all(|value| value.map(str::trim).is_none_or(str::is_empty))
            && self.states.iter().all(|value| value.trim().is_empty())
    }
}

fn resolve_semantic_node(
    nodes: &[AccessibilityNode],
    selector: &ElementSelector<'_>,
    purpose: ElementResolvePurpose,
) -> std::result::Result<AccessibilityNode, String> {
    let mut matches = nodes
        .iter()
        .filter(|node| node_matches_selector(node, selector))
        .collect::<Vec<_>>();

    if matches.is_empty() {
        return Err(format!(
            "No cached accessibility node matched semantic selector {}. Call get_app_state first or pass element_index.",
            describe_selector(selector)
        ));
    }

    if let Some(node) =
        unique_preferred_node(&matches, |node| node_matches_resolve_purpose(node, purpose))
    {
        return Ok(node.clone());
    }

    let useful_matches = matches
        .iter()
        .copied()
        .filter(|node| node_matches_resolve_purpose(node, purpose))
        .collect::<Vec<_>>();
    if !useful_matches.is_empty() {
        matches = useful_matches;
    }

    if let Some(node) = unique_preferred_node(&matches, node_is_showing) {
        return Ok(node.clone());
    }

    let visible_matches = matches
        .iter()
        .copied()
        .filter(|node| node_is_showing(node))
        .collect::<Vec<_>>();
    if !visible_matches.is_empty() {
        matches = visible_matches;
    }

    if matches.len() == 1 {
        return Ok(matches[0].clone());
    }

    Err(format!(
        "Semantic selector {} matched multiple cached nodes: {}. Pass element_index or add more selector fields.",
        describe_selector(selector),
        describe_matching_nodes(&matches),
    ))
}

fn unique_preferred_node<'a>(
    nodes: &[&'a AccessibilityNode],
    predicate: impl Fn(&AccessibilityNode) -> bool,
) -> Option<&'a AccessibilityNode> {
    let mut matches = nodes.iter().copied().filter(|node| predicate(node));
    let first = matches.next()?;
    matches.next().is_none().then_some(first)
}

fn node_matches_selector(node: &AccessibilityNode, selector: &ElementSelector<'_>) -> bool {
    selector
        .role
        .is_none_or(|role| normalized_contains(Some(node.role.as_str()), role))
        && selector
            .name
            .is_none_or(|name| normalized_contains(node.name.as_deref(), name))
        && selector.text.is_none_or(|text| {
            normalized_contains(
                node.text
                    .as_ref()
                    .and_then(|value| value.content.as_deref()),
                text,
            ) || normalized_contains(node.name.as_deref(), text)
                || normalized_contains(node.description.as_deref(), text)
        })
        && selector
            .states
            .iter()
            .filter(|state| !state.trim().is_empty())
            .all(|state| {
                node.states
                    .iter()
                    .any(|node_state| normalized_equals(node_state, state))
            })
}

fn node_matches_resolve_purpose(node: &AccessibilityNode, purpose: ElementResolvePurpose) -> bool {
    match purpose {
        ElementResolvePurpose::Click => {
            node.bounds.as_ref().and_then(bounds_center).is_some()
                || primary_action_name(&node.actions).is_some()
        }
        ElementResolvePurpose::Action => !node.actions.is_empty(),
        ElementResolvePurpose::SetValue => node.supports_editable_text || node.value.is_some(),
    }
}

fn node_is_showing(node: &AccessibilityNode) -> bool {
    node.states
        .iter()
        .any(|state| normalized_equals(state, "showing"))
        && node
            .states
            .iter()
            .any(|state| normalized_equals(state, "visible"))
}

fn normalized_equals(actual: &str, expected: &str) -> bool {
    normalize_text(actual) == normalize_text(expected)
}

fn normalized_contains(actual: Option<&str>, expected: &str) -> bool {
    let expected = normalize_text(expected);
    !expected.is_empty()
        && actual
            .map(normalize_text)
            .is_some_and(|actual| actual.contains(&expected))
}

fn normalize_text(value: &str) -> String {
    value
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .to_lowercase()
}

fn describe_selector(selector: &ElementSelector<'_>) -> String {
    let mut parts = Vec::new();
    if let Some(role) = selector.role.map(str::trim).filter(|role| !role.is_empty()) {
        parts.push(format!("role={role:?}"));
    }
    if let Some(name) = selector.name.map(str::trim).filter(|name| !name.is_empty()) {
        parts.push(format!("name={name:?}"));
    }
    if let Some(text) = selector.text.map(str::trim).filter(|text| !text.is_empty()) {
        parts.push(format!("text={text:?}"));
    }
    let states = selector
        .states
        .iter()
        .map(|state| state.trim())
        .filter(|state| !state.is_empty())
        .collect::<Vec<_>>();
    if !states.is_empty() {
        parts.push(format!("states={states:?}"));
    }
    if parts.is_empty() {
        "<empty>".to_string()
    } else {
        parts.join(", ")
    }
}

fn describe_matching_nodes(nodes: &[&AccessibilityNode]) -> String {
    nodes
        .iter()
        .take(8)
        .map(|node| {
            format!(
                "element_index {} role={:?} name={:?}",
                node.index, node.role, node.name
            )
        })
        .collect::<Vec<_>>()
        .join("; ")
}

fn is_plain_left_click(button: Option<&str>, click_count: Option<u32>) -> bool {
    let button = button.unwrap_or("left");
    let click_count = click_count.unwrap_or(1);
    matches!(button.to_ascii_lowercase().as_str(), "left" | "primary") && click_count == 1
}

fn click_equivalent_action(action: &AccessibilityAction) -> bool {
    matches!(
        action.name.to_ascii_lowercase().as_str(),
        "click" | "press" | "toggle"
    )
}

fn requested_or_primary_action(action: Option<&str>) -> &str {
    match action.map(str::trim).filter(|value| !value.is_empty()) {
        Some(action) => action,
        None => "0",
    }
}

fn primary_action(actions: &[AccessibilityAction]) -> Option<&AccessibilityAction> {
    actions.first()
}

fn primary_action_name(actions: &[AccessibilityAction]) -> Option<String> {
    primary_action(actions).map(|action| action.name.clone())
}

fn bounds_center(bounds: &Bounds) -> Option<(i32, i32)> {
    if bounds.width <= 0 || bounds.height <= 0 {
        return None;
    }
    if bounds.x <= i32::MIN / 2 || bounds.y <= i32::MIN / 2 {
        return None;
    }
    Some((
        bounds.x.checked_add(bounds.width / 2)?,
        bounds.y.checked_add(bounds.height / 2)?,
    ))
}

/// Context-cost warning for an AT-SPI snapshot that no app target narrowed.
/// Silent when the tree failed (the failure message already explains) or when
/// any filter or pid match scoped the roots. When the caller did pass a target
/// and it still matched no AT-SPI root, telling them to pass a target would
/// only replay the same desktop-wide snapshot, so that case points at the
/// app's accessibility support instead.
fn unscoped_accessibility_tree_warning(
    tree_scoped: bool,
    tree_ok: bool,
    target_requested: bool,
) -> Option<&'static str> {
    if !tree_ok || tree_scoped {
        return None;
    }
    Some(if target_requested {
        "WARNING: the requested target matched no AT-SPI application root, so the accessibility tree covers the whole desktop and can flood context. The target app may expose no accessibility tree (Electron apps need --force-renderer-accessibility); check list_apps for its AT-SPI name and pass that as app_name_or_bundle_identifier, or lower max_nodes to bound the cost."
    } else {
        "WARNING: no app target scoped the accessibility tree, so it covers the whole desktop and can flood context. Pass app_name_or_bundle_identifier or a window target (window_id, pid, app_id, wm_class, title) to limit it."
    })
}

/// Note appended when raw traversal hit max_nodes; the tree is incomplete.
fn truncated_accessibility_tree_note(truncated: bool) -> Option<&'static str> {
    truncated.then_some(
        "The node, depth, or read budget stopped traversal with unread elements left. To recover missing elements, scope to a narrower app or window target and raise max_nodes or max_depth (hard caps 2000 and 64); lowering max_nodes would drop more.",
    )
}

fn compact_accessibility_tree(nodes: Vec<AccessibilityNode>) -> Vec<AccessibilityNode> {
    if nodes.is_empty() {
        return nodes;
    }

    let keep = nodes
        .iter()
        .map(should_keep_accessibility_node)
        .collect::<Vec<_>>();
    let mut old_to_new = vec![None; nodes.len()];
    let mut compacted = Vec::new();

    for (old_index, node) in nodes.iter().enumerate() {
        if !keep[old_index] {
            continue;
        }

        let mut compacted_node = node.clone();
        compacted_node.index = compacted.len() as u32;
        compacted_node.parent_index = nearest_kept_parent(&keep, &nodes, old_index);
        old_to_new[old_index] = Some(compacted_node.index);
        compacted.push(compacted_node);
    }

    for node in &mut compacted {
        node.parent_index = node
            .parent_index
            .and_then(|old_parent| old_to_new.get(old_parent as usize).copied().flatten());
    }

    let child_counts = compacted.iter().filter_map(|node| node.parent_index).fold(
        vec![0_i32; compacted.len()],
        |mut counts, parent_index| {
            counts[parent_index as usize] += 1;
            counts
        },
    );

    for (index, node) in compacted.iter_mut().enumerate() {
        node.child_count = child_counts[index];
    }

    compacted
}

fn nearest_kept_parent(
    keep: &[bool],
    nodes: &[AccessibilityNode],
    old_index: usize,
) -> Option<u32> {
    let mut parent = nodes[old_index].parent_index;
    while let Some(parent_index) = parent {
        let parent_usize = parent_index as usize;
        if keep.get(parent_usize).copied().unwrap_or(false) {
            return Some(parent_index);
        }
        parent = nodes.get(parent_usize).and_then(|node| node.parent_index);
    }
    None
}

fn should_keep_accessibility_node(node: &AccessibilityNode) -> bool {
    if node.depth <= 1 {
        return true;
    }

    if is_actionable_accessibility_node(node) || has_meaningful_node_copy(node) {
        return true;
    }

    matches!(
        node.role.as_str(),
        "page tab" | "menu item" | "menu" | "list item" | "tree item"
    ) && !is_sentinel_or_missing_bounds(node.bounds.as_ref())
}

fn is_actionable_accessibility_node(node: &AccessibilityNode) -> bool {
    !node.actions.is_empty() || node.supports_editable_text || node.value.is_some()
}

fn has_meaningful_node_copy(node: &AccessibilityNode) -> bool {
    has_non_empty_text(node.name.as_deref())
        || has_non_empty_text(node.description.as_deref())
        || has_non_empty_text(node.text.as_ref().and_then(|text| text.content.as_deref()))
}

fn has_non_empty_text(value: Option<&str>) -> bool {
    value.map(str::trim).is_some_and(|value| !value.is_empty())
}

fn is_sentinel_or_missing_bounds(bounds: Option<&Bounds>) -> bool {
    bounds.is_none()
}

fn select_accessibility_object_ref(
    apps: &[AccessibleAppSummary],
    target_pid: u32,
    candidates: &[String],
) -> Option<String> {
    let mut pid_matches = apps.iter().filter(|app| app.pid == Some(target_pid));
    let first = pid_matches.next()?;
    let second = pid_matches.next();

    if second.is_none() {
        return Some(first.object_ref.clone());
    }

    let lowered_candidates = candidates
        .iter()
        .map(|candidate| candidate.to_ascii_lowercase())
        .collect::<Vec<_>>();

    apps.iter()
        .filter(|app| app.pid == Some(target_pid))
        .find(|app| {
            let name = app.name.as_deref().unwrap_or_default().to_ascii_lowercase();
            lowered_candidates
                .iter()
                .any(|candidate| !candidate.is_empty() && name.contains(candidate))
        })
        .map(|app| app.object_ref.clone())
        .or_else(|| Some(first.object_ref.clone()))
}

fn accessibility_filter_candidates(window_context: Option<&WindowInfo>) -> Vec<String> {
    let Some(window) = window_context else {
        return Vec::new();
    };

    let mut candidates = Vec::new();
    push_candidate(&mut candidates, window.title.as_deref());
    push_candidate(&mut candidates, window.wm_class.as_deref());

    if let Some(app_id) = trimmed_nonempty(window.app_id.as_deref()) {
        if !app_id.starts_with("window:") {
            push_candidate(&mut candidates, Some(app_id));
            if let Some(stripped) = app_id.strip_suffix(".desktop") {
                push_candidate(&mut candidates, Some(stripped));
                let normalized = stripped.replace(['-', '_', '.'], " ");
                push_candidate(&mut candidates, Some(normalized.as_str()));
            } else {
                let normalized = app_id.replace(['-', '_', '.'], " ");
                push_candidate(&mut candidates, Some(normalized.as_str()));
            }
        }
    }

    candidates
}

fn push_candidate(candidates: &mut Vec<String>, value: Option<&str>) {
    let Some(value) = trimmed_nonempty(value) else {
        return;
    };

    if !candidates.iter().any(|candidate| candidate == value) {
        candidates.push(value.to_string());
    }
}

fn trimmed_nonempty(value: Option<&str>) -> Option<&str> {
    value.map(str::trim).filter(|value| !value.is_empty())
}

fn env_contains(key: &str, needle: &str) -> bool {
    env::var(key)
        .ok()
        .is_some_and(|value| value.to_ascii_lowercase().contains(needle))
}

/// True when an environment variable is set to `"1"` (an explicit on switch).
fn env_flag_enabled(key: &str) -> bool {
    env::var(key).ok().as_deref() == Some("1")
}

fn env_var_non_empty(key: &str) -> bool {
    env::var(key)
        .map(|value| !value.trim().is_empty())
        .unwrap_or(false)
}

/// Return the base64 payload of a `data:` URL (or the original string if bare).
fn data_url_payload(data_url: &str) -> String {
    data_url
        .split_once(',')
        .map(|(_, payload)| payload)
        .unwrap_or(data_url)
        .to_string()
}

fn session_is_wayland(session_type: Option<&str>, wayland_display: Option<&str>) -> bool {
    match session_type
        .map(str::trim)
        .filter(|value| !value.is_empty())
    {
        Some(value) => value.eq_ignore_ascii_case("wayland"),
        None => wayland_display.is_some_and(|value| !value.trim().is_empty()),
    }
}

fn native_x11_xdotool_pointer_session(
    session_type: Option<&str>,
    wayland_display: Option<&str>,
) -> bool {
    session_type.is_some_and(|value| value.trim().eq_ignore_ascii_case("x11"))
        && wayland_display.is_none_or(|value| value.trim().is_empty())
}

fn prefer_xdotool_pointer(
    force_ydotool: bool,
    session_type: Option<&str>,
    display_available: bool,
    wayland_display: Option<&str>,
    xdotool_available: bool,
) -> bool {
    !force_ydotool
        && native_x11_xdotool_pointer_session(session_type, wayland_display)
        && display_available
        && xdotool_available
}

fn prefer_xdotool_keyboard(
    force_ydotool: bool,
    force_xdotool: bool,
    is_wayland: bool,
    display_available: bool,
    xdotool_available: bool,
) -> bool {
    !force_ydotool && display_available && xdotool_available && (force_xdotool || !is_wayland)
}

fn prepare_app_state_screenshot(
    mut raw: RawScreenshotCapture,
    crop: Option<(i32, i32, u32, u32)>,
    target_requested: bool,
    options: ScreenshotPayloadOptions,
) -> Result<ScreenshotCapture> {
    if target_requested && crop.is_none() {
        anyhow::bail!(
            "targeted screenshot requires a resolved window; refusing to return the full desktop"
        );
    }
    if let Some(rect) = crop {
        let (x, y, width, height) = clip_capture_rect(rect, raw.width, raw.height)?;
        let (bytes, width, height) = crop_png(&raw.bytes, x, y, width, height)
            .map_err(|error| anyhow::anyhow!("targeted screenshot crop failed: {error}"))?;
        raw = RawScreenshotCapture {
            mime_type: raw.mime_type,
            bytes,
            source: raw.source,
            width,
            height,
        };
    }
    prepare_screenshot_payload(raw, options)
}

fn ensure_readonly_screenshot_target_is_visible(window: &WindowInfo) -> Result<()> {
    if window.hidden {
        anyhow::bail!("targeted get_app_state screenshot requires a visible, unminimized window");
    }
    if !window.focused {
        anyhow::bail!(
            "targeted get_app_state screenshot requires the window to already be focused; use the screenshot tool to raise it before capture"
        );
    }
    Ok(())
}

#[derive(Debug, Clone, Copy)]
struct WindowCoordinateMap {
    capture_rect: (i32, i32, u32, u32),
    full_capture_rect: (i32, i32, u32, u32),
    portal_rect: Option<(i32, i32, u32, u32)>,
}

impl WindowCoordinateMap {
    fn portal_point(&self, capture_x: i32, capture_y: i32) -> Option<(i32, i32)> {
        let (portal_x, portal_y, portal_width, portal_height) = self.portal_rect?;
        let (full_x, full_y, full_width, full_height) = self.full_capture_rect;
        Some((
            map_coordinate_between_rects(capture_x, full_x, full_width, portal_x, portal_width),
            map_coordinate_between_rects(capture_y, full_y, full_height, portal_y, portal_height),
        ))
    }
}

fn map_coordinate_between_rects(
    value: i32,
    source_origin: i32,
    source_size: u32,
    target_origin: i32,
    target_size: u32,
) -> i32 {
    let offset = i64::from(value) - i64::from(source_origin);
    let scaled = offset.saturating_mul(i64::from(target_size)) / i64::from(source_size.max(1));
    (i64::from(target_origin) + scaled).clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32
}

fn logical_window_crop_rect(
    bounds: &crate::windowing::WindowBounds,
    monitors: &[(i32, i32, i32, i32)],
    capture_width: u32,
    capture_height: u32,
) -> Result<(i32, i32, u32, u32)> {
    let mut monitors = monitors
        .iter()
        .filter(|(_, _, width, height)| *width > 0 && *height > 0);
    let first = monitors
        .next()
        .ok_or_else(|| anyhow::anyhow!("desktop returned no usable monitor geometry"))?;
    let (mut min_x, mut min_y) = (i64::from(first.0), i64::from(first.1));
    let (mut max_x, mut max_y) = (
        i64::from(first.0) + i64::from(first.2),
        i64::from(first.1) + i64::from(first.3),
    );
    for (x, y, width, height) in monitors {
        min_x = min_x.min(i64::from(*x));
        min_y = min_y.min(i64::from(*y));
        max_x = max_x.max(i64::from(*x) + i64::from(*width));
        max_y = max_y.max(i64::from(*y) + i64::from(*height));
    }
    let logical_width = max_x - min_x;
    let logical_height = max_y - min_y;
    if logical_width <= 0 || logical_height <= 0 || capture_width == 0 || capture_height == 0 {
        anyhow::bail!("screenshot or monitor geometry is empty");
    }
    let scale_x = f64::from(capture_width) / logical_width as f64;
    let scale_y = f64::from(capture_height) / logical_height as f64;
    if !scale_x.is_finite() || !scale_y.is_finite() || (scale_x - scale_y).abs() > 0.01 {
        anyhow::bail!(
            "captured desktop {}x{} does not have a uniform scale relative to the logical monitor layout {}x{}",
            capture_width,
            capture_height,
            logical_width,
            logical_height
        );
    }

    let x = i64::from(
        bounds
            .x
            .ok_or_else(|| anyhow::anyhow!("window x is unavailable"))?,
    );
    let y = i64::from(
        bounds
            .y
            .ok_or_else(|| anyhow::anyhow!("window y is unavailable"))?,
    );
    if bounds.width == 0 || bounds.height == 0 {
        anyhow::bail!("window bounds are empty");
    }
    let left = (((x - min_x) as f64) * scale_x).floor() as i64;
    let top = (((y - min_y) as f64) * scale_y).floor() as i64;
    let right = (((x + i64::from(bounds.width) - min_x) as f64) * scale_x).ceil() as i64;
    let bottom = (((y + i64::from(bounds.height) - min_y) as f64) * scale_y).ceil() as i64;
    let width = u32::try_from(right - left)
        .map_err(|_| anyhow::anyhow!("scaled window width is invalid"))?;
    let height = u32::try_from(bottom - top)
        .map_err(|_| anyhow::anyhow!("scaled window height is invalid"))?;
    let left = i32::try_from(left).map_err(|_| anyhow::anyhow!("scaled window x is invalid"))?;
    let top = i32::try_from(top).map_err(|_| anyhow::anyhow!("scaled window y is invalid"))?;
    Ok((left, top, width, height))
}

fn clip_capture_rect(
    (x, y, width, height): (i32, i32, u32, u32),
    capture_width: u32,
    capture_height: u32,
) -> Result<(i32, i32, u32, u32)> {
    let left = i64::from(x).max(0);
    let top = i64::from(y).max(0);
    let right = (i64::from(x) + i64::from(width)).min(i64::from(capture_width));
    let bottom = (i64::from(y) + i64::from(height)).min(i64::from(capture_height));
    if right <= left || bottom <= top {
        anyhow::bail!(
            "targeted screenshot window is outside the captured desktop; refusing to return the full desktop"
        );
    }
    Ok((
        i32::try_from(left).map_err(|_| anyhow::anyhow!("capture crop x is invalid"))?,
        i32::try_from(top).map_err(|_| anyhow::anyhow!("capture crop y is invalid"))?,
        u32::try_from(right - left)
            .map_err(|_| anyhow::anyhow!("capture crop width is invalid"))?,
        u32::try_from(bottom - top)
            .map_err(|_| anyhow::anyhow!("capture crop height is invalid"))?,
    ))
}

/// Convert a window's bounds into a crop rectangle, if it has a usable origin
/// and non-zero size.
fn window_crop_rect(bounds: &crate::windowing::WindowBounds) -> Option<(i32, i32, u32, u32)> {
    let x = bounds.x?;
    let y = bounds.y?;
    if bounds.width == 0 || bounds.height == 0 {
        return None;
    }
    Some((x, y, bounds.width, bounds.height))
}

fn apply_window_relative_click_coordinates(
    params: &mut ClickParams,
    capture_rect: (i32, i32, u32, u32),
) -> std::result::Result<(), String> {
    let (relative_x, relative_y) = params
        .x
        .zip(params.y)
        .ok_or_else(|| "Relative coordinate clicks require both x and y.".to_string())?;
    let (origin_x, origin_y, width, height) = capture_rect;
    if width == 0 || height == 0 {
        return Err(
            "Relative coordinate clicks require non-empty target-window bounds.".to_string(),
        );
    }
    if relative_x < 0 || relative_y < 0 {
        return Err("Relative click coordinates must be inside target-window bounds.".to_string());
    }
    if relative_x as u32 >= width || relative_y as u32 >= height {
        return Err("Relative click coordinates must be inside target-window bounds.".to_string());
    }
    let x = origin_x
        .checked_add(relative_x)
        .ok_or_else(|| "Relative click x coordinate overflowed.".to_string())?;
    let y = origin_y
        .checked_add(relative_y)
        .ok_or_else(|| "Relative click y coordinate overflowed.".to_string())?;
    params.x = Some(x);
    params.y = Some(y);
    Ok(())
}

/// Point a window-targeted scroll at the centre of the resolved window when
/// the caller supplied no coordinates. Without this the wheel events land on
/// whatever is under the current pointer position.
fn apply_window_center_scroll_point(
    params: &mut ScrollParams,
    capture_rect: (i32, i32, u32, u32),
) -> std::result::Result<(), String> {
    let (origin_x, origin_y, width, height) = capture_rect;
    if width == 0 || height == 0 {
        return Err(
            "Window-targeted scroll requires non-empty target-window bounds; pass x/y explicitly."
                .to_string(),
        );
    }
    params.x = Some(origin_x.saturating_add((width / 2) as i32));
    params.y = Some(origin_y.saturating_add((height / 2) as i32));
    Ok(())
}

fn apply_window_relative_scroll_coordinates(
    params: &mut ScrollParams,
    capture_rect: (i32, i32, u32, u32),
) -> std::result::Result<(), String> {
    let (relative_x, relative_y) = params
        .x
        .zip(params.y)
        .ok_or_else(|| "Relative scroll coordinates require both x and y.".to_string())?;
    let (origin_x, origin_y, width, height) = capture_rect;
    if width == 0 || height == 0 {
        return Err(
            "Relative scroll coordinates require non-empty target-window bounds.".to_string(),
        );
    }
    if relative_x < 0 || relative_y < 0 || relative_x as u32 >= width || relative_y as u32 >= height
    {
        return Err("Relative scroll coordinates must be inside target-window bounds.".to_string());
    }
    params.x = Some(origin_x.saturating_add(relative_x));
    params.y = Some(origin_y.saturating_add(relative_y));
    Ok(())
}

/// Crop a PNG image to `(x, y, w, h)` (clamped to the image), returning the
/// re-encoded PNG and the actual cropped dimensions.
fn crop_png(
    raw: &[u8],
    x: i32,
    y: i32,
    w: u32,
    h: u32,
) -> std::result::Result<(Vec<u8>, u32, u32), String> {
    use std::io::Cursor;
    let img = image::load_from_memory_with_format(raw, image::ImageFormat::Png)
        .map_err(|e| format!("decode png: {e}"))?;
    let (iw, ih) = (img.width(), img.height());
    let x = x.max(0) as u32;
    let y = y.max(0) as u32;
    if x >= iw || y >= ih {
        return Err("crop origin outside image".into());
    }
    let w = w.min(iw - x);
    let h = h.min(ih - y);
    let sub = img.crop_imm(x, y, w, h);
    let mut out = Vec::new();
    sub.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png)
        .map_err(|e| format!("encode png: {e}"))?;
    Ok((out, w, h))
}

fn action_result(
    action: &str,
    result: std::result::Result<Vec<Output>, String>,
    received: Option<serde_json::Value>,
) -> ActionOutput {
    match result {
        Ok(_) => ActionOutput {
            ok: true,
            implemented: true,
            action: action.to_string(),
            message: if action == "type_text" {
                "Action sent through ydotool using US physical key positions. The active keyboard layout can change the resulting text; verify the field contents."
            } else {
                "Action sent through ydotool."
            }
            .to_string(),
            received,
        },
        Err(message) => ActionOutput {
            ok: false,
            implemented: true,
            action: action.to_string(),
            message,
            received,
        },
    }
}

fn unsafe_pointer_fallback(action: &str, received: Option<serde_json::Value>) -> ActionOutput {
    ActionOutput {
        ok: false,
        implemented: true,
        action: action.to_string(),
        message: "Coordinate input was not sent: ydotool cannot position the pointer accurately on Wayland. Enable the uinput absolute pointer with working screenshot capture, or use COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER=1 with an approved desktop portal session. Remove COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER if set.".to_string(),
        received,
    }
}

fn portal_start_error(
    action: &str,
    error: anyhow::Error,
    received: Option<serde_json::Value>,
) -> ActionOutput {
    ActionOutput {
        ok: false,
        implemented: true,
        action: action.to_string(),
        message: format!("Input was not sent because the remote desktop portal could not start; input was not replayed through ydotool: {error:#}"),
        received,
    }
}

fn abs_pointer_error(
    action: &str,
    error: String,
    received: Option<serde_json::Value>,
) -> ActionOutput {
    ActionOutput {
        ok: false,
        implemented: true,
        action: action.to_string(),
        message: format!("Absolute pointer {action} may have started before it failed; input was not replayed through another backend: {error}"),
        received,
    }
}

fn abs_pointer_result(
    action: &str,
    result: std::result::Result<crate::abs_pointer::PointerLanding, String>,
    received: Option<serde_json::Value>,
) -> ActionOutput {
    match result {
        Ok(landing) => with_notes(
            ActionOutput {
                ok: true,
                implemented: true,
                action: action.to_string(),
                message: if action == "scroll" {
                    "Pointer positioned through uinput; wheel input sent through ydotool."
                        .to_string()
                } else {
                    "Action sent through the uinput absolute pointer.".to_string()
                },
                received,
            },
            abs_pointer_clamp_note(landing),
        ),
        Err(error) => abs_pointer_error(action, error, received),
    }
}

fn portal_action_error(
    action: &str,
    error: anyhow::Error,
    received: Option<serde_json::Value>,
) -> ActionOutput {
    ActionOutput {
        ok: false,
        implemented: true,
        action: action.to_string(),
        message: format!(
            "Remote desktop portal {action} may have started before it failed; input was not replayed through another backend: {error:#}"
        ),
        received,
    }
}

fn portal_coordinate_error(action: &str, received: Option<serde_json::Value>) -> ActionOutput {
    ActionOutput {
        ok: false,
        implemented: true,
        action: action.to_string(),
        message: format!(
            "Remote desktop portal {action} was not sent because the coordinate could not be mapped safely to the complete shared desktop; input was not replayed through another backend."
        ),
        received,
    }
}

fn action_result_with_focus(
    action: &str,
    result: std::result::Result<Vec<Output>, String>,
    received: Option<serde_json::Value>,
    focus: Option<WindowFocusResult>,
) -> ActionOutput {
    with_focus_context(action_result(action, result, received), focus)
}

fn successful_action_with_focus(
    action: &str,
    message: &str,
    received: Option<serde_json::Value>,
    focus: Option<WindowFocusResult>,
) -> ActionOutput {
    with_focus_context(
        ActionOutput {
            ok: true,
            implemented: true,
            action: action.to_string(),
            message: message.to_string(),
            received,
        },
        focus,
    )
}

fn with_focus_context(mut output: ActionOutput, focus: Option<WindowFocusResult>) -> ActionOutput {
    if output.ok {
        if let Some(focus) = focus {
            let verification = if focus.exact_window_focused {
                "exact window-focus"
            } else {
                "app-level focus"
            };
            output.message = format!(
                "{} Target window_id {} was focused with {verification} verification before input.",
                output.message, focus.requested_window.window_id,
            );
        }
    }
    output
}

/// Post-input focus feedback. Only a complete search that finds nothing is a
/// warning; a search that hit a limit, or an app without an AT-SPI tree, cannot
/// tell whether the input landed, so it says that instead.
fn focus_probe_feedback(probe: &FocusProbe, expects_editable: bool) -> String {
    match probe {
        FocusProbe::Found(element) => describe_focused_element(element, expects_editable),
        FocusProbe::NoneFocused => "WARNING: AT-SPI reports no focused element in the target app; the input may have landed nowhere.".to_string(),
        FocusProbe::Incomplete => "Focused-element feedback unavailable: the focused element was not reached within the AT-SPI search limits, so the input could not be verified. Its absence here does not mean the input was lost.".to_string(),
        FocusProbe::NoAccessibleApp => "Focused-element feedback unavailable: the target app exposes no AT-SPI tree, so the input could not be verified. Electron apps need --force-renderer-accessibility.".to_string(),
    }
}

fn node_target_mismatch_message(element_index: u32, owner_pid: u32, target_pid: u32) -> String {
    format!(
        "element_index {element_index} comes from the latest get_app_state snapshot of pid {owner_pid}, not the target pid {target_pid}. Call get_app_state for the target app and use an index from that result."
    )
}

fn focus_target_pid(focus: Option<&WindowFocusResult>) -> Option<u32> {
    let focus = focus?;
    focus
        .focused_window
        .as_ref()
        .and_then(|window| window.pid)
        .or(focus.requested_window.pid)
}

fn describe_focused_element(element: &FocusedElementSummary, expects_editable: bool) -> String {
    let name = element
        .name
        .as_deref()
        .filter(|name| !name.is_empty())
        .map(|name| format!(" \"{name}\""))
        .unwrap_or_default();
    if element.editable {
        format!("Focused element: {}{name} (editable).", element.role)
    } else if expects_editable {
        format!(
            "WARNING: focused element is {}{name}, which is not editable — the typed text likely went nowhere. Click the intended input first or use set_value.",
            element.role
        )
    } else {
        format!("Focused element: {}{name} (not editable).", element.role)
    }
}

fn first_line(text: &str) -> &str {
    text.lines().next().unwrap_or(text)
}

/// Append supplemental notes (off-screen or focused-element feedback) to an
/// action result message without changing ok/implemented semantics.
fn with_notes(mut output: ActionOutput, notes: impl IntoIterator<Item = String>) -> ActionOutput {
    for note in notes {
        output.message = format!("{} {note}", output.message);
    }
    output
}

fn abs_pointer_clamp_note(landing: crate::abs_pointer::PointerLanding) -> Option<String> {
    (landing.requested != landing.emitted).then(|| {
        format!(
            "Requested coordinate {},{} was clamped to {},{} by the uinput absolute pointer.",
            landing.requested.0, landing.requested.1, landing.emitted.0, landing.emitted.1
        )
    })
}

fn focus_satisfies_target(focus: &WindowFocusResult, target: &WindowTarget) -> bool {
    if target.requires_exact_focus() {
        focus.exact_window_focused
    } else {
        focus.exact_window_focused || focus.app_focused
    }
}

async fn window_list_output() -> ListWindowsOutput {
    match list_windows().await {
        Ok(windows) => {
            let backend = window_backend(windows.iter());
            let note = registry::list_note(&backend);
            ListWindowsOutput {
                backend,
                windows,
                error: None,
                permissions_hint: None,
                note: note.to_string(),
            }
        }
        Err(error) => {
            let error = format!("{error:#}");
            ListWindowsOutput {
                backend: GNOME_SHELL_INTROSPECT_BACKEND.to_string(),
                windows: Vec::new(),
                permissions_hint: window_permission_hint(&error),
                error: Some(error),
                note: "Window listing failed, so targeted keyboard input cannot safely focus or verify a target window."
                    .to_string(),
            }
        }
    }
}

fn window_backend<'a>(windows: impl Iterator<Item = &'a WindowInfo>) -> String {
    windows
        .map(|window| window.backend.clone())
        .next()
        .unwrap_or_else(|| GNOME_SHELL_INTROSPECT_BACKEND.to_string())
}

fn absolute_mousemove_args(x: i32, y: i32) -> Vec<String> {
    vec![
        "mousemove".to_string(),
        "--absolute".to_string(),
        "--".to_string(),
        x.to_string(),
        y.to_string(),
    ]
}

fn xdotool_pointer_click_args(
    x: i32,
    y: i32,
    count: u32,
    button: Option<&str>,
) -> Option<Vec<String>> {
    let button = xdotool_pointer_button_code(button)?;
    Some(vec![
        "mousemove".to_string(),
        "--".to_string(),
        x.to_string(),
        y.to_string(),
        "click".to_string(),
        "--repeat".to_string(),
        count.to_string(),
        button.to_string(),
    ])
}

fn xdotool_pointer_button_code(button: Option<&str>) -> Option<&'static str> {
    match button.unwrap_or("left").to_ascii_lowercase().as_str() {
        "left" => Some("1"),
        "middle" => Some("2"),
        "right" => Some("3"),
        _ => None,
    }
}

/// Gap between XTEST wheel-button clicks, in milliseconds.
///
/// xdotool's own default is 100ms. One page is five clicks and valid requests
/// can ask for up to 100 pages, so 12ms keeps the bounded operation responsive
/// while matching the pacing used by `xdotool type`.
const XDOTOOL_SCROLL_CLICK_DELAY_MS: u64 = 12;

/// X11 wheel buttons: 4 up, 5 down, 6 left, 7 right. `repeat` is the notch
/// count, the same number ydotool would pass to `mousemove --wheel`.
fn xdotool_pointer_scroll_args(
    point: Option<(i32, i32)>,
    repeat: u32,
    direction: ScrollDirection,
) -> Vec<String> {
    let button = match direction {
        ScrollDirection::Up => "4",
        ScrollDirection::Down => "5",
        ScrollDirection::Left => "6",
        ScrollDirection::Right => "7",
    };
    let mut args = Vec::new();
    if let Some((x, y)) = point {
        args.extend([
            "mousemove".to_string(),
            "--".to_string(),
            x.to_string(),
            y.to_string(),
        ]);
    }
    args.extend([
        "click".to_string(),
        "--delay".to_string(),
        XDOTOOL_SCROLL_CLICK_DELAY_MS.to_string(),
        "--repeat".to_string(),
        repeat.max(1).to_string(),
        button.to_string(),
    ]);
    args
}

/// `xdotool click --repeat` sleeps `--delay` after every click, including
/// the last, so the command timeout has to cover that gap.
fn xdotool_scroll_timeout(repeat: u32, delay_ms: u64) -> Duration {
    INPUT_COMMAND_TIMEOUT.saturating_add(Duration::from_millis(
        u64::from(repeat.max(1)).saturating_mul(delay_ms),
    ))
}

#[derive(Debug)]
struct PointerCommandResult {
    outputs: Vec<Output>,
    backend: KeyboardCommandBackend,
}

async fn run_xdotool_pointer_or_fallback<F, Fut>(
    program: &Path,
    args: &[String],
    fallback: F,
) -> std::result::Result<PointerCommandResult, String>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = std::result::Result<Vec<Output>, String>>,
{
    match run_xdotool(program, args).await {
        XdotoolAttempt::Unavailable => fallback().await.map(|outputs| PointerCommandResult {
            outputs,
            backend: KeyboardCommandBackend::Ydotool,
        }),
        XdotoolAttempt::Finished(result) => result.map(|output| PointerCommandResult {
            outputs: vec![output],
            backend: KeyboardCommandBackend::Xdotool,
        }),
    }
}

async fn run_xdotool_pointer_or_fallback_with_timeout<F, Fut>(
    program: &Path,
    args: &[String],
    command_timeout: Duration,
    fallback: F,
) -> std::result::Result<PointerCommandResult, String>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = std::result::Result<Vec<Output>, String>>,
{
    match run_xdotool_with_timeout(program, args, command_timeout).await {
        XdotoolAttempt::Unavailable => fallback().await.map(|outputs| PointerCommandResult {
            outputs,
            backend: KeyboardCommandBackend::Ydotool,
        }),
        XdotoolAttempt::Finished(result) => result.map(|output| PointerCommandResult {
            outputs: vec![output],
            backend: KeyboardCommandBackend::Xdotool,
        }),
    }
}

fn wheel_mousemove_args(dx: i32, dy: i32) -> Vec<String> {
    vec![
        "mousemove".to_string(),
        "--wheel".to_string(),
        "--".to_string(),
        dx.to_string(),
        dy.to_string(),
    ]
}

async fn run_ydotool_sequence(
    commands: &[Vec<String>],
) -> std::result::Result<Vec<Output>, String> {
    let mut outputs = Vec::new();
    for (index, args) in commands.iter().enumerate() {
        outputs.push(run_ydotool(args).await?);
        if index + 1 < commands.len() {
            sleep(Duration::from_millis(35)).await;
        }
    }
    Ok(outputs)
}

async fn run_ydotool_drag(
    start_x: i32,
    start_y: i32,
    end_x: i32,
    end_y: i32,
) -> std::result::Result<Vec<Output>, String> {
    let mut outputs = vec![run_ydotool(&absolute_mousemove_args(start_x, start_y)).await?];
    sleep(Duration::from_millis(35)).await;

    let mut first_error = match run_ydotool(&["click".to_string(), "0x40".to_string()]).await {
        Ok(output) => {
            outputs.push(output);
            None
        }
        Err(error) => Some(error),
    };
    sleep(Duration::from_millis(35)).await;

    if first_error.is_none() {
        match run_ydotool(&absolute_mousemove_args(end_x, end_y)).await {
            Ok(output) => outputs.push(output),
            Err(error) => first_error = Some(error),
        }
        sleep(Duration::from_millis(35)).await;
    }

    let release = run_ydotool(&["click".to_string(), "0x80".to_string()]).await;
    match (first_error, release) {
        (None, Ok(output)) => {
            outputs.push(output);
            Ok(outputs)
        }
        (Some(error), Ok(_)) => Err(error),
        (None, Err(error)) => Err(error),
        (Some(error), Err(release_error)) => Err(format!(
            "{error}; ydotool button release also failed: {release_error}"
        )),
    }
}

async fn run_abs_pointer<T, F>(
    pointer: Arc<Mutex<Option<crate::abs_pointer::AbsPointer>>>,
    operation: F,
) -> std::result::Result<T, String>
where
    T: Send + 'static,
    F: FnOnce(&mut crate::abs_pointer::AbsPointer) -> Result<T> + Send + 'static,
{
    tokio::task::spawn_blocking(move || {
        let mut guard = pointer
            .lock()
            .map_err(|_| "absolute pointer lock was poisoned".to_string())?;
        let pointer = guard
            .as_mut()
            .ok_or_else(|| "absolute pointer is unavailable".to_string())?;
        operation(pointer).map_err(|error| format!("{error:#}"))
    })
    .await
    .map_err(|error| format!("absolute pointer task failed: {error}"))?
}

async fn run_cancellation_safe_input<T, F>(
    input_guard: tokio::sync::OwnedMutexGuard<()>,
    operation: F,
) -> (
    Option<tokio::sync::OwnedMutexGuard<()>>,
    std::result::Result<T, String>,
)
where
    T: Send + 'static,
    F: Future<Output = std::result::Result<T, String>> + Send + 'static,
{
    // Dropping a JoinHandle detaches its task, retaining the guard until the
    // stateful input operation has completed even if the caller is cancelled.
    match tokio::spawn(async move { (input_guard, operation.await) }).await {
        Ok((input_guard, result)) => (Some(input_guard), result),
        Err(error) => (None, Err(format!("stateful input task failed: {error}"))),
    }
}

async fn run_ydotool(args: &[String]) -> std::result::Result<Output, String> {
    let support = ydotool::ensure_supported_async().await?;
    let socket = ydotool::socket_path_for_command()?;
    let mut command = TokioCommand::new(&support.executable);
    command.args(args);
    command.env("YDOTOOL_SOCKET", socket);
    let output =
        crate::command_runner::output_with_timeout(command, "run ydotool", INPUT_COMMAND_TIMEOUT)
            .await
            .map_err(|error| format!("{error:#}"))?;
    if output.status.success() {
        if let Some(error) = ydotool::cli_error(&output.stderr) {
            Err(error)
        } else {
            Ok(output)
        }
    } else {
        Err(ydotool_output_error(output))
    }
}

async fn run_ydotool_type_text(text: &str) -> std::result::Result<Output, String> {
    validate_ydotool_text(text)?;
    let support = ydotool::ensure_supported_async().await?;
    let socket = ydotool::socket_path_for_command()?;
    let mut command = TokioCommand::new(&support.executable);
    command.args(["type", "--file", "-"]);
    command.env("YDOTOOL_SOCKET", socket);
    let output = crate::command_runner::output_with_stdin(
        command,
        "run ydotool type",
        ydotool_type_timeout(text),
        text.as_bytes().to_vec(),
    )
    .await
    .map_err(|error| format!("{error:#}"))?;
    if output.status.success() {
        if let Some(error) = ydotool::cli_error(&output.stderr) {
            Err(error)
        } else {
            Ok(output)
        }
    } else {
        Err(ydotool_output_error(output))
    }
}

fn validate_ydotool_text(text: &str) -> std::result::Result<(), String> {
    if let Some(ch) = text
        .chars()
        .find(|ch| !matches!(ch, '\t' | '\n' | ' '..='~'))
    {
        return Err(format!(
            "ydotool cannot type U+{:04X}; no text was sent. Use a layout-aware text backend or set_value on an editable field.",
            ch as u32
        ));
    }
    Ok(())
}

fn ydotool_type_timeout(text: &str) -> Duration {
    let text_seconds = (text.chars().count() as u64).div_ceil(YDOTOOL_TYPE_CHARS_PER_SECOND);
    Duration::from_secs(INPUT_COMMAND_TIMEOUT.as_secs().saturating_add(text_seconds))
}

const EVDEV_KEY_LEFTCTRL: i32 = 29;
const EVDEV_KEY_LEFTSHIFT: i32 = 42;
const EVDEV_KEY_V: i32 = 47;
const EVDEV_KEY_INSERT: i32 = 110;
const KDE_CLIPBOARD_RESTORE_MIN_DELAY_MS: u64 = 1_500;
const KDE_CLIPBOARD_RESTORE_MAX_DELAY_MS: u64 = 5_000;
const KDE_CLIPBOARD_RESTORE_CHARS_PER_SECOND: u64 = 250;

fn kde_clipboard_restore_delay(text: &str) -> Duration {
    let text_delay_ms = (text.chars().count() as u64)
        .saturating_mul(1_000)
        .div_ceil(KDE_CLIPBOARD_RESTORE_CHARS_PER_SECOND);
    Duration::from_millis(text_delay_ms.clamp(
        KDE_CLIPBOARD_RESTORE_MIN_DELAY_MS,
        KDE_CLIPBOARD_RESTORE_MAX_DELAY_MS,
    ))
}

#[derive(Debug)]
struct KdeClipboardPasteError {
    message: String,
    can_fallback_to_ydotool: bool,
    clear_portal_keyboard_session: bool,
}

impl KdeClipboardPasteError {
    fn before_text_input(message: String) -> Self {
        Self {
            message,
            can_fallback_to_ydotool: true,
            clear_portal_keyboard_session: false,
        }
    }

    fn after_portal_input(message: String) -> Self {
        Self {
            message,
            can_fallback_to_ydotool: false,
            clear_portal_keyboard_session: true,
        }
    }
}

async fn run_kde_clipboard_paste_text(
    session: &PortalKeyboardSession,
    text: &str,
    paste_shortcut: KdeClipboardPasteShortcut,
) -> std::result::Result<String, KdeClipboardPasteError> {
    let previous = kde_clipboard_contents()
        .await
        .map_err(KdeClipboardPasteError::before_text_input)?;
    kde_set_clipboard_contents(text)
        .await
        .map_err(KdeClipboardPasteError::before_text_input)?;

    let (modifiers, keycode) = kde_clipboard_paste_chord(paste_shortcut);
    let paste_result = press_keycode_chord(session, modifiers, keycode)
        .await
        .map_err(|error| format!("{error:#}"));

    sleep(kde_clipboard_restore_delay(text)).await;
    let restore_result = kde_set_clipboard_contents(&previous).await;

    match (paste_result, restore_result) {
        (Ok(_), Ok(_)) => Ok("Action pasted through KDE clipboard integration.".to_string()),
        (Err(error), Ok(_)) => Err(KdeClipboardPasteError::after_portal_input(error)),
        (Ok(_), Err(restore_error)) => Ok(format!(
            "Action pasted through KDE clipboard integration. Warning: previous KDE clipboard contents could not be restored: {restore_error}"
        )),
        (Err(error), Err(restore_error)) => Err(KdeClipboardPasteError::after_portal_input(
            format!("{error}; previous KDE clipboard contents could not be restored: {restore_error}"),
        )),
    }
}

/// Paste chord for KDE clipboard text input.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum KdeClipboardPasteShortcut {
    /// Ctrl+V, for ordinary widgets.
    Standard,
    /// Ctrl+Shift+V, for most terminal emulators.
    CtrlShiftV,
    /// Shift+Insert, for the xterm and rxvt families.
    ShiftInsert,
}

async fn kde_clipboard_paste_shortcut(
    target: &WindowTarget,
    focus: Option<&WindowFocusResult>,
) -> KdeClipboardPasteShortcut {
    let current = match focus {
        Some(_) => None,
        None => focused_window().await.ok().flatten(),
    };
    let window = focus
        .map(|focus| {
            focus
                .focused_window
                .as_ref()
                .unwrap_or(&focus.requested_window)
        })
        .or(current.as_ref());
    let terminal = kde_clipboard_terminal_shortcut(target, window);
    if terminal.is_none() {
        return KdeClipboardPasteShortcut::Standard;
    }
    // A terminal window can hold focus on a non-terminal widget (a search
    // field, a tab rename box) that pastes with plain Ctrl+V. Only an answer
    // from the terminal's own AT-SPI app may override the chord: without a pid,
    // or when the terminal exposes no AT-SPI root, keep the terminal chord.
    let Some(pid) = window.and_then(|window| window.pid) else {
        return kde_clipboard_shortcut_for_focus(terminal, None);
    };
    let focused_element = timeout(
        Duration::from_millis(1_500),
        focused_element_summary_in_app(pid),
    )
    .await
    .ok()
    .and_then(Result::ok)
    .flatten();
    kde_clipboard_shortcut_for_focus(terminal, focused_element.as_ref())
}

/// The terminal chord for the resolved window, or for an explicit terminal
/// selector when no window resolved. A resolved window always wins over a
/// possibly stale selector.
fn kde_clipboard_terminal_shortcut(
    target: &WindowTarget,
    window: Option<&WindowInfo>,
) -> Option<TerminalPasteShortcut> {
    match window {
        Some(window) => terminal_paste_shortcut(window),
        None => target
            .has_terminal_target()
            .then_some(TerminalPasteShortcut::CtrlShiftV),
    }
}

fn kde_clipboard_shortcut_for_focus(
    terminal: Option<TerminalPasteShortcut>,
    focused_element: Option<&FocusedElementSummary>,
) -> KdeClipboardPasteShortcut {
    // No AT-SPI answer (xterm exposes none) keeps the terminal chord; a
    // concrete non-terminal focused element switches back to Ctrl+V.
    if focused_element.is_some_and(|element| !element.is_terminal) {
        return KdeClipboardPasteShortcut::Standard;
    }
    match terminal {
        Some(TerminalPasteShortcut::CtrlShiftV) => KdeClipboardPasteShortcut::CtrlShiftV,
        Some(TerminalPasteShortcut::ShiftInsert) => KdeClipboardPasteShortcut::ShiftInsert,
        None => KdeClipboardPasteShortcut::Standard,
    }
}

fn kde_clipboard_paste_chord(shortcut: KdeClipboardPasteShortcut) -> (&'static [i32], i32) {
    match shortcut {
        KdeClipboardPasteShortcut::Standard => (&[EVDEV_KEY_LEFTCTRL], EVDEV_KEY_V),
        KdeClipboardPasteShortcut::CtrlShiftV => {
            (&[EVDEV_KEY_LEFTCTRL, EVDEV_KEY_LEFTSHIFT], EVDEV_KEY_V)
        }
        KdeClipboardPasteShortcut::ShiftInsert => (&[EVDEV_KEY_LEFTSHIFT], EVDEV_KEY_INSERT),
    }
}

async fn kde_clipboard_contents() -> std::result::Result<String, String> {
    let connection = kde_clipboard_connection().await?;
    let proxy = kde_clipboard_proxy(&connection).await?;
    let output: String = kde_clipboard_dbus_operation(
        "getClipboardContents",
        proxy.call("getClipboardContents", &()),
    )
    .await?;
    Ok(output)
}

async fn kde_set_clipboard_contents(text: &str) -> std::result::Result<(), String> {
    let connection = kde_clipboard_connection().await?;
    let proxy = kde_clipboard_proxy(&connection).await?;
    let _: () = kde_clipboard_dbus_operation(
        "setClipboardContents",
        proxy.call("setClipboardContents", &(text)),
    )
    .await?;
    Ok(())
}

async fn kde_clipboard_connection() -> std::result::Result<ZbusConnection, String> {
    ZbusConnection::session()
        .await
        .map_err(|error| format!("failed to connect to session bus for KDE clipboard: {error}"))
}

async fn kde_clipboard_proxy(
    connection: &ZbusConnection,
) -> std::result::Result<ZbusProxy<'_>, String> {
    kde_clipboard_dbus_operation(
        "proxy creation",
        ZbusProxy::new(
            connection,
            KDE_KLIPPER_SERVICE,
            KDE_KLIPPER_PATH,
            KDE_KLIPPER_INTERFACE,
        ),
    )
    .await
}

async fn kde_clipboard_dbus_operation<T, F>(
    operation: &'static str,
    future: F,
) -> std::result::Result<T, String>
where
    F: Future<Output = zbus::Result<T>>,
{
    kde_clipboard_dbus_operation_with_timeout(operation, future, KDE_CLIPBOARD_DBUS_TIMEOUT).await
}

async fn kde_clipboard_dbus_operation_with_timeout<T, F>(
    operation: &'static str,
    future: F,
    timeout_duration: Duration,
) -> std::result::Result<T, String>
where
    F: Future<Output = zbus::Result<T>>,
{
    timeout(timeout_duration, future)
        .await
        .map_err(|_| format!("KDE clipboard {operation} timed out"))?
        .map_err(|error| format!("KDE clipboard {operation} failed: {error}"))
}

fn ydotool_output_error(output: Output) -> String {
    command_output_error("ydotool", output)
}

/// X11 keyboard input runs through `xdotool` (XTEST) instead of ydotool.
///
/// ydotool injects raw evdev keycodes into a virtual uinput device. Under X11
/// the server re-interprets those scancodes through the active XKB layout, so
/// named keys and chords land as unrelated glyphs and literal text can mangle
/// symbols/digits (`_` → `%`, `1` → `+`). XTEST resolves keysyms against the
/// live layout, which is what X11 clients actually expect. See issue #58.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum KeyboardCommandBackend {
    Wtype,
    Xdotool,
    Ydotool,
}

struct KeyboardCommandResult {
    output: Output,
    backend: KeyboardCommandBackend,
}

enum XdotoolAttempt {
    Unavailable,
    Finished(std::result::Result<Output, String>),
}

async fn run_xdotool(program: &Path, args: &[String]) -> XdotoolAttempt {
    run_xdotool_with_timeout(program, args, INPUT_COMMAND_TIMEOUT).await
}

async fn run_xdotool_with_timeout(
    program: &Path,
    args: &[String],
    command_timeout: Duration,
) -> XdotoolAttempt {
    let mut command = TokioCommand::new(program);
    command.args(args);
    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());
    command.kill_on_drop(true);
    command.process_group(0);

    // A busy-file spawn error is transient, not "xdotool is unavailable"; do
    // not let it route input to the ydotool fallback.
    match crate::command_runner::spawn_retrying_busy(&mut command).await {
        Ok(child) => XdotoolAttempt::Finished(
            match crate::command_runner::output_child(child, "run xdotool", command_timeout)
                .await
                .map_err(|error| format!("{error:#}"))
            {
                Ok(output) if output.status.success() => Ok(output),
                Ok(output) => Err(command_output_error("xdotool", output)),
                Err(error) => Err(error),
            },
        ),
        Err(_) => XdotoolAttempt::Unavailable,
    }
}

async fn run_xdotool_or_fallback<F, Fut>(
    program: &Path,
    args: &[String],
    fallback: F,
) -> std::result::Result<KeyboardCommandResult, String>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = std::result::Result<Output, String>>,
{
    run_xdotool_or_fallback_with_timeout(program, args, INPUT_COMMAND_TIMEOUT, fallback).await
}

async fn run_xdotool_or_fallback_with_timeout<F, Fut>(
    program: &Path,
    args: &[String],
    command_timeout: Duration,
    fallback: F,
) -> std::result::Result<KeyboardCommandResult, String>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = std::result::Result<Output, String>>,
{
    match run_xdotool_with_timeout(program, args, command_timeout).await {
        XdotoolAttempt::Unavailable => fallback().await.map(|output| KeyboardCommandResult {
            output,
            backend: KeyboardCommandBackend::Ydotool,
        }),
        XdotoolAttempt::Finished(result) => result.map(|output| KeyboardCommandResult {
            output,
            backend: KeyboardCommandBackend::Xdotool,
        }),
    }
}

async fn run_wtype_type_text_or_fallback<F, Fut>(
    program: &Path,
    text: &str,
    fallback: F,
) -> std::result::Result<KeyboardCommandResult, String>
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = std::result::Result<Output, String>>,
{
    let available = if program.components().count() > 1 {
        std::fs::metadata(program)
            .map(|meta| meta.is_file())
            .unwrap_or(false)
    } else {
        program.to_str().is_some_and(which_in_path)
    };
    if !available {
        return fallback().await.map(|output| KeyboardCommandResult {
            output,
            backend: KeyboardCommandBackend::Ydotool,
        });
    }

    let mut command = TokioCommand::new(program);
    command.arg("-");
    let output = crate::command_runner::output_with_stdin(
        command,
        "run wtype",
        ydotool_type_timeout(text),
        text.as_bytes().to_vec(),
    )
    .await
    .map_err(|error| format!("{error:#}"))?;
    if !output.status.success() {
        return Err(command_output_error("wtype", output));
    }
    Ok(KeyboardCommandResult {
        output,
        backend: KeyboardCommandBackend::Wtype,
    })
}

/// True when `xdotool` can drive this session: an X11 session with `DISPLAY`
/// set and the binary present. `COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD=1`
/// opts out; `COMPUTER_USE_LINUX_FORCE_XDOTOOL_KEYBOARD=1` forces it on.
fn xdotool_available() -> bool {
    which_in_path("xdotool")
}

fn wtype_available() -> bool {
    which_in_path("wtype")
}

fn prefer_wtype_keyboard(
    force_ydotool: bool,
    is_wayland: bool,
    compatible_desktop: bool,
    available: bool,
) -> bool {
    !force_ydotool && is_wayland && compatible_desktop && available
}

/// Default per-character delay for `xdotool type`, in milliseconds (xdotool's
/// own default). `--delay 0` lets XTEST key events race each other on some X
/// servers (Cinnamon on Mint 22, issue #147): every character arrives, but in
/// a random order. A small delay keeps them ordered.
/// `COMPUTER_USE_LINUX_XDOTOOL_TYPE_DELAY_MS` overrides it.
const XDOTOOL_TYPE_DELAY_MS: u64 = 12;
const XDOTOOL_TYPE_DELAY_ENV: &str = "COMPUTER_USE_LINUX_XDOTOOL_TYPE_DELAY_MS";

fn xdotool_type_delay_ms() -> u64 {
    env::var(XDOTOOL_TYPE_DELAY_ENV)
        .ok()
        .and_then(|value| value.trim().parse::<u64>().ok())
        .unwrap_or(XDOTOOL_TYPE_DELAY_MS)
}

fn xdotool_type_args_with_delay(text: &str, delay_ms: u64) -> Vec<String> {
    vec![
        "type".to_string(),
        "--clearmodifiers".to_string(),
        "--delay".to_string(),
        delay_ms.to_string(),
        "--".to_string(),
        text.to_string(),
    ]
}

/// `xdotool type` spends about `delay_ms` per character, so long inputs need
/// more than the flat input timeout.
fn xdotool_type_timeout(text: &str, delay_ms: u64) -> Duration {
    let chars = text.chars().count() as u64;
    INPUT_COMMAND_TIMEOUT.saturating_add(Duration::from_millis(chars.saturating_mul(delay_ms)))
}

fn which_in_path(binary: &str) -> bool {
    let Ok(path) = env::var("PATH") else {
        return false;
    };
    env::split_paths(&path).any(|dir| {
        let candidate = dir.join(binary);
        std::fs::metadata(&candidate)
            .map(|meta| meta.is_file())
            .unwrap_or(false)
    })
}

/// Map our key grammar onto an `xdotool key` spec such as `ctrl+a`, `Return`,
/// or `shift+F5`. Returns `None` for keys the grammar does not accept, so the
/// caller keeps its existing "never silently dropped" error.
fn xdotool_key_spec(key: &str) -> Option<String> {
    let parts = key
        .split('+')
        .map(str::trim)
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>();
    let (key_part, modifier_parts) = parts.split_last()?;

    // Validate through the same evdev grammar so both backends accept exactly
    // the same input set.
    key_chord(key)?;

    let mut spec = Vec::new();
    for part in modifier_parts {
        spec.push(xdotool_modifier_name(part)?.to_string());
    }

    if modifier_parts.is_empty() {
        if let Some(bare) = xdotool_modifier_keysym(key_part) {
            return Some(bare.to_string());
        }
    }
    spec.push(xdotool_keysym_name(key_part)?);
    Some(spec.join("+"))
}

fn xdotool_modifier_name(key: &str) -> Option<&'static str> {
    match normalize_key(key).as_str() {
        "ctrl" | "control" => Some("ctrl"),
        "alt" | "option" => Some("alt"),
        "shift" => Some("shift"),
        "meta" | "super" | "cmd" | "command" => Some("super"),
        _ => None,
    }
}

/// Standalone keysym for a bare modifier press (`press_key "Super"`).
fn xdotool_modifier_keysym(key: &str) -> Option<&'static str> {
    match normalize_key(key).as_str() {
        "ctrl" | "control" => Some("ctrl"),
        "alt" | "option" => Some("alt"),
        "shift" => Some("shift"),
        "meta" | "super" | "cmd" | "command" => Some("super"),
        _ => None,
    }
}

fn xdotool_keysym_name(key: &str) -> Option<String> {
    let normalized = normalize_key(key);
    let named = match normalized.as_str() {
        "enter" | "return" => "Return",
        "escape" | "esc" => "Escape",
        "tab" => "Tab",
        "backspace" => "BackSpace",
        "delete" | "del" => "Delete",
        "space" => "space",
        "home" => "Home",
        "end" => "End",
        "pageup" | "page_up" => "Page_Up",
        "pagedown" | "page_down" => "Page_Down",
        "arrowleft" | "left" => "Left",
        "arrowright" | "right" => "Right",
        "arrowup" | "up" => "Up",
        "arrowdown" | "down" => "Down",
        "f1" => "F1",
        "f2" => "F2",
        "f3" => "F3",
        "f4" => "F4",
        "f5" => "F5",
        "f6" => "F6",
        "f7" => "F7",
        "f8" => "F8",
        "f9" => "F9",
        "f10" => "F10",
        "f11" => "F11",
        "f12" => "F12",
        value if value.len() == 1 && value.as_bytes()[0].is_ascii_alphanumeric() => {
            return Some(value.to_string());
        }
        _ => return None,
    };
    Some(named.to_string())
}

fn command_output_error(command: &str, output: Output) -> String {
    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let detail = if stderr.is_empty() { stdout } else { stderr };
    if detail.is_empty() {
        format!("{command} exited with {}", output.status)
    } else {
        detail
    }
}

async fn ydotool_backend_available() -> bool {
    ydotool_backend_available_from(
        ydotool::connectable_socket_path().is_ok(),
        ydotool::ensure_supported_async().await.is_ok(),
    )
}

fn ydotool_backend_available_from(socket_available: bool, cli_supported: bool) -> bool {
    socket_available && cli_supported
}

fn should_prefer_portal_backend_by_default(is_wayland: bool, ydotool_available: bool) -> bool {
    is_wayland && !ydotool_available
}

fn prefer_portal_text_backend(
    force_ydotool: bool,
    prefer_xdotool: bool,
    is_wayland: bool,
    is_kde: bool,
    force_portal: bool,
    prefer_wtype: bool,
) -> bool {
    !force_ydotool && !prefer_xdotool && is_wayland && !is_kde && (force_portal || !prefer_wtype)
}

fn mouse_button_code(button: Option<&str>) -> String {
    match button.unwrap_or("left").to_ascii_lowercase().as_str() {
        "right" => "0xC1",
        "middle" => "0xC2",
        "side" => "0xC3",
        "extra" => "0xC4",
        "forward" => "0xC5",
        "back" => "0xC6",
        _ => "0xC0",
    }
    .to_string()
}

/// Parse a chord like `Ctrl+Shift+P` into raw evdev codes: the held
/// modifiers plus the final key. A bare modifier (`Super`) parses as a
/// chord with no held modifiers whose key is the modifier itself.
fn key_chord(key: &str) -> Option<(Vec<u16>, u16)> {
    let parts = key
        .split('+')
        .map(str::trim)
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>();
    let (key_part, modifier_parts) = parts.split_last()?;
    if modifier_parts.is_empty() {
        if let Some(modifier) = modifier_keycode(key_part) {
            return Some((Vec::new(), modifier));
        }
    }
    let mut modifiers = Vec::new();
    for part in modifier_parts {
        modifiers.push(modifier_keycode(part)?);
    }
    let keycode = keycode(key_part)?;
    Some((modifiers, keycode))
}

/// Portal form of [`key_chord`]. Modifiers and named keys go out as keysyms
/// so the compositor resolves them against the active keymap: with Caps Lock
/// and Control swapped, `Ctrl` presses whichever key produces `Control_L`
/// (issue #191). Letters and digits stay physical keycodes: mutter drops a
/// keysym missing from the current layout group, so `Ctrl+A` as a keysym
/// would vanish under a non-Latin layout, while the US keycode is what GTK and
/// Qt match shortcuts against there.
fn portal_key_chord(key: &str) -> Option<(Vec<PortalKey>, PortalKey)> {
    let parts = key
        .split('+')
        .map(str::trim)
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>();
    let (key_part, modifier_parts) = parts.split_last()?;
    let (_, keycode) = key_chord(key)?;
    let mut modifiers = Vec::new();
    for part in modifier_parts {
        modifiers.push(PortalKey::Keysym(modifier_keysym(part)?));
    }
    let key = modifier_keysym(key_part)
        .filter(|_| modifier_parts.is_empty())
        .or_else(|| named_keysym(key_part))
        .map_or(PortalKey::Keycode(i32::from(keycode)), PortalKey::Keysym);
    Some((modifiers, key))
}

fn modifier_keysym(key: &str) -> Option<i32> {
    let keysym = match normalize_key(key).as_str() {
        "ctrl" | "control" => xkey::Control_L,
        "alt" | "option" => xkey::Alt_L,
        "shift" => xkey::Shift_L,
        "meta" | "super" | "cmd" | "command" => xkey::Super_L,
        _ => return None,
    };
    Some(keysym as i32)
}

fn named_keysym(key: &str) -> Option<i32> {
    let keysym = match normalize_key(key).as_str() {
        "enter" | "return" => xkey::Return,
        "escape" | "esc" => xkey::Escape,
        "tab" => xkey::Tab,
        "backspace" => xkey::BackSpace,
        "delete" | "del" => xkey::Delete,
        "space" => xkey::space,
        "home" => xkey::Home,
        "end" => xkey::End,
        "pageup" | "page_up" => xkey::Page_Up,
        "pagedown" | "page_down" => xkey::Page_Down,
        "arrowleft" | "left" => xkey::Left,
        "arrowright" | "right" => xkey::Right,
        "arrowup" | "up" => xkey::Up,
        "arrowdown" | "down" => xkey::Down,
        "f1" => xkey::F1,
        "f2" => xkey::F2,
        "f3" => xkey::F3,
        "f4" => xkey::F4,
        "f5" => xkey::F5,
        "f6" => xkey::F6,
        "f7" => xkey::F7,
        "f8" => xkey::F8,
        "f9" => xkey::F9,
        "f10" => xkey::F10,
        "f11" => xkey::F11,
        "f12" => xkey::F12,
        _ => return None,
    };
    Some(keysym as i32)
}

fn key_sequence(key: &str) -> Option<Vec<String>> {
    let (modifiers, keycode) = key_chord(key)?;
    let mut events = Vec::new();
    for modifier in &modifiers {
        events.push(format!("{modifier}:1"));
    }
    events.push(format!("{keycode}:1"));
    events.push(format!("{keycode}:0"));
    for modifier in modifiers.iter().rev() {
        events.push(format!("{modifier}:0"));
    }
    Some(events)
}

fn ydotool_key_args(key_events: Vec<String>, has_modifiers: bool) -> Vec<String> {
    let mut args = vec!["key".to_string()];
    if has_modifiers {
        args.extend(["-d".to_string(), "100".to_string()]);
    }
    args.extend(key_events);
    args
}

fn modifier_keycode(key: &str) -> Option<u16> {
    match normalize_key(key).as_str() {
        "ctrl" | "control" => Some(29),
        "alt" | "option" => Some(56),
        "shift" => Some(42),
        "meta" | "super" | "cmd" | "command" => Some(125),
        _ => None,
    }
}

fn keycode(key: &str) -> Option<u16> {
    match normalize_key(key).as_str() {
        "enter" | "return" => Some(28),
        "escape" | "esc" => Some(1),
        "tab" => Some(15),
        "backspace" => Some(14),
        "delete" | "del" => Some(111),
        "space" => Some(57),
        "home" => Some(102),
        "end" => Some(107),
        "pageup" | "page_up" => Some(104),
        "pagedown" | "page_down" => Some(109),
        "arrowleft" | "left" => Some(105),
        "arrowright" | "right" => Some(106),
        "arrowup" | "up" => Some(103),
        "arrowdown" | "down" => Some(108),
        "f1" => Some(59),
        "f2" => Some(60),
        "f3" => Some(61),
        "f4" => Some(62),
        "f5" => Some(63),
        "f6" => Some(64),
        "f7" => Some(65),
        "f8" => Some(66),
        "f9" => Some(67),
        "f10" => Some(68),
        "f11" => Some(87),
        "f12" => Some(88),
        value if value.len() == 1 => keycode_for_ascii(value.as_bytes()[0] as char),
        _ => None,
    }
}

fn normalize_key(key: &str) -> String {
    key.trim().to_ascii_lowercase().replace(['-', ' '], "")
}

fn keycode_for_ascii(value: char) -> Option<u16> {
    match value {
        'a' => Some(30),
        'b' => Some(48),
        'c' => Some(46),
        'd' => Some(32),
        'e' => Some(18),
        'f' => Some(33),
        'g' => Some(34),
        'h' => Some(35),
        'i' => Some(23),
        'j' => Some(36),
        'k' => Some(37),
        'l' => Some(38),
        'm' => Some(50),
        'n' => Some(49),
        'o' => Some(24),
        'p' => Some(25),
        'q' => Some(16),
        'r' => Some(19),
        's' => Some(31),
        't' => Some(20),
        'u' => Some(22),
        'v' => Some(47),
        'w' => Some(17),
        'x' => Some(45),
        'y' => Some(21),
        'z' => Some(44),
        '1' => Some(2),
        '2' => Some(3),
        '3' => Some(4),
        '4' => Some(5),
        '5' => Some(6),
        '6' => Some(7),
        '7' => Some(8),
        '8' => Some(9),
        '9' => Some(10),
        '0' => Some(11),
        _ => None,
    }
}

fn list_process_apps() -> Vec<AppCandidate> {
    let output = Command::new("ps")
        .args(["-eo", "pid=,comm=,args="])
        .output();
    let Ok(output) = output else {
        return Vec::new();
    };
    if !output.status.success() {
        return Vec::new();
    }

    String::from_utf8_lossy(&output.stdout)
        .lines()
        .filter_map(parse_process_line)
        .filter(|app| looks_like_desktop_app(&app.name, &app.command))
        .take(50)
        .collect()
}

fn parse_process_line(line: &str) -> Option<AppCandidate> {
    let trimmed = line.trim();
    let mut parts = trimmed.splitn(3, char::is_whitespace);
    let pid = parts.next()?.parse().ok()?;
    let name = parts.next()?.to_string();
    let command = parts.next().unwrap_or("").trim().to_string();
    Some(AppCandidate { name, pid, command })
}

fn looks_like_desktop_app(name: &str, command: &str) -> bool {
    let haystack = format!("{name} {command}").to_ascii_lowercase();
    [
        "codex",
        "electron",
        "chrome",
        "chromium",
        "firefox",
        "brave",
        "code",
        "gnome-terminal",
        "ptyxis",
        "kgx",
        "nautilus",
        "slack",
        "discord",
        "spotify",
        "obsidian",
    ]
    .iter()
    .any(|needle| haystack.contains(needle))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::atspi_tree::{AccessibilityAction, Bounds};
    use crate::windows::{WindowBounds, GNOME_SHELL_EXTENSION_BACKEND};
    use std::os::unix::fs::PermissionsExt;

    /// Restores an environment variable when a test ends, including on panic.
    struct EnvVarGuard {
        key: &'static str,
        original: Option<std::ffi::OsString>,
    }

    impl EnvVarGuard {
        fn set(key: &'static str, value: &str) -> Self {
            let original = std::env::var_os(key);
            std::env::set_var(key, value);
            Self { key, original }
        }

        fn unset(key: &'static str) -> Self {
            let original = std::env::var_os(key);
            std::env::remove_var(key);
            Self { key, original }
        }
    }

    impl Drop for EnvVarGuard {
        fn drop(&mut self) {
            match &self.original {
                Some(value) => std::env::set_var(self.key, value),
                None => std::env::remove_var(self.key),
            }
        }
    }

    /// The run_shell tests change the same process-wide variable, so they
    /// take turns instead of racing each other under the parallel test runner.
    static SHELL_ENV_TEST_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

    /// Exercise the real handlers in a separate process so backend overrides
    /// cannot race other tests. All command programs are harmless stubs; the
    /// private datagram socket and invalid D-Bus address cannot reach a desktop.
    #[test]
    fn pointer_coordinate_backend_dispatch_is_safe() {
        let dir = std::env::temp_dir().join(format!(
            "cul-pointer-safety-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir(&dir).unwrap();
        std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
        struct Cleanup(std::path::PathBuf);
        impl Drop for Cleanup {
            fn drop(&mut self) {
                let _ = std::fs::remove_dir_all(&self.0);
            }
        }
        let _cleanup = Cleanup(dir.clone());
        let socket_path = dir.join("input.sock");
        let _socket = std::os::unix::net::UnixDatagram::bind(&socket_path).unwrap();
        for program in ["ydotool", "xdotool", "systemctl", "gnome-screenshot"] {
            let path = dir.join(program);
            std::fs::write(
                &path,
                r#"#!/bin/sh
case "${0##*/}" in
  systemctl) exit 0 ;;
  gnome-screenshot) printf 'capture\n' >> "$CUL_POINTER_TEST_CAPTURE_LOG"; exit 1 ;;
  ydotool)
    case "$1" in help|--help) printf '%s\n' click mousemove type key debug; exit 0 ;; esac
    # Capability probes use their own socket. Record only dispatched input.
    [ "$YDOTOOL_SOCKET" = "$CUL_POINTER_TEST_SOCKET" ] || exit 0 ;;
esac
printf '%s %s\n' "${0##*/}" "$*" >> "$CUL_POINTER_TEST_LOG"
"#,
            )
            .unwrap();
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).unwrap();
        }
        for mode in [
            "wayland-default",
            "wayland-uncached",
            "wayland-ydotool",
            "wayland-portal",
            "x11",
        ] {
            let log = dir.join(format!("{mode}.log"));
            let capture_log = dir.join(format!("{mode}-capture.log"));
            let mut command = Command::new(std::env::current_exe().unwrap());
            command
                .args([
                    "--exact",
                    "server::tests::pointer_coordinate_backend_child",
                    "--nocapture",
                ])
                .env_clear()
                .env("PATH", &dir)
                .env("HOME", &dir)
                .env("CUL_POINTER_TEST_MODE", mode)
                .env("CUL_POINTER_TEST_LOG", &log)
                .env("CUL_POINTER_TEST_CAPTURE_LOG", &capture_log)
                .env("COMPUTER_USE_LINUX_SCREENSHOT_BACKEND", "gnome-screenshot")
                .env("CUL_POINTER_TEST_SOCKET", &socket_path)
                .env("YDOTOOL_SOCKET", &socket_path)
                .env("XDG_RUNTIME_DIR", &dir)
                .env(
                    "DBUS_SESSION_BUS_ADDRESS",
                    format!("unix:path={}/no-bus", dir.display()),
                )
                .env("DISPLAY", ":9876")
                .env(
                    "WAYLAND_DISPLAY",
                    if mode == "x11" { "" } else { "no-wayland" },
                )
                .env(
                    "XDG_SESSION_TYPE",
                    if mode == "x11" { "x11" } else { "wayland" },
                )
                .env("CU_DISABLE_ABS_POINTER", "1")
                .env(
                    "COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER",
                    if mode == "wayland-ydotool" { "1" } else { "0" },
                )
                .env(
                    "COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER",
                    if mode == "wayland-portal" { "1" } else { "0" },
                );
            for key in [
                "DESKTOP_SESSION",
                "HYPRLAND_INSTANCE_SIGNATURE",
                "XAUTHORITY",
                "XDG_SESSION_DESKTOP",
                "XDG_CURRENT_DESKTOP",
            ] {
                command.env(key, "pointer-test");
            }
            let output = crate::command_runner::output_blocking_with_timeout(
                &mut command,
                "pointer backend regression",
                Duration::from_secs(15),
            )
            .unwrap();
            assert!(
                output.status.success(),
                "{mode}: {}{}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(
                !capture_log.exists(),
                "{mode}: a pointer warning triggered screenshot capture"
            );
            let commands = std::fs::read_to_string(&log).unwrap_or_default();
            match mode {
                "wayland-portal" => assert!(commands.is_empty(), "{commands}"),
                "x11" => {
                    assert!(
                        commands.contains("xdotool mousemove -- 5536 700 click"),
                        "{commands}"
                    );
                    assert!(
                        commands.contains("ydotool mousemove --absolute -- 100 100"),
                        "{commands}"
                    );
                }
                _ => assert_eq!(commands, "ydotool mousemove --wheel -- 0 -5\n"),
            }
        }
    }

    #[tokio::test]
    async fn pointer_coordinate_backend_child() {
        let Ok(mode) = std::env::var("CUL_POINTER_TEST_MODE") else {
            return;
        };
        let backend = ComputerUseLinux::default();
        if mode != "wayland-uncached" {
            backend.cache_desktop_size(9376, 1600);
            assert!(backend.off_screen_note_for_point(9376, 700).await.is_some());
        }
        assert!(backend.off_screen_note_for_point(5536, 700).await.is_none());
        let click: ClickParams =
            serde_json::from_value(serde_json::json!({"x":5536,"y":700})).unwrap();
        let scroll: ScrollParams =
            serde_json::from_value(serde_json::json!({"x":5536,"y":700,"direction":"down"}))
                .unwrap();
        let drag = DragParams {
            start_x: 100,
            start_y: 100,
            end_x: 200,
            end_y: 200,
        };
        let results = [
            backend.click(Parameters(click)).await.0,
            backend.scroll(Parameters(scroll)).await.0,
            backend.drag(Parameters(drag)).await.0,
        ];
        for result in results {
            if mode == "x11" {
                assert!(result.ok, "{}", result.message);
            } else {
                assert!(!result.ok, "{}", result.message);
                let expected = if mode == "wayland-portal" {
                    "portal could not start"
                } else {
                    "cannot position the pointer accurately on Wayland"
                };
                assert!(result.message.contains(expected), "{}", result.message);
            }
        }
        let scroll: ScrollParams =
            serde_json::from_value(serde_json::json!({"direction":"down"})).unwrap();
        let result = backend.scroll(Parameters(scroll)).await.0;
        assert_eq!(result.ok, mode != "wayland-portal", "{}", result.message);
    }

    #[test]
    fn completion_tool_is_explicitly_opt_in_and_has_side_effect_annotations() {
        let server = ComputerUseLinux::default();
        assert!(!server
            .router_with_completion(false)
            .list_all()
            .iter()
            .any(|t| t.name == "complete_interaction"));
        let tools = server.router_with_completion(true).list_all();
        let tool = tools
            .iter()
            .find(|t| t.name == "complete_interaction")
            .unwrap();
        let value = serde_json::to_value(tool).unwrap();
        assert_eq!(value["annotations"]["readOnlyHint"], false);
        assert_eq!(value["annotations"]["destructiveHint"], false);
        assert_eq!(value["annotations"]["idempotentHint"], false);
        assert_eq!(value["annotations"]["openWorldHint"], true);
    }

    #[tokio::test]
    async fn completion_notification_handles_missing_and_failed_backends() {
        for path in ["/nonexistent/computer-use-notify", "/bin/false"] {
            let result =
                send_completion_notification(Path::new(path), Duration::from_secs(2)).await;
            assert!(result.ok);
            assert_eq!(result.cue, "skipped");
        }
        let result =
            send_completion_notification(Path::new("/bin/true"), Duration::from_secs(2)).await;
        assert_eq!(result.cue, "notification");
    }

    #[tokio::test]
    async fn completion_notification_timeout_is_bounded() {
        let dir = std::env::temp_dir().join(format!(
            "computer-use-notify-test-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let program = dir.join("notify");
        std::fs::write(&program, "#!/bin/sh\nexec sleep 30\n").unwrap();
        std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o700)).unwrap();
        let started = std::time::Instant::now();
        let output = send_completion_notification(&program, Duration::from_millis(30)).await;
        assert_eq!(output.cue, "skipped");
        assert!(started.elapsed() < Duration::from_secs(3));
        std::fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn exported_tool_schemas_omit_unsigned_integer_formats() {
        let tools = ComputerUseLinux::default().mcp_tool_router().list_all();
        let value = serde_json::to_value(tools).unwrap();
        let mut unsupported = Vec::new();
        collect_unsigned_integer_formats(&value, "$", &mut unsupported);

        assert!(
            unsupported.is_empty(),
            "unsupported unsigned integer formats: {unsupported:?}"
        );
    }

    fn collect_unsigned_integer_formats(
        value: &serde_json::Value,
        path: &str,
        unsupported: &mut Vec<String>,
    ) {
        match value {
            serde_json::Value::Object(object) => {
                if matches!(
                    object.get("format").and_then(serde_json::Value::as_str),
                    Some("uint" | "uint8" | "uint16" | "uint32" | "uint64" | "usize")
                ) {
                    unsupported.push(path.to_string());
                }
                for (key, nested) in object {
                    collect_unsigned_integer_formats(nested, &format!("{path}/{key}"), unsupported);
                }
            }
            serde_json::Value::Array(items) => {
                for (index, nested) in items.iter().enumerate() {
                    collect_unsigned_integer_formats(
                        nested,
                        &format!("{path}/{index}"),
                        unsupported,
                    );
                }
            }
            _ => {}
        }
    }

    fn node(index: u32, bounds: Option<Bounds>) -> AccessibilityNode {
        node_with_actions(index, bounds, Vec::new())
    }

    fn node_with_actions(
        index: u32,
        bounds: Option<Bounds>,
        actions: Vec<AccessibilityAction>,
    ) -> AccessibilityNode {
        AccessibilityNode {
            index,
            parent_index: None,
            depth: 0,
            object_ref: format!(":1.{index}/org/a11y/atspi/accessible/{index}"),
            role: "push button".to_string(),
            name: Some(format!("Button {index}")),
            description: None,
            child_count: 0,
            bounds,
            states: Vec::new(),
            actions,
            value: None,
            text: None,
            supports_editable_text: false,
        }
    }

    fn click_action() -> AccessibilityAction {
        AccessibilityAction {
            index: 0,
            name: "Click".to_string(),
            description: "Clicks the element".to_string(),
            keybinding: String::new(),
        }
    }

    fn solid_png(width: u32, height: u32) -> Vec<u8> {
        let img = image::RgbaImage::from_pixel(width, height, image::Rgba([32, 128, 192, 255]));
        let mut out = Vec::new();
        image::DynamicImage::ImageRgba8(img)
            .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
            .unwrap();
        out
    }

    #[test]
    fn targeted_app_state_crops_before_screenshot_payload_resize() {
        let raw = RawScreenshotCapture {
            mime_type: "image/png".to_string(),
            bytes: solid_png(400, 200),
            source: "test".to_string(),
            width: 400,
            height: 200,
        };
        let capture = prepare_app_state_screenshot(
            raw,
            Some((50, 20, 200, 100)),
            true,
            ScreenshotPayloadOptions {
                max_width: Some(100),
                max_height: Some(100),
                max_bytes: Some(1024 * 1024),
                ..Default::default()
            },
        )
        .unwrap();

        assert_eq!(
            (capture.coordinate_width, capture.coordinate_height),
            (200, 100)
        );
        assert_eq!((capture.width, capture.height), (100, 50));
    }

    #[test]
    fn unresolved_app_state_target_refuses_full_desktop_screenshot() {
        let raw = RawScreenshotCapture {
            mime_type: "image/png".to_string(),
            bytes: solid_png(400, 200),
            source: "test".to_string(),
            width: 400,
            height: 200,
        };

        let error =
            prepare_app_state_screenshot(raw, None, true, ScreenshotPayloadOptions::default())
                .unwrap_err();

        assert!(error.to_string().contains("requires a resolved window"));
    }

    #[test]
    fn targeted_app_state_crops_only_visible_part_of_offscreen_window() {
        let raw = RawScreenshotCapture {
            mime_type: "image/png".to_string(),
            bytes: solid_png(400, 200),
            source: "test".to_string(),
            width: 400,
            height: 200,
        };
        let capture = prepare_app_state_screenshot(
            raw,
            Some((-50, -40, 100, 100)),
            true,
            ScreenshotPayloadOptions::default(),
        )
        .unwrap();

        assert_eq!(
            (capture.coordinate_width, capture.coordinate_height),
            (50, 60)
        );
    }

    fn sample_app_state_output(screenshot: Option<&ScreenshotCapture>) -> GetAppStateOutput {
        GetAppStateOutput {
            app_name_or_bundle_identifier: None,
            window_context: None,
            window_error: None,
            window_permissions_hint: None,
            backend: "linux-atspi".to_string(),
            screenshot: screenshot.map(ScreenshotSummary::from),
            screenshot_error: None,
            accessibility_tree: Vec::new(),
            accessibility_tree_raw_count: 0,
            tree_scoped: true,
            accessibility_tree_truncated: false,
            accessibility_error: None,
            readiness: crate::diagnostics::ReadinessReport {
                can_register_mcp_tools: true,
                can_build_accessibility_tree: true,
                can_query_windows: true,
                can_focus_apps: true,
                can_focus_windows: true,
                can_send_development_input: true,
                can_capture_screenshots: true,
                screenshot_capture_status: crate::diagnostics::ScreenshotCaptureStatus::Verified,
                recommended_next_step: String::new(),
                blockers: Vec::new(),
                warnings: Vec::new(),
            },
            diagnostics: None,
            message: "ok".to_string(),
        }
    }

    #[test]
    fn app_state_result_emits_screenshot_as_image_block_not_inline_base64() {
        // Issue #145: the base64 payload must ride a structured image block so
        // hosts count it as vision tokens instead of a 100KB text blob.
        let raw = RawScreenshotCapture {
            mime_type: "image/png".to_string(),
            bytes: solid_png(64, 32),
            source: "test".to_string(),
            width: 64,
            height: 32,
        };
        let capture =
            prepare_app_state_screenshot(raw, None, false, ScreenshotPayloadOptions::default())
                .unwrap();
        let payload = data_url_payload(&capture.data_url);
        let output = sample_app_state_output(Some(&capture));

        let result = app_state_result(output, Some(capture)).unwrap();

        assert_eq!(result.content.len(), 2);
        let image = result.content[0]
            .as_image()
            .expect("first block is an image");
        assert_eq!(image.mime_type, "image/png");
        assert_eq!(image.data, payload);
        let text = &result.content[1]
            .as_text()
            .expect("second block is text")
            .text;
        assert!(
            !text.contains(&payload),
            "base64 leaked into the text block"
        );
        assert!(!text.contains("data_url"));
        let structured = result.structured_content.expect("structured content");
        assert_eq!(structured["screenshot"]["width"], 64);
        assert_eq!(structured["screenshot"]["mime_type"], "image/png");
        assert!(structured["screenshot"].get("data_url").is_none());
    }

    #[test]
    fn app_state_result_without_screenshot_is_text_only() {
        let result = app_state_result(sample_app_state_output(None), None).unwrap();
        assert_eq!(result.content.len(), 1);
        assert!(result.content[0].as_text().is_some());
        assert_eq!(
            result.structured_content.unwrap()["screenshot"],
            serde_json::Value::Null
        );
    }

    #[test]
    fn gnome_window_crop_scales_logical_bounds_to_capture_pixels() {
        let bounds = WindowBounds {
            x: Some(6),
            y: Some(36),
            width: 1357,
            height: 1144,
        };
        let monitors = [(0, 0, 1920, 1200)];

        assert_eq!(
            logical_window_crop_rect(&bounds, &monitors, 2560, 1600).unwrap(),
            (8, 48, 1810, 1526)
        );
    }

    #[test]
    fn portal_points_map_capture_pixels_back_to_logical_window_space() {
        let scaled = WindowCoordinateMap {
            capture_rect: (8, 48, 1810, 1526),
            full_capture_rect: (8, 48, 1810, 1526),
            portal_rect: Some((6, 36, 1357, 1144)),
        };
        assert_eq!(scaled.portal_point(913, 811), Some((684, 608)));

        let clipped = WindowCoordinateMap {
            capture_rect: (0, 0, 50, 60),
            full_capture_rect: (-50, -40, 100, 100),
            portal_rect: Some((-50, -40, 100, 100)),
        };
        assert_eq!(clipped.portal_point(0, 0), Some((0, 0)));
    }

    #[test]
    fn scaled_kwin_bounds_keep_capture_and_portal_spaces_distinct() {
        let bounds = WindowBounds {
            x: Some(1000),
            y: Some(100),
            width: 800,
            height: 600,
        };
        let logical_rect = window_crop_rect(&bounds).unwrap();
        let full_capture_rect =
            logical_window_crop_rect(&bounds, &[(0, 0, 1920, 1080)], 3840, 2160).unwrap();
        let mapping = WindowCoordinateMap {
            capture_rect: full_capture_rect,
            full_capture_rect,
            portal_rect: Some(logical_rect),
        };

        assert_eq!(mapping.capture_rect, (2000, 200, 1600, 1200));
        assert_eq!(mapping.portal_point(2400, 500), Some((1200, 250)));
    }

    #[test]
    fn kwin_mapping_uses_the_workspace_geometry_origin() {
        let bounds = WindowBounds {
            x: Some(1100),
            y: Some(50),
            width: 800,
            height: 600,
        };
        let logical_rect = window_crop_rect(&bounds).unwrap();
        let full_capture_rect =
            logical_window_crop_rect(&bounds, &[(100, -50, 1920, 1080)], 3840, 2160).unwrap();
        let mapping = WindowCoordinateMap {
            capture_rect: full_capture_rect,
            full_capture_rect,
            portal_rect: Some(logical_rect),
        };

        assert_eq!(mapping.capture_rect, (2000, 200, 1600, 1200));
        assert_eq!(mapping.portal_point(2400, 500), Some((1300, 200)));
    }

    #[test]
    fn gnome_window_crop_accounts_for_negative_monitor_origins() {
        let bounds = WindowBounds {
            x: Some(-900),
            y: Some(100),
            width: 400,
            height: 300,
        };
        let monitors = [(-1000, 0, 1000, 800), (0, 0, 1200, 800)];

        assert_eq!(
            logical_window_crop_rect(&bounds, &monitors, 2200, 800).unwrap(),
            (100, 100, 400, 300)
        );
    }

    #[test]
    fn readonly_targeted_screenshot_requires_focused_visible_window() {
        let mut window = window_info(1, Some("Target"), None, None, None);
        assert!(ensure_readonly_screenshot_target_is_visible(&window).is_err());
        window.focused = true;
        assert!(ensure_readonly_screenshot_target_is_visible(&window).is_ok());
        window.hidden = true;
        assert!(ensure_readonly_screenshot_target_is_visible(&window).is_err());
    }

    #[test]
    fn wayland_display_is_enough_to_select_portal_fallback() {
        assert!(session_is_wayland(None, Some("wayland-1")));
        assert!(session_is_wayland(Some("  "), Some("wayland-1")));
        assert!(session_is_wayland(Some("wayland"), None));
        assert!(!session_is_wayland(Some("x11"), None));
        assert!(!session_is_wayland(None, Some("  ")));
    }

    #[test]
    fn incompatible_ydotool_socket_does_not_suppress_portal_fallback() {
        let incompatible_ydotool = ydotool_backend_available_from(true, false);
        let compatible_ydotool = ydotool_backend_available_from(true, true);

        assert!(should_prefer_portal_backend_by_default(
            true,
            incompatible_ydotool
        ));
        assert!(!should_prefer_portal_backend_by_default(
            true,
            compatible_ydotool
        ));
        assert!(!should_prefer_portal_backend_by_default(
            false,
            incompatible_ydotool
        ));
    }

    #[test]
    fn xdotool_keyboard_override_policy_matches_documented_precedence() {
        assert!(prefer_xdotool_keyboard(false, true, true, true, true));
        assert!(!prefer_xdotool_keyboard(true, true, true, true, true));
        assert!(!prefer_xdotool_keyboard(false, true, true, false, true));
        assert!(!prefer_xdotool_keyboard(false, false, true, true, true));
        assert!(prefer_xdotool_keyboard(false, false, false, true, true));
    }

    #[test]
    fn wayland_prefers_wtype_unless_ydotool_is_forced() {
        assert!(prefer_wtype_keyboard(false, true, true, true));
        assert!(!prefer_wtype_keyboard(true, true, true, true));
        assert!(!prefer_wtype_keyboard(false, false, true, true));
        assert!(!prefer_wtype_keyboard(false, true, false, true));
        assert!(!prefer_wtype_keyboard(false, true, true, false));
    }

    #[test]
    fn literal_text_portal_policy_preserves_layout_aware_routes_and_overrides() {
        // GNOME has no wtype route. ydotool availability must not suppress
        // literal portal text, unlike pointer and physical chord policies.
        assert!(prefer_portal_text_backend(
            false, false, true, false, false, false
        ));
        // A compatible compositor with wtype needs no portal prompt.
        assert!(!prefer_portal_text_backend(
            false, false, true, false, false, true
        ));
        assert!(prefer_portal_text_backend(
            false, false, true, false, true, true
        ));
        // Explicit raw/XTEST overrides, KDE clipboard, and X11 keep precedence.
        assert!(!prefer_portal_text_backend(
            true, false, true, false, true, false
        ));
        assert!(!prefer_portal_text_backend(
            false, true, true, false, true, false
        ));
        assert!(!prefer_portal_text_backend(
            false, false, true, true, true, false
        ));
        assert!(!prefer_portal_text_backend(
            false, false, false, false, true, false
        ));
    }

    #[tokio::test]
    async fn raw_text_rejects_unsupported_characters_before_backend_access() {
        assert!(validate_ydotool_text("probe-0710_a/b\t\nXYZ").is_ok());
        for text in [
            "prefixä",
            "prefix中",
            "prefix\r",
            "prefix\0",
            "prefix\u{7f}",
        ] {
            // This exits before CLI probing, socket discovery or input, even
            // though a supported prefix appears before the invalid character.
            let error = run_ydotool_type_text(text).await.unwrap_err();
            assert!(error.contains("no text was sent"), "{error}");
        }
    }

    #[test]
    fn raw_text_success_explains_layout_dependence() {
        let result = action_result("type_text", Ok(Vec::new()), None);
        assert!(result.ok);
        assert!(result.message.contains("US physical key positions"));
        assert!(result.message.contains("verify the field contents"));
    }

    #[test]
    fn window_crop_happens_before_screenshot_payload_resize() {
        let (cropped, width, height) = crop_png(&solid_png(400, 200), 50, 20, 200, 100).unwrap();
        let capture = prepare_screenshot_payload(
            RawScreenshotCapture {
                mime_type: "image/png".to_string(),
                bytes: cropped,
                source: "test".to_string(),
                width,
                height,
            },
            ScreenshotPayloadOptions {
                max_width: Some(100),
                max_height: Some(100),
                max_bytes: Some(1024 * 1024),
                ..Default::default()
            },
        )
        .unwrap();

        assert_eq!(
            (capture.coordinate_width, capture.coordinate_height),
            (200, 100)
        );
        assert_eq!((capture.width, capture.height), (100, 50));
        assert!(capture.resized);
    }

    fn window_info(
        window_id: u64,
        title: Option<&str>,
        app_id: Option<&str>,
        wm_class: Option<&str>,
        pid: Option<u32>,
    ) -> WindowInfo {
        WindowInfo {
            window_id,
            title: title.map(str::to_string),
            app_id: app_id.map(str::to_string),
            wm_class: wm_class.map(str::to_string),
            pid,
            bounds: Some(WindowBounds {
                x: Some(10),
                y: Some(20),
                width: 800,
                height: 600,
            }),
            workspace: Some(0),
            focused: false,
            hidden: false,
            client_type: Some("wayland".to_string()),
            backend: GNOME_SHELL_EXTENSION_BACKEND.to_string(),
            terminal: None,
        }
    }

    #[test]
    fn relative_click_coordinates_use_capture_space_rect() {
        let mut params = ClickParams {
            x: Some(7),
            y: Some(9),
            relative: Some(true),
            ..Default::default()
        };

        apply_window_relative_click_coordinates(&mut params, (133, 267, 1067, 800)).unwrap();

        assert_eq!((params.x, params.y), (Some(140), Some(276)));
    }

    #[test]
    fn relative_click_origin_is_screenshot_crop_not_raw_surface_buffer() {
        // GNOME Wayland CSD probe: frame [760,406,400,300], buffer
        // [746,394,428,329]. The screenshot's green target is at [850,543].
        // Crop-local [90,137] and desktop [850,543] must denote the same point;
        // substituting the buffer/shadow origin would miss by [14,12].
        let mut params = ClickParams {
            x: Some(90),
            y: Some(137),
            relative: Some(true),
            ..Default::default()
        };
        apply_window_relative_click_coordinates(&mut params, (760, 406, 400, 300)).unwrap();
        assert_eq!((params.x, params.y), (Some(850), Some(543)));
    }

    #[test]
    fn relative_click_coordinates_require_xy() {
        let mut params = ClickParams {
            x: Some(7),
            relative: Some(true),
            ..Default::default()
        };

        let error =
            apply_window_relative_click_coordinates(&mut params, (100, 200, 800, 600)).unwrap_err();

        assert!(error.contains("both x and y"));
        assert_eq!((params.x, params.y), (Some(7), None));
    }

    #[test]
    fn relative_click_coordinates_must_stay_inside_bounds() {
        for (x, y) in [(-1, 9), (7, -1), (800, 9), (7, 600)] {
            let mut params = ClickParams {
                x: Some(x),
                y: Some(y),
                relative: Some(true),
                ..Default::default()
            };

            let error = apply_window_relative_click_coordinates(&mut params, (100, 200, 800, 600))
                .unwrap_err();

            assert!(error.contains("inside target-window bounds"));
            assert_eq!((params.x, params.y), (Some(x), Some(y)));
        }
    }

    #[test]
    fn absolute_pointer_note_reports_the_emitted_coordinate() {
        assert_eq!(
            abs_pointer_clamp_note(crate::abs_pointer::PointerLanding {
                requested: (1920, 1080),
                emitted: (1919, 1079),
            }),
            Some(
                "Requested coordinate 1920,1080 was clamped to 1919,1079 by the uinput absolute pointer."
                    .to_string()
            )
        );
        assert_eq!(
            abs_pointer_clamp_note(crate::abs_pointer::PointerLanding {
                requested: (640, 480),
                emitted: (640, 480),
            }),
            None
        );
    }

    #[test]
    fn accessibility_filter_candidates_prefer_title_and_skip_synthetic_app_id() {
        let window = window_info(
            42,
            Some("CU ATSPI GTK Test"),
            Some("window:46"),
            Some("cu_atspi_gtk_test.py"),
            Some(2914326),
        );

        let candidates = accessibility_filter_candidates(Some(&window));

        assert_eq!(
            candidates,
            vec![
                "CU ATSPI GTK Test".to_string(),
                "cu_atspi_gtk_test.py".to_string(),
            ]
        );
    }

    #[test]
    fn select_accessibility_object_ref_prefers_exact_pid_match() {
        let apps = vec![
            AccessibleAppSummary {
                object_ref: ":1.31/org/a11y/atspi/accessible/root".to_string(),
                name: Some("electron".to_string()),
                pid: Some(2774076),
                role: "application".to_string(),
                child_count: 1,
                bounds: None,
            },
            AccessibleAppSummary {
                object_ref: ":1.64/org/a11y/atspi/accessible/root".to_string(),
                name: Some("cu_atspi_gtk_test.py".to_string()),
                pid: Some(2914326),
                role: "application".to_string(),
                child_count: 1,
                bounds: None,
            },
        ];

        let object_ref = select_accessibility_object_ref(
            &apps,
            2914326,
            &[
                "CU ATSPI GTK Test".to_string(),
                "cu_atspi_gtk_test.py".to_string(),
            ],
        )
        .unwrap();

        assert_eq!(object_ref, ":1.64/org/a11y/atspi/accessible/root");
    }

    #[test]
    fn compact_accessibility_tree_reparents_actionable_descendants() {
        let nodes = vec![
            AccessibilityNode {
                index: 0,
                parent_index: None,
                depth: 0,
                object_ref: ":1.0/root".to_string(),
                role: "application".to_string(),
                name: Some("demo-app".to_string()),
                description: None,
                child_count: 1,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 1,
                parent_index: Some(0),
                depth: 1,
                object_ref: ":1.1/frame".to_string(),
                role: "frame".to_string(),
                name: Some("Demo Frame".to_string()),
                description: None,
                child_count: 1,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 2,
                parent_index: Some(1),
                depth: 2,
                object_ref: ":1.2/filler".to_string(),
                role: "filler".to_string(),
                name: None,
                description: None,
                child_count: 1,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 3,
                parent_index: Some(2),
                depth: 3,
                object_ref: ":1.3/button".to_string(),
                role: "button".to_string(),
                name: Some("Run".to_string()),
                description: None,
                child_count: 0,
                bounds: Some(Bounds {
                    x: 10,
                    y: 20,
                    width: 100,
                    height: 40,
                }),
                states: Vec::new(),
                actions: vec![AccessibilityAction {
                    index: 0,
                    name: "Click".to_string(),
                    description: "Clicks the button".to_string(),
                    keybinding: String::new(),
                }],
                value: None,
                text: None,
                supports_editable_text: false,
            },
        ];

        let compacted = compact_accessibility_tree(nodes);

        assert_eq!(compacted.len(), 3);
        assert_eq!(compacted[0].role, "application");
        assert_eq!(compacted[1].role, "frame");
        assert_eq!(compacted[2].role, "button");
        assert_eq!(compacted[2].parent_index, Some(1));
        assert_eq!(compacted[1].child_count, 1);
    }

    #[test]
    fn compact_accessibility_tree_drops_structural_noise() {
        let nodes = vec![
            AccessibilityNode {
                index: 0,
                parent_index: None,
                depth: 0,
                object_ref: ":1.0/root".to_string(),
                role: "application".to_string(),
                name: Some("demo-app".to_string()),
                description: None,
                child_count: 2,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 1,
                parent_index: Some(0),
                depth: 1,
                object_ref: ":1.1/frame".to_string(),
                role: "frame".to_string(),
                name: Some("Demo Frame".to_string()),
                description: None,
                child_count: 2,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 2,
                parent_index: Some(1),
                depth: 2,
                object_ref: ":1.2/tab".to_string(),
                role: "page tab".to_string(),
                name: Some("Hidden".to_string()),
                description: None,
                child_count: 0,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
            AccessibilityNode {
                index: 3,
                parent_index: Some(1),
                depth: 2,
                object_ref: ":1.3/separator".to_string(),
                role: "separator".to_string(),
                name: None,
                description: None,
                child_count: 0,
                bounds: None,
                states: Vec::new(),
                actions: Vec::new(),
                value: None,
                text: None,
                supports_editable_text: false,
            },
        ];

        let compacted = compact_accessibility_tree(nodes);

        assert_eq!(compacted.len(), 3);
        assert_eq!(compacted[2].role, "page tab");
        assert_eq!(compacted[2].name.as_deref(), Some("Hidden"));
    }

    #[test]
    fn kde_clipboard_restore_delay_uses_minimum_for_short_text() {
        assert_eq!(
            kde_clipboard_restore_delay("short"),
            Duration::from_millis(KDE_CLIPBOARD_RESTORE_MIN_DELAY_MS)
        );
    }

    #[test]
    fn kde_clipboard_restore_delay_scales_and_caps_long_text() {
        let scaled_text = "x".repeat(1_000);
        assert_eq!(
            kde_clipboard_restore_delay(&scaled_text),
            Duration::from_millis(4_000)
        );

        let capped_text = "x".repeat(10_000);
        assert_eq!(
            kde_clipboard_restore_delay(&capped_text),
            Duration::from_millis(KDE_CLIPBOARD_RESTORE_MAX_DELAY_MS)
        );
    }

    #[test]
    fn kde_clipboard_maps_each_paste_shortcut_to_its_chord() {
        assert_eq!(
            kde_clipboard_paste_chord(KdeClipboardPasteShortcut::Standard),
            (&[EVDEV_KEY_LEFTCTRL][..], EVDEV_KEY_V)
        );
        assert_eq!(
            kde_clipboard_paste_chord(KdeClipboardPasteShortcut::CtrlShiftV),
            (&[EVDEV_KEY_LEFTCTRL, EVDEV_KEY_LEFTSHIFT][..], EVDEV_KEY_V)
        );
        assert_eq!(
            kde_clipboard_paste_chord(KdeClipboardPasteShortcut::ShiftInsert),
            (&[EVDEV_KEY_LEFTSHIFT][..], EVDEV_KEY_INSERT)
        );
    }

    fn focused(role: &str, is_terminal: bool) -> FocusedElementSummary {
        FocusedElementSummary {
            role: role.to_string(),
            name: None,
            editable: false,
            states: vec!["focused".to_string()],
            is_terminal,
        }
    }

    #[test]
    fn kde_clipboard_focus_fallback_uses_the_role_enum_not_the_name() {
        let ctrl_shift_v = Some(TerminalPasteShortcut::CtrlShiftV);
        let shift_insert = Some(TerminalPasteShortcut::ShiftInsert);

        // No AT-SPI answer (xterm exposes none) keeps the terminal chord.
        assert_eq!(
            kde_clipboard_shortcut_for_focus(shift_insert, None),
            KdeClipboardPasteShortcut::ShiftInsert
        );
        // A terminal widget keeps it, whatever its localized role name.
        assert_eq!(
            kde_clipboard_shortcut_for_focus(ctrl_shift_v, Some(&focused("терминал", true))),
            KdeClipboardPasteShortcut::CtrlShiftV
        );
        // A focused search field inside the terminal window pastes with Ctrl+V.
        assert_eq!(
            kde_clipboard_shortcut_for_focus(ctrl_shift_v, Some(&focused("text", false))),
            KdeClipboardPasteShortcut::Standard
        );
        assert_eq!(
            kde_clipboard_shortcut_for_focus(None, None),
            KdeClipboardPasteShortcut::Standard
        );
    }

    #[test]
    fn kde_clipboard_routes_xterm_family_to_shift_insert() {
        for wm_class in ["xterm", "UXTerm", "urxvt", "rxvt-unicode", "koi8rxterm"] {
            let window = window_info(1, Some("user@host"), None, Some(wm_class), Some(100));
            assert_eq!(
                kde_clipboard_terminal_shortcut(&WindowTarget::default(), Some(&window)),
                Some(TerminalPasteShortcut::ShiftInsert),
                "{wm_class} should paste with Shift+Insert"
            );
        }
    }

    #[test]
    fn kde_clipboard_routes_explicit_tty_targets_to_terminal_paste() {
        let target = WindowTarget {
            tty: Some("/dev/pts/11".to_string()),
            ..Default::default()
        };

        assert_eq!(
            kde_clipboard_terminal_shortcut(&target, None),
            Some(TerminalPasteShortcut::CtrlShiftV)
        );
    }

    #[test]
    fn kde_clipboard_prefers_resolved_window_over_stale_terminal_selector() {
        let target = WindowTarget {
            tty: Some("/dev/pts/11".to_string()),
            ..Default::default()
        };
        let window = window_info(
            1,
            Some("Browser"),
            Some("firefox"),
            Some("firefox"),
            Some(100),
        );

        assert_eq!(
            kde_clipboard_terminal_shortcut(&target, Some(&window)),
            None
        );
    }

    #[tokio::test]
    async fn kde_clipboard_async_routing_prefers_focus_over_stale_terminal_selector() {
        let target = WindowTarget {
            tty: Some("/dev/pts/11".to_string()),
            ..Default::default()
        };
        let window = window_info(
            1,
            Some("Browser"),
            Some("firefox"),
            Some("firefox"),
            Some(100),
        );
        let focus = WindowFocusResult {
            requested_window: window.clone(),
            focused_window: Some(window),
            exact_window_focused: true,
            app_focused: true,
            backend: KWIN_BACKEND.to_string(),
            note: "test".to_string(),
        };

        assert_eq!(
            kde_clipboard_paste_shortcut(&target, Some(&focus)).await,
            KdeClipboardPasteShortcut::Standard
        );
    }

    #[test]
    fn kde_clipboard_routes_konsole_identity_to_terminal_paste() {
        let window = window_info(
            1,
            Some("unflappable-donkey"),
            Some("org.kde.konsole"),
            Some("konsole"),
            Some(100),
        );

        assert_eq!(
            kde_clipboard_terminal_shortcut(&WindowTarget::default(), Some(&window)),
            Some(TerminalPasteShortcut::CtrlShiftV)
        );
    }

    #[test]
    fn kde_clipboard_does_not_classify_terminal_words_in_browser_titles() {
        let window = window_info(
            1,
            Some("xterm documentation"),
            Some("firefox"),
            Some("firefox"),
            Some(100),
        );

        assert_eq!(
            kde_clipboard_terminal_shortcut(&WindowTarget::default(), Some(&window)),
            None
        );
    }

    #[test]
    fn kde_clipboard_pty_metadata_does_not_change_editor_paste() {
        let mut window = window_info(
            1,
            Some("xterm integration test"),
            Some("example-ide"),
            Some("ExampleIde"),
            Some(100),
        );
        window.terminal = Some(crate::terminal::TerminalWindowContext {
            tty: "/dev/pts/11".to_string(),
            root_process: crate::terminal::TerminalProcess {
                pid: 200,
                command_name: "bash".to_string(),
                command_line: "bash".to_string(),
                cwd: None,
            },
            active_process: None,
            process_count: 1,
            confidence: "high".to_string(),
            match_reason: "one child PTY".to_string(),
        });
        let terminal = kde_clipboard_terminal_shortcut(&WindowTarget::default(), Some(&window));
        assert_eq!(terminal, None);
        assert_eq!(
            kde_clipboard_shortcut_for_focus(terminal, None),
            KdeClipboardPasteShortcut::Standard
        );
    }

    #[tokio::test]
    async fn kde_clipboard_dbus_operation_times_out_when_pending() {
        let error = kde_clipboard_dbus_operation_with_timeout(
            "proxy creation",
            std::future::pending::<zbus::Result<()>>(),
            Duration::from_millis(1),
        )
        .await
        .unwrap_err();

        assert_eq!(error, "KDE clipboard proxy creation timed out");
    }

    #[test]
    fn cached_element_index_resolves_to_bounds_center() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 100,
                height: 40,
            }),
        )]);

        let point = backend
            .resolve_optional_target_point(None, None, Some(7))
            .unwrap()
            .unwrap();

        assert_eq!(point, (60, 40));
    }

    #[test]
    fn coordinate_target_overrides_cached_element_index() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 100,
                height: 40,
            }),
        )]);

        let point = backend
            .resolve_optional_target_point(Some(200), Some(300), Some(7))
            .unwrap()
            .unwrap();

        assert_eq!(point, (200, 300));
    }

    #[test]
    fn cached_element_index_requires_positive_bounds() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 0,
                height: 40,
            }),
        )]);

        let error = backend
            .resolve_optional_target_point(None, None, Some(7))
            .unwrap_err();

        assert!(error.contains("No clickable bounds cached for element_index 7"));
    }

    #[test]
    fn cached_element_index_ignores_sentinel_bounds() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(
            7,
            Some(Bounds {
                x: i32::MIN,
                y: i32::MIN,
                width: 1,
                height: 1,
            }),
        )]);

        let error = backend
            .resolve_optional_target_point(None, None, Some(7))
            .unwrap_err();

        assert!(error.contains("No clickable bounds cached for element_index 7"));
    }

    #[test]
    fn empty_node_cache_clears_stale_element_index() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 100,
                height: 40,
            }),
        )]);
        backend.cache_nodes(&[]);

        let error = backend
            .resolve_optional_target_point(None, None, Some(7))
            .unwrap_err();

        assert!(error.contains("No clickable bounds cached for element_index 7"));
    }

    #[test]
    fn click_target_falls_back_to_primary_action_without_bounds() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node_with_actions(
            7,
            None,
            vec![AccessibilityAction {
                index: 0,
                name: "Click".to_string(),
                description: "Clicks the button".to_string(),
                keybinding: String::new(),
            }],
        )]);

        let target = backend
            .resolve_click_target(&ClickParams {
                element_index: Some(7),
                ..Default::default()
            })
            .unwrap();

        match target {
            ClickTarget::PrimaryAction {
                object_ref,
                action_name,
                action_index,
            } => {
                assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
                assert_eq!(action_name.as_deref(), Some("Click"));
                assert_eq!(action_index, 0);
            }
            ClickTarget::Coordinates(_, _) => {
                panic!("expected AT-SPI primary-action fallback")
            }
        }
    }

    #[test]
    fn element_activation_ignores_hidpi_and_zero_origin_bounds() {
        for (x, y) in [(75, 144), (0, 0)] {
            let backend = ComputerUseLinux::default();
            backend.cache_nodes(&[node_with_actions(
                7,
                Some(Bounds {
                    x,
                    y,
                    width: 320,
                    height: 120,
                }),
                vec![AccessibilityAction {
                    index: 0,
                    name: "click".into(),
                    description: String::new(),
                    keybinding: String::new(),
                }],
            )]);
            assert!(matches!(
                backend
                    .resolve_click_target(&ClickParams {
                        element_index: Some(7),
                        ..Default::default()
                    })
                    .unwrap(),
                ClickTarget::PrimaryAction {
                    action_index: 0,
                    ..
                }
            ));
            // Explicit coordinates and gestures retain pointer semantics.
            for params in [
                ClickParams {
                    x: Some(470),
                    y: Some(408),
                    element_index: Some(7),
                    ..Default::default()
                },
                ClickParams {
                    button: Some("right".into()),
                    element_index: Some(7),
                    ..Default::default()
                },
                ClickParams {
                    click_count: Some(2),
                    element_index: Some(7),
                    ..Default::default()
                },
            ] {
                assert!(matches!(
                    backend.resolve_click_target(&params).unwrap(),
                    ClickTarget::Coordinates(_, _)
                ));
            }
        }
    }

    #[test]
    fn element_click_does_not_replace_pointer_with_non_activation_action() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node_with_actions(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 20,
                height: 20,
            }),
            vec![AccessibilityAction {
                index: 0,
                name: "show-menu".into(),
                description: String::new(),
                keybinding: String::new(),
            }],
        )]);
        assert!(matches!(
            backend
                .resolve_click_target(&ClickParams {
                    element_index: Some(7),
                    ..Default::default()
                })
                .unwrap(),
            ClickTarget::Coordinates(20, 30)
        ));
    }

    #[test]
    fn entry_submit_and_slider_jump_are_not_pointer_clicks() {
        for (role, action_name) in [
            ("entry", "activate"),
            ("spin button", "activate"),
            ("slider", "jump"),
        ] {
            let backend = ComputerUseLinux::default();
            let mut target = node_with_actions(
                7,
                Some(Bounds {
                    x: 10,
                    y: 20,
                    width: 100,
                    height: 40,
                }),
                vec![AccessibilityAction {
                    index: 0,
                    name: action_name.into(),
                    description: String::new(),
                    keybinding: String::new(),
                }],
            );
            target.role = role.into();
            backend.cache_nodes(&[target]);
            assert!(matches!(
                backend
                    .resolve_click_target(&ClickParams {
                        element_index: Some(7),
                        ..Default::default()
                    })
                    .unwrap(),
                ClickTarget::Coordinates(60, 40)
            ));
            let mut node = backend.last_snapshot.lock().unwrap().nodes[0].clone();
            for bounds in [
                None,
                Some(Bounds {
                    x: i32::MIN,
                    y: i32::MIN,
                    width: 1,
                    height: 1,
                }),
                Some(Bounds {
                    x: 0,
                    y: 0,
                    width: 0,
                    height: 0,
                }),
            ] {
                node.bounds = bounds;
                backend.cache_nodes(&[node.clone()]);
                assert!(backend
                    .resolve_click_target(&ClickParams {
                        element_index: Some(7),
                        ..Default::default()
                    })
                    .is_err());
            }
        }
    }

    #[test]
    fn click_target_falls_back_to_primary_action_with_sentinel_bounds() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node_with_actions(
            7,
            Some(Bounds {
                x: i32::MIN,
                y: i32::MIN,
                width: 1,
                height: 1,
            }),
            vec![AccessibilityAction {
                index: 0,
                name: "Click".to_string(),
                description: "Clicks the button".to_string(),
                keybinding: String::new(),
            }],
        )]);

        let target = backend
            .resolve_click_target(&ClickParams {
                element_index: Some(7),
                ..Default::default()
            })
            .unwrap();

        match target {
            ClickTarget::PrimaryAction {
                object_ref,
                action_name,
                action_index,
            } => {
                assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
                assert_eq!(action_name.as_deref(), Some("Click"));
                assert_eq!(action_index, 0);
            }
            ClickTarget::Coordinates(_, _) => {
                panic!("expected AT-SPI primary-action fallback")
            }
        }
    }

    #[test]
    fn click_target_requires_bounds_for_non_plain_clicks() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node_with_actions(
            7,
            None,
            vec![AccessibilityAction {
                index: 0,
                name: "Click".to_string(),
                description: "Clicks the button".to_string(),
                keybinding: String::new(),
            }],
        )]);

        let error = backend
            .resolve_click_target(&ClickParams {
                element_index: Some(7),
                button: Some("right".to_string()),
                ..Default::default()
            })
            .unwrap_err();

        assert!(error.contains("No clickable bounds cached for element_index 7"));
    }

    #[test]
    fn absolute_mousemove_uses_coordinate_separator() {
        assert_eq!(
            absolute_mousemove_args(200, 300),
            vec![
                "mousemove".to_string(),
                "--absolute".to_string(),
                "--".to_string(),
                "200".to_string(),
                "300".to_string(),
            ]
        );
    }

    #[test]
    fn native_x11_pointer_policy_requires_explicit_x11_without_wayland_display() {
        assert!(native_x11_xdotool_pointer_session(Some("x11"), None));
        assert!(!native_x11_xdotool_pointer_session(
            Some("wayland"),
            Some("wayland-0")
        ));
        assert!(!native_x11_xdotool_pointer_session(
            Some("x11"),
            Some("wayland-0")
        ));
    }

    #[test]
    fn xdotool_pointer_command_is_single_no_sync_move_and_click() {
        assert_eq!(
            xdotool_pointer_click_args(1550, 930, 3, Some("right")),
            Some(vec![
                "mousemove".to_string(),
                "--".to_string(),
                "1550".to_string(),
                "930".to_string(),
                "click".to_string(),
                "--repeat".to_string(),
                "3".to_string(),
                "3".to_string(),
            ])
        );
    }

    #[test]
    fn xdotool_pointer_policy_requires_all_pure_gating_conditions() {
        let eligible = (false, Some("x11"), true, None, true);
        assert!(prefer_xdotool_pointer(
            eligible.0, eligible.1, eligible.2, eligible.3, eligible.4
        ));
        assert!(!prefer_xdotool_pointer(true, Some("x11"), true, None, true));
        assert!(!prefer_xdotool_pointer(
            false,
            Some("wayland"),
            true,
            None,
            true
        ));
        assert!(!prefer_xdotool_pointer(
            false,
            None,
            true,
            Some("wayland-0"),
            true
        ));
        assert!(!prefer_xdotool_pointer(
            false,
            Some("x11"),
            false,
            None,
            true
        ));
        assert!(!prefer_xdotool_pointer(
            false,
            Some("x11"),
            true,
            None,
            false
        ));
    }

    #[test]
    fn xdotool_pointer_supports_only_standard_buttons() {
        assert!(xdotool_pointer_click_args(10, 20, 1, None).is_some());
        assert!(xdotool_pointer_click_args(10, 20, 1, Some("middle")).is_some());
        assert!(xdotool_pointer_click_args(10, 20, 1, Some("right")).is_some());
    }

    #[test]
    fn extended_pointer_buttons_do_not_construct_xdotool_commands() {
        for button in ["side", "extra", "forward", "back"] {
            assert_eq!(xdotool_pointer_click_args(10, 20, 1, Some(button)), None);
        }
    }

    #[tokio::test]
    async fn pointer_xdotool_spawn_failure_uses_ydotool_fallback() {
        let result = run_xdotool_pointer_or_fallback(
            Path::new("/definitely/missing/xdotool"),
            &[],
            || async { Ok::<_, String>(Vec::new()) },
        )
        .await
        .expect("spawn failure should use fallback");

        assert_eq!(result.backend, KeyboardCommandBackend::Ydotool);
    }

    #[tokio::test]
    async fn pointer_xdotool_nonzero_exit_does_not_use_ydotool_fallback() {
        let result = run_xdotool_pointer_or_fallback(
            Path::new("/bin/sh"),
            &["-c".to_string(), "exit 9".to_string()],
            || async { Err::<Vec<Output>, _>("fallback called".to_string()) },
        )
        .await;

        let error = result.expect_err("launched nonzero xdotool must be terminal");
        assert!(!error.contains("fallback called"));
    }

    #[test]
    fn xdotool_scroll_uses_wheel_buttons_instead_of_a_valuator_event() {
        assert_eq!(
            xdotool_pointer_scroll_args(Some((1550, 930)), 5, ScrollDirection::Up),
            vec![
                "mousemove".to_string(),
                "--".to_string(),
                "1550".to_string(),
                "930".to_string(),
                "click".to_string(),
                "--delay".to_string(),
                "12".to_string(),
                "--repeat".to_string(),
                "5".to_string(),
                "4".to_string(),
            ]
        );
        assert_eq!(
            xdotool_pointer_scroll_args(Some((-20, 15)), 1, ScrollDirection::Down),
            vec![
                "mousemove".to_string(),
                "--".to_string(),
                "-20".to_string(),
                "15".to_string(),
                "click".to_string(),
                "--delay".to_string(),
                "12".to_string(),
                "--repeat".to_string(),
                "1".to_string(),
                "5".to_string(),
            ]
        );
        assert_eq!(
            xdotool_pointer_scroll_args(None, 3, ScrollDirection::Left),
            vec![
                "click".to_string(),
                "--delay".to_string(),
                "12".to_string(),
                "--repeat".to_string(),
                "3".to_string(),
                "6".to_string(),
            ]
        );
        assert_eq!(
            xdotool_pointer_scroll_args(None, 0, ScrollDirection::Right),
            vec![
                "click".to_string(),
                "--delay".to_string(),
                "12".to_string(),
                "--repeat".to_string(),
                "1".to_string(),
                "7".to_string(),
            ]
        );
    }

    #[test]
    fn scroll_pages_are_bounded_before_unit_conversion() {
        assert_eq!(scroll_units(None).unwrap(), 5);
        assert_eq!(scroll_units(Some(1.0)).unwrap(), 5);
        assert_eq!(scroll_units(Some(-1.0)).unwrap(), 5);
        assert_eq!(scroll_units(Some(0.0)).unwrap(), 1);
        assert_eq!(scroll_units(Some(0.1)).unwrap(), 1);
        assert_eq!(scroll_units(Some(100.0)).unwrap(), 500);
        assert_eq!(scroll_units(Some(-100.0)).unwrap(), 500);

        for pages in [
            100.000_001,
            -100.000_001,
            f64::NAN,
            f64::INFINITY,
            f64::NEG_INFINITY,
        ] {
            assert!(scroll_units(Some(pages)).is_err(), "accepted {pages}");
        }
    }

    #[test]
    fn maximum_scroll_timeout_is_bounded() {
        let repeat = u32::try_from(scroll_units(Some(MAX_SCROLL_PAGES)).unwrap()).unwrap();

        assert_eq!(repeat, 500);
        assert_eq!(
            xdotool_scroll_timeout(repeat, XDOTOOL_SCROLL_CLICK_DELAY_MS),
            Duration::from_secs(16)
        );
    }

    #[test]
    fn xdotool_scroll_timeout_covers_the_gap_after_every_click() {
        assert_eq!(
            xdotool_scroll_timeout(1, 12),
            INPUT_COMMAND_TIMEOUT + Duration::from_millis(12)
        );
        assert_eq!(
            xdotool_scroll_timeout(5, 100),
            INPUT_COMMAND_TIMEOUT + Duration::from_millis(500)
        );
        assert_eq!(
            xdotool_scroll_timeout(0, 12),
            INPUT_COMMAND_TIMEOUT + Duration::from_millis(12)
        );
    }

    #[test]
    fn wheel_mousemove_uses_coordinate_separator_for_negative_values() {
        assert_eq!(
            wheel_mousemove_args(0, -3),
            vec![
                "mousemove".to_string(),
                "--wheel".to_string(),
                "--".to_string(),
                "0".to_string(),
                "-3".to_string(),
            ]
        );
    }

    #[test]
    fn pointer_actions_keep_pixel_coordinates_for_ydotool_absolute_moves() {
        assert_eq!(
            absolute_mousemove_args(1550, 930),
            vec![
                "mousemove".to_string(),
                "--absolute".to_string(),
                "--".to_string(),
                "1550".to_string(),
                "930".to_string(),
            ]
        );
    }

    #[test]
    fn key_chord_splits_modifiers_and_key() {
        assert_eq!(key_chord("Ctrl+Shift+P"), Some((vec![29, 42], 25)));
        assert_eq!(key_chord("Ctrl+S"), Some((vec![29], 31)));
        assert_eq!(key_chord("Enter"), Some((vec![], 28)));
        // A bare modifier is a chord with no held modifiers.
        assert_eq!(key_chord("Super"), Some((vec![], 125)));
        assert_eq!(key_chord("NotAKey"), None);
    }

    #[test]
    fn portal_key_chord_sends_modifiers_and_named_keys_as_keysyms() {
        use PortalKey::{Keycode, Keysym};
        // Modifiers resolve through the live keymap (#191); letters stay
        // physical so non-Latin layout groups still see Ctrl+A.
        assert_eq!(
            portal_key_chord("Ctrl+A"),
            Some((vec![Keysym(xkey::Control_L as i32)], Keycode(30)))
        );
        assert_eq!(
            portal_key_chord("ctrl+shift+t"),
            Some((
                vec![Keysym(xkey::Control_L as i32), Keysym(xkey::Shift_L as i32)],
                Keycode(20)
            ))
        );
        assert_eq!(
            portal_key_chord("Alt+F4"),
            Some((vec![Keysym(xkey::Alt_L as i32)], Keysym(xkey::F4 as i32)))
        );
        assert_eq!(
            portal_key_chord("Meta+Return"),
            Some((
                vec![Keysym(xkey::Super_L as i32)],
                Keysym(xkey::Return as i32)
            ))
        );
        assert_eq!(
            portal_key_chord("Super"),
            Some((vec![], Keysym(xkey::Super_L as i32)))
        );
        assert_eq!(
            portal_key_chord("page-down"),
            Some((vec![], Keysym(xkey::Page_Down as i32)))
        );
        assert_eq!(
            portal_key_chord("Ctrl+1"),
            Some((vec![Keysym(xkey::Control_L as i32)], Keycode(2)))
        );
        assert_eq!(portal_key_chord("Ctrl+NotAKey"), None);
        assert_eq!(portal_key_chord("Hyper+A"), None);
    }

    #[test]
    fn portal_key_chord_accepts_exactly_the_evdev_grammar() {
        for key in [
            "enter",
            "Escape",
            "tab",
            "backspace",
            "del",
            "space",
            "home",
            "end",
            "pageup",
            "left",
            "right",
            "up",
            "down",
            "f1",
            "f12",
            "a",
            "z",
            "0",
            "9",
            "ctrl",
            "alt",
            "shift",
            "cmd",
            "Ctrl+Alt+Delete",
            "Shift+Tab",
            "ctrl+shift",
            "?",
            "F13",
            "Ctrl+",
            "",
        ] {
            assert_eq!(
                portal_key_chord(key).is_some(),
                key_chord(key).is_some(),
                "{key}"
            );
        }
    }

    #[test]
    fn key_sequence_presses_modifiers_around_key() {
        assert_eq!(
            key_sequence("Ctrl+Shift+P"),
            Some(vec![
                "29:1".to_string(),
                "42:1".to_string(),
                "25:1".to_string(),
                "25:0".to_string(),
                "42:0".to_string(),
                "29:0".to_string(),
            ])
        );
    }

    #[test]
    fn ydotool_modifier_chords_include_an_inter_event_delay() {
        let args = ydotool_key_args(key_sequence("Ctrl+T").unwrap(), true);

        assert_eq!(args, ["key", "-d", "100", "29:1", "20:1", "20:0", "29:0"]);
    }

    #[test]
    fn key_sequence_presses_bare_modifier() {
        assert_eq!(
            key_sequence("Super"),
            Some(vec!["125:1".to_string(), "125:0".to_string()])
        );
    }

    #[test]
    fn xdotool_key_spec_maps_named_keys_to_x11_keysyms() {
        assert_eq!(xdotool_key_spec("Return"), Some("Return".to_string()));
        assert_eq!(xdotool_key_spec("enter"), Some("Return".to_string()));
        assert_eq!(xdotool_key_spec("Escape"), Some("Escape".to_string()));
        assert_eq!(xdotool_key_spec("backspace"), Some("BackSpace".to_string()));
        assert_eq!(xdotool_key_spec("PageUp"), Some("Page_Up".to_string()));
        assert_eq!(xdotool_key_spec("ArrowLeft"), Some("Left".to_string()));
        assert_eq!(xdotool_key_spec("f5"), Some("F5".to_string()));
        assert_eq!(xdotool_key_spec("space"), Some("space".to_string()));
    }

    #[test]
    fn xdotool_key_spec_maps_chords_with_modifier_prefixes() {
        assert_eq!(xdotool_key_spec("ctrl+a"), Some("ctrl+a".to_string()));
        assert_eq!(xdotool_key_spec("Ctrl+S"), Some("ctrl+s".to_string()));
        assert_eq!(
            xdotool_key_spec("Ctrl+Shift+P"),
            Some("ctrl+shift+p".to_string())
        );
        assert_eq!(
            xdotool_key_spec("Meta+Return"),
            Some("super+Return".to_string())
        );
        assert_eq!(xdotool_key_spec("Alt+F4"), Some("alt+F4".to_string()));
    }

    #[test]
    fn xdotool_key_spec_maps_bare_modifier_to_single_keysym() {
        assert_eq!(xdotool_key_spec("Super"), Some("super".to_string()));
        assert_eq!(xdotool_key_spec("ctrl"), Some("ctrl".to_string()));
    }

    #[test]
    fn xdotool_type_keeps_a_per_character_delay_so_xtest_events_stay_ordered() {
        // Issue #147: `--delay 0` delivers characters out of order on some X
        // servers. The default must stay non-zero.
        const { assert!(XDOTOOL_TYPE_DELAY_MS > 0) };
        let text = "x".repeat(10_000);
        let args = xdotool_type_args_with_delay(&text, XDOTOOL_TYPE_DELAY_MS);

        assert_eq!(
            &args[..5],
            ["type", "--clearmodifiers", "--delay", "12", "--"]
        );
        assert_eq!(args[3], XDOTOOL_TYPE_DELAY_MS.to_string());
        assert_eq!(args[5], text);
    }

    #[test]
    fn xdotool_type_timeout_grows_with_text_length() {
        assert_eq!(xdotool_type_timeout("", 12), INPUT_COMMAND_TIMEOUT);
        assert_eq!(
            xdotool_type_timeout(&"x".repeat(1_000), 12),
            INPUT_COMMAND_TIMEOUT + Duration::from_secs(12)
        );
        assert_eq!(
            xdotool_type_timeout(&"x".repeat(1_000), 0),
            INPUT_COMMAND_TIMEOUT
        );
    }

    #[tokio::test]
    async fn launched_xdotool_failure_does_not_replay_through_ydotool() {
        let dir = std::env::temp_dir().join(format!(
            "computer-use-linux-xdotool-fallback-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
        ));
        std::fs::create_dir_all(&dir).expect("create command test directory");
        let ydotool = dir.join("ydotool");
        let xdotool_marker = dir.join("xdotool-ran");
        let ydotool_marker = dir.join("ydotool-ran");
        std::fs::write(
            &ydotool,
            format!("#!/bin/sh\ntouch '{}'\n", ydotool_marker.display()),
        )
        .expect("write fake ydotool");
        std::fs::set_permissions(&ydotool, std::fs::Permissions::from_mode(0o700))
            .expect("make fake ydotool executable");
        let xdotool_args = vec![
            "-c".to_string(),
            format!("touch '{}'; exit 9", xdotool_marker.display()),
        ];

        let result = run_xdotool_or_fallback(Path::new("/bin/sh"), &xdotool_args, || async {
            TokioCommand::new(&ydotool)
                .output()
                .await
                .map_err(|error| error.to_string())
        })
        .await;

        assert!(result.is_err());
        assert!(xdotool_marker.exists(), "fake xdotool did not execute");
        assert!(
            !ydotool_marker.exists(),
            "ydotool replayed input after xdotool started"
        );
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn unavailable_xdotool_uses_ydotool_fallback() {
        let result = run_xdotool_or_fallback(
            Path::new("/definitely/missing/xdotool"),
            &xdotool_type_args_with_delay("text", XDOTOOL_TYPE_DELAY_MS),
            || async {
                TokioCommand::new("sh")
                    .args(["-c", "exit 0"])
                    .output()
                    .await
                    .map_err(|error| error.to_string())
            },
        )
        .await
        .expect("spawn failure should use fallback");

        assert_eq!(result.backend, KeyboardCommandBackend::Ydotool);
        assert!(result.output.status.success());
    }

    #[tokio::test]
    async fn wtype_receives_unicode_text_through_stdin() {
        let dir = std::env::temp_dir().join(format!(
            "computer-use-linux-wtype-unicode-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
        ));
        std::fs::create_dir_all(&dir).expect("create command test directory");
        let wtype = dir.join("wtype");
        let captured = dir.join("captured");
        std::fs::write(
            &wtype,
            format!("#!/bin/sh\ncat > '{}'\n", captured.display()),
        )
        .expect("write fake wtype");
        std::fs::set_permissions(&wtype, std::fs::Permissions::from_mode(0o700))
            .expect("make fake wtype executable");
        let text = "Zwölf Yaks aßen Öl über München";

        let result = run_wtype_type_text_or_fallback(&wtype, text, || async {
            panic!("available wtype must not fall back")
        })
        .await
        .expect("wtype should succeed");

        assert_eq!(result.backend, KeyboardCommandBackend::Wtype);
        assert_eq!(std::fs::read_to_string(&captured).unwrap(), text);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn unavailable_wtype_uses_ydotool_fallback() {
        let result = run_wtype_type_text_or_fallback(
            Path::new("/definitely/missing/wtype"),
            "text",
            || async {
                TokioCommand::new("sh")
                    .args(["-c", "exit 0"])
                    .output()
                    .await
                    .map_err(|error| error.to_string())
            },
        )
        .await
        .expect("missing wtype should use fallback");

        assert_eq!(result.backend, KeyboardCommandBackend::Ydotool);
    }

    #[tokio::test]
    async fn launched_wtype_failure_does_not_replay_through_ydotool() {
        let dir = std::env::temp_dir().join(format!(
            "computer-use-linux-wtype-fallback-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
        ));
        std::fs::create_dir_all(&dir).expect("create command test directory");
        let wtype = dir.join("wtype");
        let fallback_marker = dir.join("ydotool-ran");
        std::fs::write(&wtype, "#!/bin/sh\nexit 9\n").expect("write fake wtype");
        std::fs::set_permissions(&wtype, std::fs::Permissions::from_mode(0o700))
            .expect("make fake wtype executable");

        let result = run_wtype_type_text_or_fallback(&wtype, "text", || async {
            std::fs::write(&fallback_marker, "ran").unwrap();
            TokioCommand::new("true")
                .output()
                .await
                .map_err(|error| error.to_string())
        })
        .await;

        assert!(result.is_err());
        assert!(
            !fallback_marker.exists(),
            "ydotool replayed input after wtype started"
        );
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn cancelling_xdotool_wait_kills_the_child() {
        let dir = std::env::temp_dir().join(format!(
            "computer-use-linux-xdotool-cancel-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
        ));
        std::fs::create_dir_all(&dir).expect("create command test directory");
        let xdotool = dir.join("xdotool");
        let pid_path = dir.join("pid");
        std::fs::write(
            &xdotool,
            format!(
                "#!/bin/sh\nprintf '%s' $$ > '{}'\nexec sleep 60\n",
                pid_path.display()
            ),
        )
        .expect("write fake xdotool");
        std::fs::set_permissions(&xdotool, std::fs::Permissions::from_mode(0o700))
            .expect("make fake xdotool executable");

        let task = tokio::spawn(async move { run_xdotool(&xdotool, &[]).await });
        let mut pid = None;
        for _ in 0..200 {
            if let Ok(value) = std::fs::read_to_string(&pid_path) {
                pid = value.parse::<u32>().ok();
                if pid.is_some() {
                    break;
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        let pid = pid.expect("fake xdotool did not record its pid");
        task.abort();
        let _ = task.await;

        for _ in 0..50 {
            if !Path::new(&format!("/proc/{pid}")).exists() {
                let _ = std::fs::remove_dir_all(dir);
                return;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        unsafe {
            libc::kill(pid as i32, libc::SIGKILL);
        }
        let _ = std::fs::remove_dir_all(dir);
        panic!("cancelled xdotool child {pid} was not killed");
    }

    #[tokio::test]
    async fn cancelling_between_press_and_release_keeps_input_locked() {
        let lock = std::sync::Arc::new(tokio::sync::Mutex::new(()));
        let guard = std::sync::Arc::clone(&lock).lock_owned().await;
        let (pressed_tx, pressed_rx) = tokio::sync::oneshot::channel();
        let (allow_release_tx, allow_release_rx) = tokio::sync::oneshot::channel();
        let (released_tx, released_rx) = tokio::sync::oneshot::channel();

        let waiter = tokio::spawn(async move {
            run_cancellation_safe_input(guard, async move {
                let _ = pressed_tx.send(());
                let _ = allow_release_rx.await;
                let _ = released_tx.send(());
                Ok::<(), String>(())
            })
            .await
        });

        pressed_rx.await.expect("press command did not finish");
        waiter.abort();
        let _ = waiter.await;
        assert!(
            lock.try_lock().is_err(),
            "input lock was released while the paired operation was incomplete"
        );

        allow_release_tx
            .send(())
            .expect("release command stopped on caller cancellation");
        timeout(Duration::from_secs(1), released_rx)
            .await
            .expect("release command did not finish")
            .expect("release command dropped its completion marker");
        timeout(
            Duration::from_secs(1),
            std::sync::Arc::clone(&lock).lock_owned(),
        )
        .await
        .expect("input lock remained held after the operation finished");
    }

    /// The xdotool path must accept exactly the keys the evdev grammar accepts,
    /// so switching backends can never silently widen or narrow the surface.
    #[test]
    fn xdotool_key_spec_rejects_everything_key_chord_rejects() {
        for key in ["NotAKey", "", "ctrl+", "ctrl+NotAKey", "f13", "hyper+a"] {
            assert_eq!(
                xdotool_key_spec(key).is_some(),
                key_chord(key).is_some(),
                "backend grammars diverged for {key:?}"
            );
        }
    }

    #[test]
    fn key_sequence_keeps_shortcuts_and_navigation_on_raw_events() {
        assert_eq!(
            key_sequence("Ctrl+L"),
            Some(vec![
                "29:1".to_string(),
                "38:1".to_string(),
                "38:0".to_string(),
                "29:0".to_string(),
            ])
        );
        assert_eq!(
            key_sequence("ArrowLeft"),
            Some(vec!["105:1".to_string(), "105:0".to_string()])
        );
        assert_eq!(
            key_sequence("Escape"),
            Some(vec!["1:1".to_string(), "1:0".to_string()])
        );
        assert_eq!(
            key_sequence("Enter"),
            Some(vec!["28:1".to_string(), "28:0".to_string()])
        );
    }

    #[test]
    fn ydotool_type_timeout_scales_with_text_length() {
        assert_eq!(ydotool_type_timeout("").as_secs(), 10);
        assert_eq!(ydotool_type_timeout("x").as_secs(), 11);
        assert_eq!(ydotool_type_timeout(&"x".repeat(200)).as_secs(), 20);
        assert_eq!(ydotool_type_timeout(&"x".repeat(500)).as_secs(), 35);
    }

    #[tokio::test]
    async fn command_wait_drains_output_before_exit() {
        let mut command = tokio::process::Command::new("sh");
        command.args(["-c", "yes noisy | head -c 200000 >&2; exit 7"]);
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());

        let output = crate::command_runner::output_with_timeout(
            command,
            "run test command",
            Duration::from_secs(5),
        )
        .await
        .expect("child should exit before timeout");

        assert_eq!(output.status.code(), Some(7));
        assert!(output.stderr.len() >= 100_000);
    }

    #[test]
    fn perform_action_defaults_to_primary_action_index() {
        assert_eq!(requested_or_primary_action(None), "0");
        assert_eq!(requested_or_primary_action(Some("   ")), "0");
        assert_eq!(
            requested_or_primary_action(Some(" show-menu ")),
            "show-menu"
        );
    }

    #[test]
    fn element_identifier_overrides_cached_object_ref() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(7, None)]);

        let object_ref = backend
            .resolve_object_ref(
                Some(7),
                Some(":1.99/org/a11y/atspi/accessible/3"),
                &ElementSelector::default(),
                ElementResolvePurpose::Action,
            )
            .unwrap();

        assert_eq!(object_ref, ":1.99/org/a11y/atspi/accessible/3");
    }

    #[test]
    fn element_index_resolves_to_cached_object_ref() {
        let backend = ComputerUseLinux::default();
        backend.cache_nodes(&[node(7, None)]);

        let object_ref = backend
            .resolve_object_ref(
                Some(7),
                None,
                &ElementSelector::default(),
                ElementResolvePurpose::Action,
            )
            .unwrap();

        assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
    }

    #[test]
    fn semantic_selector_resolves_unique_cached_node_by_role_and_name() {
        let backend = ComputerUseLinux::default();
        let mut search_entry = node(7, None);
        search_entry.role = "entry".to_string();
        search_entry.name = Some("Search files".to_string());
        search_entry.supports_editable_text = true;
        backend.cache_nodes(&[search_entry]);

        let object_ref = backend
            .resolve_object_ref(
                None,
                None,
                &ElementSelector {
                    role: Some("entry"),
                    name: Some("search"),
                    ..Default::default()
                },
                ElementResolvePurpose::SetValue,
            )
            .unwrap();

        assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
    }

    #[test]
    fn semantic_selector_prefers_actionable_match() {
        let backend = ComputerUseLinux::default();
        let mut label = node(4, None);
        label.role = "label".to_string();
        label.name = Some("Close".to_string());
        let mut button = node_with_actions(7, None, vec![click_action()]);
        button.role = "push button".to_string();
        button.name = Some("Close".to_string());
        backend.cache_nodes(&[label, button]);

        let object_ref = backend
            .resolve_object_ref(
                None,
                None,
                &ElementSelector {
                    name: Some("close"),
                    ..Default::default()
                },
                ElementResolvePurpose::Action,
            )
            .unwrap();

        assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
    }

    #[test]
    fn semantic_selector_prefers_editable_match() {
        let backend = ComputerUseLinux::default();
        let mut label = node(4, None);
        label.role = "label".to_string();
        label.name = Some("Search".to_string());
        let mut entry = node(7, None);
        entry.role = "entry".to_string();
        entry.name = Some("Search".to_string());
        entry.supports_editable_text = true;
        backend.cache_nodes(&[label, entry]);

        let object_ref = backend
            .resolve_object_ref(
                None,
                None,
                &ElementSelector {
                    name: Some("search"),
                    ..Default::default()
                },
                ElementResolvePurpose::SetValue,
            )
            .unwrap();

        assert_eq!(object_ref, ":1.7/org/a11y/atspi/accessible/7");
    }

    #[test]
    fn semantic_selector_reports_ambiguous_matches() {
        let backend = ComputerUseLinux::default();
        let mut first = node_with_actions(7, None, vec![click_action()]);
        first.name = Some("Close".to_string());
        let mut second = node_with_actions(9, None, vec![click_action()]);
        second.name = Some("Close".to_string());
        backend.cache_nodes(&[first, second]);

        let error = backend
            .resolve_object_ref(
                None,
                None,
                &ElementSelector {
                    name: Some("close"),
                    ..Default::default()
                },
                ElementResolvePurpose::Action,
            )
            .unwrap_err();

        assert!(error.contains("matched multiple cached nodes"));
        assert!(error.contains("element_index 7"));
        assert!(error.contains("element_index 9"));
    }

    #[test]
    fn semantic_click_selector_prefers_native_activation() {
        let backend = ComputerUseLinux::default();
        let mut button = node_with_actions(
            7,
            Some(Bounds {
                x: 10,
                y: 20,
                width: 100,
                height: 40,
            }),
            vec![click_action()],
        );
        button.name = Some("Run".to_string());
        backend.cache_nodes(&[button]);

        let target = backend
            .resolve_click_target(&ClickParams {
                role: Some("button".to_string()),
                name: Some("run".to_string()),
                ..Default::default()
            })
            .unwrap();

        assert!(matches!(
            target,
            ClickTarget::PrimaryAction {
                action_index: 0,
                ..
            }
        ));
    }

    #[test]
    fn describe_focused_element_editable() {
        let element = FocusedElementSummary {
            role: "text".to_string(),
            name: Some("Message".to_string()),
            editable: true,
            states: vec!["focused".to_string()],
            is_terminal: false,
        };
        let described = describe_focused_element(&element, true);
        assert!(described.contains("editable"));
        assert!(!described.contains("WARNING"));
    }

    #[test]
    fn describe_focused_element_warns_on_non_editable_when_typing() {
        let element = FocusedElementSummary {
            role: "push button".to_string(),
            name: Some("OK".to_string()),
            editable: false,
            states: vec!["focused".to_string()],
            is_terminal: false,
        };
        let described = describe_focused_element(&element, true);
        assert!(described.contains("WARNING"));
        assert!(described.contains("not editable"));
    }

    #[test]
    fn describe_focused_element_no_warning_for_press_key() {
        let element = FocusedElementSummary {
            role: "push button".to_string(),
            name: None,
            editable: false,
            states: vec![],
            is_terminal: false,
        };
        let described = describe_focused_element(&element, false);
        assert!(!described.contains("WARNING"));
    }

    #[test]
    fn relative_scroll_translates_coordinates() {
        let mut params = ScrollParams {
            element_index: None,
            x: Some(10),
            y: Some(20),
            direction: "down".to_string(),
            pages: None,
            window_id: Some(1),
            pid: None,
            app_id: None,
            wm_class: None,
            window_title: None,
            relative: Some(true),
        };
        apply_window_relative_scroll_coordinates(&mut params, (100, 200, 800, 600)).unwrap();
        assert_eq!(params.x, Some(110));
        assert_eq!(params.y, Some(220));
    }

    #[test]
    fn window_targeted_scroll_defaults_to_window_center() {
        let mut params = ScrollParams {
            element_index: None,
            x: None,
            y: None,
            direction: "down".to_string(),
            pages: None,
            window_id: Some(1),
            pid: None,
            app_id: None,
            wm_class: None,
            window_title: None,
            relative: None,
        };
        apply_window_center_scroll_point(&mut params, (100, 200, 800, 600)).unwrap();
        assert_eq!(params.x, Some(500));
        assert_eq!(params.y, Some(500));
    }

    #[test]
    fn window_targeted_scroll_with_empty_capture_rect_errors() {
        let mut params = ScrollParams {
            element_index: None,
            x: None,
            y: None,
            direction: "down".to_string(),
            pages: None,
            window_id: Some(1),
            pid: None,
            app_id: None,
            wm_class: None,
            window_title: None,
            relative: None,
        };
        let error = apply_window_center_scroll_point(&mut params, (0, 0, 0, 0)).unwrap_err();
        assert!(error.contains("pass x/y explicitly"));
        assert_eq!(params.x, None);
        assert_eq!(params.y, None);
    }

    #[test]
    fn relative_scroll_rejects_out_of_bounds() {
        let mut params = ScrollParams {
            element_index: None,
            x: Some(801),
            y: Some(20),
            direction: "down".to_string(),
            pages: None,
            window_id: Some(1),
            pid: None,
            app_id: None,
            wm_class: None,
            window_title: None,
            relative: Some(true),
        };
        assert!(
            apply_window_relative_scroll_coordinates(&mut params, (100, 200, 800, 600)).is_err()
        );
    }

    #[test]
    fn shell_execution_requires_exact_operator_opt_in() {
        assert!(shell_execution_enabled_value(Some("1")));
        for value in [None, Some(""), Some("0"), Some("true"), Some("yes")] {
            assert!(!shell_execution_enabled_value(value));
        }
    }

    #[tokio::test]
    async fn shell_execution_clears_ambient_credentials_and_accepts_explicit_env() {
        let _serial = SHELL_ENV_TEST_LOCK.lock().await;
        let _enabled = EnvVarGuard::set(SHELL_ENABLE_ENV, "1");
        let _credential = EnvVarGuard::set("OPENAI_API_KEY", "test-ambient-credential");
        let output = execute_shell(RunShellParams {
            command: "printf '%s|%s' \"${OPENAI_API_KEY-unset}\" \"$EXPLICIT_VALUE\"".into(),
            cwd: None,
            env: BTreeMap::from([("EXPLICIT_VALUE".into(), "test-explicit-value".into())]),
            timeout_seconds: Some(2),
        })
        .await;
        assert!(output.ok, "{:?}", output.error);
        assert_eq!(output.stdout, "unset|test-explicit-value");
        assert_eq!(output.exit_code, Some(0));
    }

    #[tokio::test]
    async fn shell_execution_disabled_never_runs_requested_command() {
        async fn assert_refused() {
            let output = execute_shell(RunShellParams {
                command: "printf must-not-run".into(),
                cwd: None,
                env: BTreeMap::new(),
                timeout_seconds: None,
            })
            .await;
            assert!(!output.ok);
            assert!(output.stdout.is_empty());
            assert!(output
                .error
                .unwrap()
                .contains("shell execution is disabled"));
        }

        let _serial = SHELL_ENV_TEST_LOCK.lock().await;
        {
            let _disabled = EnvVarGuard::set(SHELL_ENABLE_ENV, "0");
            assert_refused().await;
        }
        {
            let _unset = EnvVarGuard::unset(SHELL_ENABLE_ENV);
            assert_refused().await;
        }
    }

    #[test]
    fn shell_environment_policy_excludes_ambient_credentials() {
        for allowed in [
            "PATH",
            "HOME",
            "LANG",
            "LC_ALL",
            "XDG_RUNTIME_DIR",
            "DISPLAY",
            "WAYLAND_DISPLAY",
            "DBUS_SESSION_BUS_ADDRESS",
        ] {
            assert!(inherited_shell_environment_allowed(allowed));
        }
        for secret in [
            "AWS_SECRET_ACCESS_KEY",
            "GITHUB_TOKEN",
            "OPENAI_API_KEY",
            "SSH_AUTH_SOCK",
            "LD_PRELOAD",
        ] {
            assert!(!inherited_shell_environment_allowed(secret));
        }
    }

    #[test]
    fn shell_environment_skips_non_utf8_entries() {
        use std::os::unix::ffi::OsStringExt;

        let variables = [
            (OsString::from("PATH"), OsString::from("/usr/bin")),
            (OsString::from_vec(vec![0xff]), OsString::from("ignored")),
            (OsString::from("LANG"), OsString::from_vec(vec![0xff])),
            (OsString::from("GITHUB_TOKEN"), OsString::from("secret")),
        ];

        assert_eq!(
            inherited_shell_environment(variables),
            vec![("PATH".to_string(), "/usr/bin".to_string())]
        );
    }

    #[test]
    fn shell_environment_names_follow_exec_rules() {
        for valid in ["A", "_PRIVATE", "NAME_2"] {
            assert!(valid_environment_name(valid));
        }
        for invalid in ["", "2FAST", "BAD-NAME", "A=B", "naïve"] {
            assert!(!valid_environment_name(invalid));
        }
    }

    #[test]
    fn shell_output_is_bounded_before_mcp_serialization() {
        let oversized = vec![b'x'; SHELL_RESPONSE_STREAM_BYTES + 17];
        let (visible, truncated) = bounded_shell_stream(&oversized);
        assert!(truncated);
        assert_eq!(visible.len(), SHELL_RESPONSE_STREAM_BYTES);

        let (visible, truncated) = bounded_shell_stream(b"ok");
        assert!(!truncated);
        assert_eq!(visible, "ok");
    }

    #[test]
    fn shell_audit_digest_is_stable_and_does_not_echo_command_text() {
        let digest = shell_command_sha256("printf secret");
        assert_eq!(digest.len(), 64);
        assert!(digest
            .chars()
            .all(|character| character.is_ascii_hexdigit()));
        assert!(!digest.contains("secret"));
        assert_eq!(digest, shell_command_sha256("printf secret"));
    }
}

#[cfg(test)]
mod accessibility_tree_note_tests {
    use super::{truncated_accessibility_tree_note, unscoped_accessibility_tree_warning};

    #[test]
    fn unscoped_warning_only_when_tree_succeeded_without_a_scope() {
        let warning = unscoped_accessibility_tree_warning(false, true, false).unwrap();
        assert!(warning.starts_with("WARNING:"));
        assert!(warning.contains("no app target scoped"));
        assert!(warning.contains("app_name_or_bundle_identifier"));
        assert!(warning.contains("window_id, pid, app_id, wm_class, title"));

        for target_requested in [false, true] {
            assert!(unscoped_accessibility_tree_warning(true, true, target_requested).is_none());
            assert!(unscoped_accessibility_tree_warning(false, false, target_requested).is_none());
            assert!(unscoped_accessibility_tree_warning(true, false, target_requested).is_none());
        }
    }

    /// A caller that already passed a target must not be told to pass a target;
    /// that advice would replay the same desktop-wide snapshot.
    #[test]
    fn unscoped_warning_with_a_target_points_at_accessibility_support() {
        let warning = unscoped_accessibility_tree_warning(false, true, true).unwrap();
        assert!(warning.starts_with("WARNING:"));
        assert!(warning.contains("matched no AT-SPI application root"));
        assert!(warning.contains("force-renderer-accessibility"));
        assert!(warning.contains("list_apps"));
        assert!(!warning.contains("Pass app_name_or_bundle_identifier or a window target"));
    }

    #[test]
    fn truncated_note_only_when_the_node_cap_was_hit() {
        let note = truncated_accessibility_tree_note(true).unwrap();
        assert!(note.contains("raise max_nodes or max_depth"));
        assert!(
            !note.contains("smaller max_nodes"),
            "shrinking the cap belongs to the unscoped warning, not the truncation note"
        );
        assert!(truncated_accessibility_tree_note(false).is_none());
    }
}

#[cfg(test)]
mod focus_probe_feedback_tests {
    use super::{focus_probe_feedback, FocusProbe, FocusedElementSummary};

    #[test]
    fn only_a_complete_empty_search_warns() {
        let none = focus_probe_feedback(&FocusProbe::NoneFocused, true);
        assert!(none.starts_with("WARNING:"), "{none}");

        for probe in [FocusProbe::Incomplete, FocusProbe::NoAccessibleApp] {
            let text = focus_probe_feedback(&probe, true);
            assert!(
                !text.contains("WARNING"),
                "{probe:?} must not claim lost input: {text}"
            );
            assert!(text.contains("could not be verified"), "{text}");
        }
        assert!(focus_probe_feedback(&FocusProbe::Incomplete, true).contains("search limits"));
        assert!(focus_probe_feedback(&FocusProbe::NoAccessibleApp, true)
            .contains("--force-renderer-accessibility"));
    }

    #[test]
    fn found_element_is_described() {
        let element = FocusedElementSummary {
            role: "text".to_string(),
            name: None,
            editable: true,
            states: vec!["focused".to_string()],
            is_terminal: false,
        };
        let text = focus_probe_feedback(&FocusProbe::Found(element), true);
        assert!(text.contains("text"), "{text}");
        assert!(!text.starts_with("WARNING"), "{text}");
    }
}

#[cfg(test)]
mod node_target_scope_tests {
    use super::*;

    fn cached_node(index: u32, object_ref: &str) -> AccessibilityNode {
        AccessibilityNode {
            index,
            parent_index: None,
            depth: 1,
            object_ref: object_ref.to_string(),
            role: "push button".to_string(),
            name: Some("Hit me".to_string()),
            description: None,
            child_count: 0,
            bounds: None,
            states: vec!["enabled".to_string()],
            actions: Vec::new(),
            value: None,
            text: None,
            supports_editable_text: false,
        }
    }

    #[tokio::test]
    async fn index_from_another_apps_snapshot_is_rejected() {
        let backend = ComputerUseLinux::default();
        let node = cached_node(2, ":1.9/org/a11y/atspi/accessible/42");
        backend.cache_snapshot(std::slice::from_ref(&node), Some(2_690_687));

        let error = backend
            .check_node_target(&node, Some(2_690_664))
            .await
            .unwrap_err();
        assert!(error.contains("pid 2690687"), "{error}");
        assert!(error.contains("target pid 2690664"), "{error}");
        assert!(
            error.contains("Call get_app_state for the target app"),
            "{error}"
        );
    }

    #[tokio::test]
    async fn same_target_or_no_target_is_allowed() {
        let backend = ComputerUseLinux::default();
        let node = cached_node(2, ":1.9/org/a11y/atspi/accessible/42");
        backend.cache_snapshot(std::slice::from_ref(&node), Some(2_690_664));

        assert!(backend
            .check_node_target(&node, Some(2_690_664))
            .await
            .is_ok());
        assert!(backend.check_node_target(&node, None).await.is_ok());
    }

    #[tokio::test]
    async fn unscoped_snapshot_with_unknown_owner_is_allowed() {
        // No snapshot pid, and the owner lookup cannot resolve this ref here,
        // so a mismatch cannot be shown and the action proceeds.
        let backend = ComputerUseLinux::default();
        let node = cached_node(2, ":1.9999/org/a11y/atspi/accessible/42");
        backend.cache_snapshot(std::slice::from_ref(&node), None);

        assert!(backend.check_node_target(&node, Some(1)).await.is_ok());
    }

    #[tokio::test]
    async fn concurrent_snapshot_commit_waits_until_checked_action_finishes() {
        let backend = ComputerUseLinux::default();
        let mut original = cached_node(2, ":1.9/org/a11y/atspi/accessible/42");
        original.bounds = Some(Bounds {
            x: 10,
            y: 20,
            width: 100,
            height: 40,
        });
        original.actions = vec![AccessibilityAction {
            index: 0,
            name: "click".into(),
            description: String::new(),
            keybinding: String::new(),
        }];
        backend
            .commit_snapshot(std::slice::from_ref(&original), Some(10))
            .await;
        // Model the click/scroll operation: it holds this same lock while its
        // target ownership check awaits and until the action has been resolved.
        let input_guard = Arc::clone(&backend.input_operation_lock).lock_owned().await;
        let checked_node = backend
            .cached_node_for(
                Some(2),
                &ElementSelector::default(),
                ElementResolvePurpose::Click,
            )
            .unwrap();
        let barrier = Arc::new(tokio::sync::Barrier::new(2));
        let writer_backend = backend.clone();
        let writer_barrier = Arc::clone(&barrier);
        let writer = tokio::spawn(async move {
            let replacement = cached_node(2, ":1.20/org/a11y/atspi/accessible/99");
            writer_barrier.wait().await;
            writer_backend
                .commit_snapshot(&[replacement], Some(20))
                .await;
        });
        barrier.wait().await;
        tokio::task::yield_now().await;
        assert!(
            !writer.is_finished(),
            "snapshot publication must wait for input"
        );
        assert!(backend
            .check_node_target(&checked_node, Some(10))
            .await
            .is_ok());
        let params = ClickParams {
            element_index: Some(2),
            ..Default::default()
        };
        assert!(matches!(backend.resolve_click_target(&params).unwrap(),
            ClickTarget::PrimaryAction { object_ref, .. } if object_ref == original.object_ref));
        assert_eq!(
            backend
                .resolve_optional_target_point(None, None, Some(2))
                .unwrap(),
            Some((60, 40))
        );
        // perform_action/set_value resolve the same snapshot while holding the
        // input lock; their explicit object identifiers remain independent.
        for purpose in [
            ElementResolvePurpose::Action,
            ElementResolvePurpose::SetValue,
        ] {
            assert_eq!(
                backend
                    .resolve_object_ref(Some(2), None, &ElementSelector::default(), purpose)
                    .unwrap(),
                original.object_ref
            );
        }
        drop(input_guard);
        writer.await.unwrap();
        let snapshot = backend.last_snapshot.lock().unwrap();
        assert_eq!(snapshot.pid, Some(20));
        assert_eq!(
            snapshot.nodes[0].object_ref,
            ":1.20/org/a11y/atspi/accessible/99"
        );
    }

    #[test]
    fn a_new_snapshot_replaces_the_recorded_pid() {
        let backend = ComputerUseLinux::default();
        backend.cache_snapshot(&[], Some(10));
        backend.cache_snapshot(&[], None);
        assert_eq!(backend.last_snapshot.lock().unwrap().pid, None);
        backend.cache_snapshot(&[], Some(11));
        backend.clear_cached_nodes();
        assert_eq!(backend.last_snapshot.lock().unwrap().pid, None);
    }
}