omni-dev 0.39.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, and Datadog.
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
//! `omni-dev worktrees` — a thin client for the daemon's cross-window worktree
//! registry.
//!
//! Lifecycle stays on `omni-dev daemon` (`start`/`stop`/`status`/`restart`);
//! this command sends the `worktrees` service's ops over the daemon's Unix
//! control socket: the read views (`list`, `tree`, `tree --follow`), the actions
//! (`focus`, `close`, `show-closed`), and — for typed parity with the companion
//! (#1361) — the window feed ops (`register`/`heartbeat`/`unregister`) that let a
//! scripted/headless reporter or an integration test drive the registry the way
//! the VS Code extension does from each window.

use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use chrono::Utc;
use clap::{Parser, Subcommand};
use serde_json::{json, Value};

use crate::cli::format::TableOrJson;
use crate::daemon::client::DaemonClient;
use crate::daemon::protocol::{DaemonEnvelope, DaemonReply};
use crate::daemon::server;
use crate::git::worktree_rebase::{
    self, FetchOutcome, RebaseOptions, RebaseResult, Selection, SkipReason, WorktreeOutcome,
};

/// The `worktrees` service routing key on the daemon control socket.
const SERVICE: &str = "worktrees";

/// Worktrees: see the repos/worktrees open across every VS Code window, kept
/// live by the daemon.
#[derive(Parser)]
pub struct WorktreesCommand {
    /// The worktrees subcommand to execute.
    #[command(subcommand)]
    pub command: WorktreesSubcommands,
}

/// Worktrees subcommands.
#[derive(Subcommand)]
pub enum WorktreesSubcommands {
    /// List the repos/worktrees currently open across all windows.
    List(ListCommand),
    /// Show every repository and all its worktrees, grouped by repository.
    Tree(TreeCommand),
    /// Focus (raise) the VS Code window for a worktree folder.
    Focus(FocusCommand),
    /// Close a worktree's window and, for a linked worktree, delete it.
    Close(CloseCommand),
    /// Rebase worktrees onto the remote default branch, fetching it once per repo.
    Rebase(RebaseCommand),
    /// Enqueue eligible worktrees' PRs into the GitHub merge queue.
    MergeQueue(MergeQueueCommand),
    /// Move and resize worktrees' open windows to match a reference window.
    Reposition(RepositionCommand),
    /// Signal worktrees' open windows to reload themselves.
    Reload(ReloadCommand),
    /// Show or set whether closed worktrees are shown across all windows.
    ShowClosed(ShowClosedCommand),
    /// Register a window's open worktree folders (companion feed op).
    Register(RegisterCommand),
    /// Refresh a window's liveness and read any pending close/reload directive.
    Heartbeat(HeartbeatCommand),
    /// Remove a window's registration (companion feed op).
    Unregister(UnregisterCommand),
}

impl WorktreesCommand {
    /// Executes the worktrees command.
    ///
    /// `repo` is the global `-C/--repo` location, resolved once in [`crate::cli`]
    /// and threaded down rather than re-read from the ambient CWD. Only `rebase`
    /// uses it (it is the one subcommand that acts on the local repository); the
    /// daemon-client subcommands address worktrees by absolute path instead.
    pub async fn execute(self, repo: Option<&Path>) -> Result<()> {
        match self.command {
            WorktreesSubcommands::List(cmd) => cmd.execute().await,
            WorktreesSubcommands::Tree(cmd) => cmd.execute().await,
            WorktreesSubcommands::Focus(cmd) => cmd.execute().await,
            WorktreesSubcommands::Close(cmd) => cmd.execute().await,
            WorktreesSubcommands::Rebase(cmd) => cmd.execute(repo).await,
            WorktreesSubcommands::MergeQueue(cmd) => cmd.execute().await,
            WorktreesSubcommands::Reposition(cmd) => cmd.execute().await,
            WorktreesSubcommands::Reload(cmd) => cmd.execute().await,
            WorktreesSubcommands::ShowClosed(cmd) => cmd.execute().await,
            WorktreesSubcommands::Register(cmd) => cmd.execute().await,
            WorktreesSubcommands::Heartbeat(cmd) => cmd.execute().await,
            WorktreesSubcommands::Unregister(cmd) => cmd.execute().await,
        }
    }
}

/// Lists the live cross-window set of open worktrees/repos.
#[derive(Parser)]
pub struct ListCommand {
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = TableOrJson::Table)]
    pub output: TableOrJson,
    /// Deprecated: use `-o`/`--output json` instead.
    #[arg(long, hide = true)]
    pub json: bool,
}

impl ListCommand {
    /// Executes the list command.
    pub async fn execute(mut self) -> Result<()> {
        if self.json {
            eprintln!("warning: --json is deprecated; use -o/--output json instead");
            self.output = TableOrJson::Json;
        }
        let socket = server::resolve_socket(self.socket)?;
        let result = call(&socket, "list", Value::Null).await?;
        match self.output {
            TableOrJson::Json => println!("{}", serde_json::to_string_pretty(&result)?),
            TableOrJson::Table => println!("{}", render_windows(&result)),
        }
        Ok(())
    }
}

/// Shows every repository and all of its worktrees (open or not), grouped by
/// repository — the daemon's `tree` op, which derives the repos from the open
/// windows and enumerates each repo's worktrees.
#[derive(Parser)]
pub struct TreeCommand {
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = TableOrJson::Table)]
    pub output: TableOrJson,
    /// Stream live snapshots: re-render on every change until interrupted
    /// (Ctrl-C). Uses the daemon's `subscribe` push op.
    #[arg(short = 'f', long)]
    pub follow: bool,
}

impl TreeCommand {
    /// Executes the tree command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        if self.follow {
            return follow_tree_stream(&socket, self.output).await;
        }
        let mut result = call(&socket, "tree", Value::Null).await?;
        // Ahead/behind is no longer part of the (cheap) streamed `tree` snapshot
        // (#1306); fetch it on demand for the worktrees we are about to render and
        // fold it back in, so `worktrees tree` shows the same `+ahead -behind` sync
        // state as before. Best-effort: an older daemon without the `ahead-behind`
        // op just renders `-`.
        enrich_ahead_behind(&socket, &mut result).await;
        match self.output {
            TableOrJson::Json => println!("{}", serde_json::to_string_pretty(&result)?),
            TableOrJson::Table => println!("{}", render_tree(&result)),
        }
        Ok(())
    }
}

/// Follows the daemon's `subscribe` push stream, re-rendering the tree on each
/// snapshot until the daemon closes the stream or the user interrupts (Ctrl-C).
///
/// Each frame is enriched with on-demand ahead/behind, exactly like the one-shot
/// path, so a followed view — table **or** JSON — carries the same shape as a
/// plain `tree` (the JSON stream stays one compact NDJSON frame per snapshot).
async fn follow_tree_stream(socket: &Path, output: TableOrJson) -> Result<()> {
    let mut sub = DaemonClient::new(socket)
        .subscribe(DaemonEnvelope::service(SERVICE, "subscribe", Value::Null))
        .await?;
    loop {
        tokio::select! {
            frame = sub.next() => {
                // `None` = the daemon closed the stream (shutdown); we are done.
                let Some(frame) = frame else { break };
                let mut payload = reply_payload(frame?)?;
                // Enrich before either renderer so `tree --follow` matches the
                // one-shot `tree` byte-for-byte in JSON and column-for-column in
                // the table (the one-shot enriches ahead of both branches too).
                enrich_ahead_behind(socket, &mut payload).await;
                match output {
                    // A compact one-line frame per snapshot (an NDJSON stream).
                    TableOrJson::Json => println!("{}", serde_json::to_string(&payload)?),
                    TableOrJson::Table => println!("{}", render_tree(&payload)),
                }
            }
            // Ctrl-C ends the follow; dropping `sub` closes the connection,
            // which the daemon reads as the stream's teardown.
            _ = tokio::signal::ctrl_c() => break,
        }
    }
    Ok(())
}

/// Focuses (raises) the VS Code window for a worktree folder.
///
/// Reuses the daemon's `open` op — the same launcher path the macOS tray's
/// per-window "focus" action drives (`OMNI_DEV_VSCODE_BIN` → well-known paths →
/// `code`), which VS Code uses to reuse an already-open window. This makes that
/// tray-only capability reachable from the CLI on Linux/headless too (#1113).
#[derive(Parser)]
pub struct FocusCommand {
    /// Worktree folder whose window to focus. Shown by `worktrees tree`/`list`.
    #[arg(value_name = "PATH")]
    pub path: PathBuf,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl FocusCommand {
    /// Executes the focus command.
    pub async fn execute(self) -> Result<()> {
        // Resolve to an absolute path client-side: the daemon runs in a different
        // cwd and guards the `open` path as absolute-and-existing, so a relative
        // path would be meaningless there. A clear error here beats the daemon's.
        let path = std::fs::canonicalize(&self.path)
            .with_context(|| format!("cannot resolve worktree path: {}", self.path.display()))?;
        let socket = server::resolve_socket(self.socket)?;
        call(&socket, "open", json!({ "path": path.to_string_lossy() })).await?;
        println!("Focused {}", path.display());
        Ok(())
    }
}

/// Closes a worktree's window and, for a linked worktree, deletes it — the
/// daemon's two-phase `close` op driven from the CLI.
///
/// A CLI process is never a VS Code window, so it omits `requester_key`: the
/// daemon then treats the close as cross-window, signalling every owning window
/// to close and waiting (bounded ~20s) for them to unregister before it prunes.
/// All destructive/git logic (the `git2` prune, the main-tree refusal) stays in
/// the daemon (ADR-0049); the CLI adds no new authority.
#[derive(Parser)]
pub struct CloseCommand {
    /// Worktree folder to close. A linked worktree is deleted; the main working
    /// tree only has its window closed (never deleted).
    #[arg(value_name = "PATH")]
    pub path: PathBuf,
    /// Only close the worktree's window(s); never delete the worktree.
    #[arg(long)]
    pub window_only: bool,
    /// Run the safety check and print the report, but do not close or delete.
    #[arg(long)]
    pub dry_run: bool,
    /// Skip the interactive confirmation before deleting.
    #[arg(short = 'y', long)]
    pub yes: bool,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl CloseCommand {
    /// Executes the close command, confirming a delete interactively via stdin.
    pub async fn execute(self) -> Result<()> {
        self.execute_with(confirm_removal).await
    }

    /// The close core, with the destructive-confirm decision injected as
    /// `confirm(has_risks) -> bool`. Splitting it this way keeps the abort and
    /// confirmed-execute branches unit-testable without driving real stdin (which
    /// would block a test on a TTY); production wires in [`confirm_removal`].
    async fn execute_with<F, Fut>(self, confirm: F) -> Result<()>
    where
        F: FnOnce(bool) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        // Resolve to an absolute path client-side (like `focus`): the daemon runs
        // in a different cwd and matches the target by canonical path.
        let path = std::fs::canonicalize(&self.path)
            .with_context(|| format!("cannot resolve worktree path: {}", self.path.display()))?;
        let path_str = path.to_string_lossy().to_string();
        let socket = server::resolve_socket(self.socket)?;

        // "Close Window": non-destructive, no safety check — the daemon closes the
        // owning window(s) and never inspects git. `--dry-run` is honoured here
        // too, so the combination never has a side effect.
        if self.window_only {
            if self.dry_run {
                println!(
                    "Would close the window for {} (dry run; nothing closed)",
                    path.display()
                );
                return Ok(());
            }
            call(
                &socket,
                "close",
                json!({ "path": path_str, "remove": false }),
            )
            .await?;
            println!("Closed the window for {}", path.display());
            return Ok(());
        }

        // Phase 1: the side-effect-free safety check (remove:true, unconfirmed).
        let report = call(
            &socket,
            "close",
            json!({ "path": path_str, "remove": true }),
        )
        .await?;
        println!("{}", render_safety_report(&path, &report));

        if self.dry_run {
            return Ok(());
        }
        // The daemon refuses to remove the main working tree; fail fast rather than
        // send a phase-2 execute it would reject.
        if report.get("removable").and_then(Value::as_bool) != Some(true) {
            bail!(
                "{} is not a removable worktree (nothing deleted); \
                 use --window-only to just close its window",
                path.display()
            );
        }
        let has_risks = report
            .get("risks")
            .and_then(Value::as_array)
            .is_some_and(|r| !r.is_empty());
        if !self.yes && !confirm(has_risks).await {
            println!("Aborted; nothing was deleted.");
            return Ok(());
        }

        // Phase 2: execute the delete.
        call(
            &socket,
            "close",
            json!({ "path": path_str, "remove": true, "confirmed": true }),
        )
        .await?;
        println!("Deleted worktree {}", path.display());
        Ok(())
    }
}

/// Rebases worktrees onto the repository's remote default branch, fetching that
/// branch **exactly once per repository** (#1400).
///
/// Unlike every other `worktrees` subcommand this runs **entirely locally** and
/// never talks to the daemon — **by choice, not by necessity** (ADR-0059). The
/// daemon hosts the same engine behind its two-phase `rebase` op, which is what
/// the tree view's "Rebase on main" drives; keeping this command local is what
/// makes a batch rebase work with **no daemon running at all**, and keeps
/// `--onto`/`--all` (CLI-only concerns) out of the wire protocol. The git work
/// lives in [`crate::git::worktree_rebase`]; see ADR-0059, ADR-0055, ADR-0003.
///
/// A rebase rewrites branch history, so it confirms by default (`--dry-run` to
/// preview, `-y` to skip the prompt) in the spirit of ADR-0027.
#[derive(Parser)]
pub struct RebaseCommand {
    /// Worktree folders to rebase. Omit these and pass `--all` to rebase every
    /// linked worktree of the current repository.
    #[arg(value_name = "PATH")]
    pub paths: Vec<PathBuf>,
    /// Rebase every linked worktree of the current repository (never the main
    /// working tree).
    #[arg(long)]
    pub all: bool,
    /// Rebase onto this ref instead of the remote default branch. A
    /// `<remote>/<branch>` value is still fetched once up front.
    #[arg(long, value_name = "REF")]
    pub onto: Option<String>,
    /// Stash uncommitted changes around each rebase instead of skipping the
    /// worktree.
    #[arg(long)]
    pub autostash: bool,
    /// Fetch and report what would be rebased, but rebase nothing.
    #[arg(long)]
    pub dry_run: bool,
    /// Leave a conflicting worktree mid-rebase to resolve in place, instead of
    /// aborting it back to its previous state.
    #[arg(long)]
    pub keep_conflicts: bool,
    /// Skip the interactive confirmation before rebasing.
    #[arg(short = 'y', long)]
    pub yes: bool,
    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = TableOrJson::Table)]
    pub output: TableOrJson,
}

impl RebaseCommand {
    /// Executes the rebase command, confirming interactively via stdin.
    pub async fn execute(self, repo: Option<&Path>) -> Result<()> {
        self.execute_with(repo, confirm_rebase).await
    }

    /// The rebase core, with the confirm decision injected as
    /// `confirm(pending) -> bool`. Splitting it this way keeps the abort and
    /// confirmed branches unit-testable without driving real stdin (which would
    /// block a test on a TTY); production wires in [`confirm_rebase`].
    async fn execute_with<F, Fut>(self, repo: Option<&Path>, confirm: F) -> Result<()>
    where
        F: FnOnce(usize) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let selection = self.selection(repo)?;
        let opts = RebaseOptions {
            onto: self.onto.clone(),
            autostash: self.autostash,
            dry_run: self.dry_run,
            keep_conflicts: self.keep_conflicts,
            // Resolved by the engine. Unlike the daemon, the CLI runs in the
            // user's shell with their own `PATH`, so the well-known-path probe is
            // belt-and-braces here rather than load-bearing.
            git_bin: None,
        };

        // Planning shells out to `git fetch` (once per repo) and walks the object
        // database, so it runs on a blocking thread rather than an async worker.
        let plan_opts = opts.clone();
        let plan =
            tokio::task::spawn_blocking(move || worktree_rebase::plan(&selection, &plan_opts))
                .await
                .context("rebase planning task panicked")??;

        let json = matches!(self.output, TableOrJson::Json);
        // A dry run, or a plan with nothing left to do, reports and stops. The
        // fetch has still happened — that is what pins the snapshot every worktree
        // was measured against.
        if self.dry_run || !plan.has_pending_rebases() {
            self.print(json, &plan.fetches, &plan.worktrees)?;
            return Ok(());
        }

        // Show what is about to happen, then confirm: a rebase rewrites history.
        if !json {
            println!("{}", render_fetches(&plan.fetches));
            println!("{}", render_outcomes(&plan.worktrees));
        }
        let pending = plan.worktrees.iter().filter(|w| is_pending(w)).count();
        if !self.yes && !confirm(pending).await {
            println!("Aborted; no worktree was rebased.");
            return Ok(());
        }

        let fetches = plan.fetches.clone();
        let outcomes = tokio::task::spawn_blocking(move || worktree_rebase::execute(plan, &opts))
            .await
            .context("rebase task panicked")?;
        if !json {
            println!();
        }
        self.print(json, &fetches, &outcomes)
    }

    /// Resolves the CLI's target selection, rejecting an empty one rather than
    /// silently rebasing everything.
    ///
    /// `repo` is the global `-C/--repo` location: it is both the repository
    /// `--all` enumerates and the base that relative `<PATH>` arguments resolve
    /// against, so the command behaves "as if started in `<PATH>`". With no
    /// `-C` the base is `.`, which git resolves against the process CWD.
    fn selection(&self, repo: Option<&Path>) -> Result<Selection> {
        let base = repo.map_or_else(|| PathBuf::from("."), Path::to_path_buf);
        if self.all {
            if !self.paths.is_empty() {
                bail!("pass either <PATH>... or --all, not both");
            }
            return Ok(Selection::All { base });
        }
        if self.paths.is_empty() {
            bail!(
                "specify one or more <PATH> arguments, or --all to rebase \
                 every linked worktree of this repository"
            );
        }
        let paths = self
            .paths
            .iter()
            .map(|path| {
                if path.is_absolute() {
                    path.clone()
                } else {
                    base.join(path)
                }
            })
            .collect();
        Ok(Selection::Paths(paths))
    }

    /// Prints a report as either pretty JSON or the human table.
    fn print(
        &self,
        json: bool,
        fetches: &[FetchOutcome],
        outcomes: &[WorktreeOutcome],
    ) -> Result<()> {
        if json {
            let value = json!({
                "dry_run": self.dry_run,
                "fetches": fetches,
                "worktrees": outcomes,
            });
            println!("{}", serde_json::to_string_pretty(&value)?);
        } else {
            println!("{}", render_fetches(fetches));
            println!("{}", render_outcomes(outcomes));
        }
        Ok(())
    }
}

/// Whether an outcome is still awaiting a rebase (drives the confirm count).
fn is_pending(outcome: &WorktreeOutcome) -> bool {
    matches!(outcome.result, RebaseResult::WouldRebase { .. })
}

/// Renders the per-repository fetch lines — one per repo, which is the visible
/// proof of the fetch-once-per-repo contract.
fn render_fetches(fetches: &[FetchOutcome]) -> String {
    if fetches.is_empty() {
        return "No repository selected.".to_string();
    }
    fetches
        .iter()
        .map(fetch_line)
        .collect::<Vec<_>>()
        .join("\n")
}

/// One repository's fetch line.
fn fetch_line(fetch: &FetchOutcome) -> String {
    let root = sanitize(&fetch.repo_root.display().to_string());
    let onto = sanitize(&fetch.onto);
    if !fetch.fetched {
        return format!("Using {onto} in {root} (local ref; nothing fetched)");
    }
    if fetch.ok {
        format!("Fetched {onto} once for {root}")
    } else {
        let detail = brief(fetch.detail.as_deref().unwrap_or(""));
        format!("Fetch of {onto} FAILED for {root}: {detail}")
    }
}

/// Renders the per-worktree result table.
fn render_outcomes(outcomes: &[WorktreeOutcome]) -> String {
    if outcomes.is_empty() {
        return "No worktrees selected.".to_string();
    }
    let mut out = format!(
        "{:<12} {:<24} {:<16} {}",
        "STATUS", "BRANCH", "ONTO", "WORKTREE"
    );
    for outcome in outcomes {
        out.push('\n');
        out.push_str(&outcome_row(outcome));
    }
    out
}

/// One worktree row: status, branch, target ref, path, and a parenthesised detail.
fn outcome_row(outcome: &WorktreeOutcome) -> String {
    let (status, detail) = status_and_detail(&outcome.result);
    let branch = sanitize(outcome.branch.as_deref().unwrap_or("-"));
    let onto = sanitize(&outcome.onto);
    let path = sanitize(&outcome.path.display().to_string());
    let suffix = if detail.is_empty() {
        String::new()
    } else {
        format!("  ({detail})")
    };
    format!("{status:<12} {branch:<24} {onto:<16} {path}{suffix}")
}

/// The status word and human detail for one outcome.
fn status_and_detail(result: &RebaseResult) -> (&'static str, String) {
    match result {
        RebaseResult::Rebased { behind } => ("rebased", format!("was {behind} behind")),
        RebaseResult::WouldRebase { behind } => ("would-rebase", format!("{behind} behind")),
        RebaseResult::UpToDate => ("up-to-date", String::new()),
        RebaseResult::Skipped { reason } => ("skipped", skip_reason_text(*reason).to_string()),
        // A left-in-place conflict is a *different instruction to the user* than an
        // aborted one — the worktree is still mid-rebase and needs finishing — so
        // the row says so instead of only quoting git's error.
        RebaseResult::Conflict {
            detail,
            left_in_place: true,
        } => (
            "conflict",
            format!(
                "left in place; resolve then `git rebase --continue`: {}",
                brief(detail)
            ),
        ),
        RebaseResult::Conflict { detail, .. } => ("conflict", brief(detail)),
        RebaseResult::FetchFailed { detail } => ("fetch-failed", brief(detail)),
    }
}

/// The human explanation for why a worktree was skipped.
fn skip_reason_text(reason: SkipReason) -> &'static str {
    match reason {
        SkipReason::MainWorkingTree => "main working tree",
        SkipReason::DetachedHead => "detached HEAD",
        SkipReason::Dirty => "uncommitted changes; pass --autostash",
        SkipReason::OperationInProgress => "a rebase/merge is already in progress",
        SkipReason::NotAWorktree => "not a git worktree",
        SkipReason::NoOntoRef => "could not resolve the target ref",
    }
}

/// A one-line, control-character-free, length-capped summary of a multi-line git
/// error, so a long conflict message cannot wreck the table layout.
fn brief(detail: &str) -> String {
    let first = detail
        .lines()
        .find(|line| !line.trim().is_empty())
        .unwrap_or("");
    let clean = sanitize(first.trim());
    if clean.chars().count() > 100 {
        let truncated: String = clean.chars().take(97).collect();
        format!("{truncated}...")
    } else {
        clean
    }
}

/// Prompts on stderr before rewriting branch history, reading from real stdin.
async fn confirm_rebase(pending: usize) -> bool {
    confirm_rebase_with(pending, read_stdin_line()).await
}

/// Prints the rebase confirmation prompt and resolves the (injected) read into a
/// yes/no decision. Any read error, EOF, or join failure is treated as "no", so a
/// rebase never proceeds unattended.
async fn confirm_rebase_with(
    pending: usize,
    read: impl std::future::Future<Output = Option<String>>,
) -> bool {
    use std::io::Write;
    eprint!("{}", rebase_prompt(pending));
    let _ = std::io::stderr().flush();
    read.await.as_deref().is_some_and(answer_is_yes)
}

/// The confirmation prompt, naming how many worktrees would be rewritten. Pure, so
/// the wording is unit-testable.
fn rebase_prompt(pending: usize) -> String {
    let noun = if pending == 1 {
        "worktree"
    } else {
        "worktrees"
    };
    format!("Rebase {pending} {noun} (this rewrites branch history)? [y/N] ")
}

/// Enqueues eligible worktrees' PRs into the GitHub merge queue — the daemon's
/// two-phase `merge-queue` op driven from the CLI (#1401).
///
/// Only worktrees that pass every eligibility gate (clean, committed, pushed, with
/// an open non-draft, conflict-free, CI-green PR) are enqueued; the rest are
/// reported as skipped-with-reason. Like `close`, the daemon re-validates on
/// execute, so the CLI adds no authority — enqueue authenticates through the
/// user's own `gh`.
#[derive(Parser)]
pub struct MergeQueueCommand {
    /// Worktree folder(s) to consider. Each is canonicalized client-side, as the
    /// daemon runs in a different cwd and matches targets by canonical path.
    #[arg(value_name = "PATH", required = true)]
    pub paths: Vec<PathBuf>,
    /// Print the eligibility report and exit; never enqueue.
    #[arg(long)]
    pub check: bool,
    /// Skip the interactive confirmation before enqueuing.
    #[arg(short = 'y', long)]
    pub yes: bool,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl MergeQueueCommand {
    /// Executes the merge-queue command, confirming the enqueue interactively via
    /// stdin.
    pub async fn execute(self) -> Result<()> {
        self.execute_with(confirm_enqueue).await
    }

    /// The merge-queue core, with the confirm decision injected as
    /// `confirm(eligible_count) -> bool`, so the abort and confirmed-execute
    /// branches are unit-testable without driving real stdin (which would block a
    /// test on a TTY); production wires in [`confirm_enqueue`].
    async fn execute_with<F, Fut>(self, confirm: F) -> Result<()>
    where
        F: FnOnce(usize) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        // Canonicalize every path client-side (like `close`/`focus`): the daemon
        // runs in a different cwd and matches targets by canonical path.
        let mut paths = Vec::with_capacity(self.paths.len());
        for p in &self.paths {
            let abs = std::fs::canonicalize(p)
                .with_context(|| format!("cannot resolve worktree path: {}", p.display()))?;
            paths.push(abs.to_string_lossy().to_string());
        }
        let socket = server::resolve_socket(self.socket)?;

        // Phase 1: the side-effect-free eligibility check.
        let report = call(
            &socket,
            "merge-queue",
            json!({ "paths": paths, "check": true }),
        )
        .await?;
        println!("{}", render_eligibility_report(&report));

        if self.check {
            return Ok(());
        }
        let eligible = report
            .get("eligible")
            .and_then(Value::as_array)
            .map_or(0, Vec::len);
        if eligible == 0 {
            println!("Nothing to enqueue.");
            return Ok(());
        }
        if !self.yes && !confirm(eligible).await {
            println!("Aborted; nothing was enqueued.");
            return Ok(());
        }

        // Phase 2: execute the enqueue (the daemon re-validates eligibility).
        let result = call(
            &socket,
            "merge-queue",
            json!({ "paths": paths, "confirmed": true }),
        )
        .await?;
        println!("{}", render_enqueue_result(&result));
        Ok(())
    }
}

/// Moves and resizes worktrees' open VS Code windows to match a reference
/// window's geometry (#1407).
///
/// The CLI counterpart of the tree view's "Reposition Windows to Match", and the
/// diagnostic surface for it: `--dry-run` reports exactly which OS window each
/// worktree resolved to without touching anything, which is how a title-matching
/// problem is diagnosed.
///
/// Paths, not window keys, are the CLI's currency (as for `focus`/`close`), so a
/// `list` call up front maps each folder to the key of the window that has it
/// open. The daemon does all the OS work — it holds the macOS Accessibility grant,
/// which a terminal-launched process would not.
#[derive(Parser)]
pub struct RepositionCommand {
    /// Worktree folders whose windows to move. Each is canonicalized client-side,
    /// as the daemon runs in a different cwd and matches by canonical path.
    #[arg(value_name = "PATH")]
    pub paths: Vec<PathBuf>,
    /// The worktree whose window supplies the target position and size. It is
    /// never itself moved.
    #[arg(
        long,
        value_name = "PATH",
        required_unless_present = "undo",
        conflicts_with = "undo"
    )]
    pub reference: Option<PathBuf>,
    /// Report which window each worktree resolves to and stop; move nothing.
    #[arg(long, conflicts_with = "undo")]
    pub dry_run: bool,
    /// Put the windows the last reposition moved back where they were.
    #[arg(long)]
    pub undo: bool,
    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = TableOrJson::Table)]
    pub output: TableOrJson,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl RepositionCommand {
    /// Executes the reposition command.
    pub async fn execute(self) -> Result<()> {
        let output = self.output;
        let socket = server::resolve_socket(self.socket)?;
        if self.undo {
            let reply = call(&socket, "reposition-undo", Value::Null).await?;
            return print_reposition(output, &reply);
        }
        // `required_unless_present` guarantees this on the non-undo path.
        let Some(reference) = self.reference.as_deref() else {
            bail!("`reposition` requires `--reference <PATH>`");
        };

        // Resolve folders to the keys of the windows that have them open. The op
        // addresses *windows*, since geometry belongs to the OS window rather than
        // to the worktree, and only the registry knows which window that is.
        let windows = call(&socket, "list", Value::Null).await?;
        let reference_key = window_key_for(&windows, reference, "repositioned")?;
        let mut target_keys = Vec::with_capacity(self.paths.len());
        for path in &self.paths {
            target_keys.push(window_key_for(&windows, path, "repositioned")?);
        }

        let reply = call(
            &socket,
            "reposition",
            json!({
                "reference_key": reference_key,
                "target_keys": target_keys,
                "check": self.dry_run,
            }),
        )
        .await?;
        print_reposition(output, &reply)
    }
}

/// Prints a `reposition` / `reposition-undo` reply in the requested format.
fn print_reposition(output: TableOrJson, reply: &Value) -> Result<()> {
    match output {
        TableOrJson::Json => println!("{}", serde_json::to_string_pretty(reply)?),
        TableOrJson::Table => println!("{}", render_reposition(reply)),
    }
    Ok(())
}

/// Signals worktrees' open VS Code windows to reload themselves (#1417).
///
/// The CLI counterpart of the tree view's "Reload Window" — the batch form of
/// `Developer: Reload Window`, which otherwise has to be run by hand in each
/// window in turn.
///
/// Addresses windows by key like `reposition`, so a `list` call up front maps
/// each folder to the key of the window that has it open. Unlike the tree view,
/// a worktree with **no** open window is an error rather than a silent skip:
/// there the selection is a sweep, here the user named each target explicitly.
///
/// The daemon only marks a directive per target; each window acts on it on its
/// next heartbeat, up to ~10s later. Nothing here waits for that, which is why
/// the output says how many windows were *signalled*.
#[derive(Parser)]
pub struct ReloadCommand {
    /// Worktree folders whose windows to reload. Each is canonicalized
    /// client-side, as the daemon runs in a different cwd and matches by
    /// canonical path.
    #[arg(value_name = "PATH", required = true)]
    pub paths: Vec<PathBuf>,
    /// Output format.
    #[arg(short = 'o', long, value_enum, default_value_t = TableOrJson::Table)]
    pub output: TableOrJson,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl ReloadCommand {
    /// Executes the reload command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        let windows = call(&socket, "list", Value::Null).await?;
        let mut target_keys = Vec::with_capacity(self.paths.len());
        for path in &self.paths {
            target_keys.push(window_key_for(&windows, path, "reloaded")?);
        }

        let reply = call(&socket, "reload", json!({ "target_keys": target_keys })).await?;
        match self.output {
            TableOrJson::Json => println!("{}", serde_json::to_string_pretty(&reply)?),
            TableOrJson::Table => println!("{}", render_reload(&reply)),
        }
        Ok(())
    }
}

/// Renders a `reload` reply as a one-line human summary.
///
/// Says "Signalled", never "Reloaded": the directive rides each window's ~10s
/// heartbeat, so when this prints nothing has reloaded yet — and the daemon
/// could not observe it if it had, since the window re-registers under the same
/// key. Any key the daemon had no live window for is named rather than dropped.
fn render_reload(reply: &Value) -> String {
    let requested = reply.get("requested").and_then(Value::as_u64).unwrap_or(0);
    let signalled = reply.get("signalled").and_then(Value::as_u64).unwrap_or(0);
    let unknown: Vec<String> = reply
        .get("unknown")
        .and_then(Value::as_array)
        .map(|keys| {
            keys.iter()
                .filter_map(Value::as_str)
                .map(sanitize)
                .collect()
        })
        .unwrap_or_default();

    // The noun agrees with `requested`, the number it sits next to: "1 of 3
    // windows", not "1 of 3 window".
    let noun = if requested == 1 { "window" } else { "windows" };
    let mut out = format!("Signalled {signalled} of {requested} {noun} to reload.");
    if !unknown.is_empty() {
        // A window that closed between the `list` above and the op landing. Rare
        // but real, and never worth swallowing.
        out.push_str(&format!(
            "\nNo longer open, so not signalled: {}",
            unknown.join(", ")
        ));
    }
    out
}

/// Finds the registry key of the window that has `path` open.
///
/// Canonicalizes client-side and compares against each window's canonicalized
/// folders, the same convention `close`/`focus` use. A worktree with no open
/// window is an error rather than a silent skip: the CLI names its targets one by
/// one, so an unmatched one is a mistake worth reporting, unlike a multi-select in
/// the tree view where a stale row is expected.
///
/// `verb` is the past participle of the caller's action ("repositioned",
/// "reloaded"), so the error names what the user was actually trying to do.
fn window_key_for(windows: &Value, path: &Path, verb: &str) -> Result<String> {
    let wanted = std::fs::canonicalize(path)
        .with_context(|| format!("cannot resolve worktree path: {}", path.display()))?;
    windows
        .get("windows")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default()
        .iter()
        .find(|window| {
            window
                .get("folders")
                .and_then(Value::as_array)
                .is_some_and(|folders| {
                    folders.iter().filter_map(Value::as_str).any(|folder| {
                        std::fs::canonicalize(folder).is_ok_and(|folder| folder == wanted)
                    })
                })
        })
        .and_then(|window| window.get("key").and_then(Value::as_str))
        .map(ToString::to_string)
        .ok_or_else(|| {
            anyhow::anyhow!(
                "no VS Code window has {} open (only open windows can be {verb})",
                wanted.display()
            )
        })
}

/// Renders a `reposition` / `reposition-undo` reply as a human-readable report:
/// the permission state, the reference geometry, a summary count, and one line per
/// target with its outcome.
fn render_reposition(reply: &Value) -> String {
    if reply.get("trusted").and_then(Value::as_bool) == Some(false) {
        return "omni-dev does not hold the macOS Accessibility permission, so no window \
                was touched.\nGrant it in System Settings → Privacy & Security → \
                Accessibility (add the omni-dev binary), then run `omni-dev daemon restart`."
            .to_string();
    }
    if let Some(blocked) = reply.get("blocked") {
        let reason = sanitize(blocked.get("reason").and_then(Value::as_str).unwrap_or("-"));
        let detail = sanitize(blocked.get("detail").and_then(Value::as_str).unwrap_or(""));
        return format!("Nothing was moved [{reason}]: {detail}");
    }

    let moved = reply.get("moved").and_then(Value::as_u64).unwrap_or(0);
    let skipped = reply.get("skipped").and_then(Value::as_u64).unwrap_or(0);
    let mut out = String::new();
    if let Some(reference) = reply.get("reference") {
        let title = sanitize(
            reference
                .get("title")
                .and_then(Value::as_str)
                .unwrap_or("-"),
        );
        out.push_str(&format!(
            "Reference: {title} {}\n",
            render_frame(reference.get("frame"))
        ));
    }
    out.push_str(&format!("Moved: {moved} / Skipped: {skipped}"));
    let results = reply
        .get("results")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    for result in results {
        let outcome = sanitize(result.get("outcome").and_then(Value::as_str).unwrap_or("-"));
        let title = sanitize(
            result
                .get("title")
                .and_then(Value::as_str)
                .or_else(|| result.get("key").and_then(Value::as_str))
                .unwrap_or("-"),
        );
        let detail = sanitize(result.get("detail").and_then(Value::as_str).unwrap_or(""));
        out.push_str(&format!("\n  {outcome}: {title}{detail}"));
    }
    if results.is_empty() {
        out.push_str("\n  (nothing to report)");
    }
    out
}

/// Renders a frame as `WxH at (X, Y)`, or `-` when absent.
fn render_frame(frame: Option<&Value>) -> String {
    let Some(frame) = frame else {
        return "-".to_string();
    };
    let field = |name: &str| {
        frame
            .get(name)
            .and_then(Value::as_f64)
            .unwrap_or(0.0)
            .round()
    };
    format!(
        "{}×{} at ({}, {})",
        field("width"),
        field("height"),
        field("x"),
        field("y")
    )
}

/// Shows or sets the cross-window "show closed worktrees" toggle.
///
/// With a boolean argument it sets the daemon-backed value (`set-show-closed`),
/// which every subscribed window re-reads; with no argument it reads the current
/// value from the top-level `show_closed` of a `tree` snapshot.
#[derive(Parser)]
pub struct ShowClosedCommand {
    /// New value (`true`/`false`). Omit to read the current value.
    #[arg(value_name = "BOOL", value_parser = clap::builder::BoolishValueParser::new())]
    pub value: Option<bool>,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl ShowClosedCommand {
    /// Executes the show-closed command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        if let Some(show_closed) = self.value {
            call(
                &socket,
                "set-show-closed",
                json!({ "show_closed": show_closed }),
            )
            .await?;
            println!("show-closed: {show_closed}");
        } else {
            // The value is not a dedicated op — it rides the `tree` snapshot.
            let tree = call(&socket, "tree", Value::Null).await?;
            let current = tree
                .get("show_closed")
                .and_then(Value::as_bool)
                .unwrap_or(true);
            println!("show-closed: {current}");
        }
        Ok(())
    }
}

/// Registers a window's open worktree folders (a companion feed op).
///
/// Exposed as a typed command so scripted/headless reporters and integration
/// tests can drive the registry the way the VS Code companion does. Mirrors
/// `RegisterRequest`.
#[derive(Parser)]
pub struct RegisterCommand {
    /// Stable per-window identity (the companion generates a per-activate UUID).
    #[arg(long, value_name = "KEY")]
    pub key: String,
    /// A workspace-folder path (repeatable).
    #[arg(long = "folder", value_name = "PATH")]
    pub folders: Vec<PathBuf>,
    /// Repository root or name, when the window has one.
    // Named `repo_name`, not `repo`, and so spelled `--repo-name`: clap
    // propagates a `global = true` arg by **arg id**, and the derive's id is the
    // field name. A local `repo` id therefore displaced the global `-C/--repo`
    // under this subcommand and its `String` was copied back into the root
    // matches, panicking `Cli`'s `PathBuf` read (#1420). Renaming only the long
    // spelling would not have been enough. The wire key stays `repo`.
    #[arg(long, value_name = "REPO")]
    pub repo_name: Option<String>,
    /// Window title, for display.
    #[arg(long, value_name = "TITLE")]
    pub title: Option<String>,
    /// Reporting process id.
    #[arg(long, value_name = "PID")]
    pub pid: Option<u32>,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl RegisterCommand {
    /// Executes the register command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        let payload = json!({
            "key": self.key,
            "folders": self.folders,
            "repo": self.repo_name,
            "title": self.title,
            "pid": self.pid,
        });
        call(&socket, "register", payload).await?;
        println!("Registered {}", self.key);
        Ok(())
    }
}

/// Refreshes a window's liveness and reports the daemon's reply.
///
/// A companion feed op made typed: the reply carries `known` (false asks the
/// window to re-register after a daemon restart) and, when present, the
/// cross-window directives `close` and `reload`. Both are omitted from the reply
/// when nothing is pending, and both read as `false` here when absent.
#[derive(Parser)]
pub struct HeartbeatCommand {
    /// The window key to heartbeat.
    #[arg(long, value_name = "KEY")]
    pub key: String,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl HeartbeatCommand {
    /// Executes the heartbeat command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        let reply = call(&socket, "heartbeat", json!({ "key": self.key })).await?;
        let known = reply.get("known").and_then(Value::as_bool).unwrap_or(false);
        // Both directives are omitted from the reply when false; treat absent as
        // false, which is also what a pre-#1417 daemon's reply reads as.
        let close = reply.get("close").and_then(Value::as_bool).unwrap_or(false);
        let reload = reply
            .get("reload")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        println!("known: {known}");
        println!("close: {close}");
        println!("reload: {reload}");
        Ok(())
    }
}

/// Removes a window's registration — a companion feed op made typed. Prints
/// whether an entry was actually removed.
#[derive(Parser)]
pub struct UnregisterCommand {
    /// The window key to unregister.
    #[arg(long, value_name = "KEY")]
    pub key: String,
    /// Control-socket path. Defaults to the per-user runtime location.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl UnregisterCommand {
    /// Executes the unregister command.
    pub async fn execute(self) -> Result<()> {
        let socket = server::resolve_socket(self.socket)?;
        let reply = call(&socket, "unregister", json!({ "key": self.key })).await?;
        let removed = reply
            .get("removed")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        println!("removed: {removed}");
        Ok(())
    }
}

/// Renders a phase-1 `close` `SafetyReport` as a human-readable block: whether
/// the target is removable, whether it is the main tree, whether a window has it
/// open (and which), and any `risks`/`info` notes. Every daemon-supplied string is
/// `sanitize`d (#1137); the booleans/counts are daemon-computed and safe.
fn render_safety_report(path: &Path, report: &Value) -> String {
    let removable = report
        .get("removable")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let is_main = report
        .get("is_main")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let open = report.get("open").and_then(Value::as_bool).unwrap_or(false);
    let mut out = format!("Worktree: {}", path.display());
    out.push_str(&format!("\n  removable:        {removable}"));
    out.push_str(&format!("\n  main working tree: {is_main}"));
    if open {
        let key = sanitize(
            report
                .get("window_key")
                .and_then(Value::as_str)
                .unwrap_or("-"),
        );
        let count = report
            .get("window_folder_count")
            .and_then(Value::as_u64)
            .unwrap_or(0);
        out.push_str(&format!(
            "\n  open in a window:  yes (key {key}, {count} folder(s))"
        ));
    } else {
        out.push_str("\n  open in a window:  no");
    }
    out.push_str(&render_notes("risks", report.get("risks")));
    out.push_str(&render_notes("info", report.get("info")));
    out
}

/// Renders a labelled list of `close` safety notes (`risks` or `info`), each a
/// `- [kind] detail` line with both fields `sanitize`d. Empty when there are none.
fn render_notes(label: &str, notes: Option<&Value>) -> String {
    let notes = notes
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    if notes.is_empty() {
        return String::new();
    }
    let mut out = format!("\n  {label}:");
    for note in notes {
        let kind = sanitize(note.get("kind").and_then(Value::as_str).unwrap_or("-"));
        let detail = sanitize(note.get("detail").and_then(Value::as_str).unwrap_or(""));
        out.push_str(&format!("\n    - [{kind}] {detail}"));
    }
    out
}

/// Renders a `merge-queue` phase-1 `EligibilityReport`: a summary count, then one
/// line per enqueue-eligible worktree (`PR #N [branch] path`) and per
/// skipped-with-reason worktree (`[kind] path — detail`). Every daemon-supplied
/// string is `sanitize`d (#1137); the counts are daemon-computed and safe.
fn render_eligibility_report(report: &Value) -> String {
    let eligible = report
        .get("eligible")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let skipped = report
        .get("skipped")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let mut out = format!("Eligible: {} / Skipped: {}", eligible.len(), skipped.len());
    for pr in eligible {
        let number = pr.get("number").and_then(Value::as_u64).unwrap_or(0);
        let branch = sanitize(pr.get("branch").and_then(Value::as_str).unwrap_or("-"));
        let path = sanitize(pr.get("path").and_then(Value::as_str).unwrap_or(""));
        out.push_str(&format!("\n  eligible: PR #{number} [{branch}] {path}"));
    }
    for skip in skipped {
        let kind = sanitize(skip.get("kind").and_then(Value::as_str).unwrap_or("-"));
        let detail = sanitize(skip.get("detail").and_then(Value::as_str).unwrap_or(""));
        let path = sanitize(skip.get("path").and_then(Value::as_str).unwrap_or(""));
        out.push_str(&format!("\n  skipped [{kind}]: {path}{detail}"));
    }
    out
}

/// Renders a `merge-queue` phase-2 `EnqueueResult`: a summary count, then one line
/// per queued PR (`PR #N`, with `(already queued)` for an idempotent no-op) and per
/// failed PR (`PR #N — error`). Skips (a worktree that became ineligible between
/// phases) are folded into the summary count. Strings are `sanitize`d (#1137).
fn render_enqueue_result(result: &Value) -> String {
    let queued = result
        .get("queued")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let failed = result
        .get("failed")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let skipped = result
        .get("skipped")
        .and_then(Value::as_array)
        .map_or(0, Vec::len);
    let mut out = format!(
        "Queued: {} / Failed: {} / Skipped: {}",
        queued.len(),
        failed.len(),
        skipped
    );
    for pr in queued {
        let number = pr.get("number").and_then(Value::as_u64).unwrap_or(0);
        let already = pr
            .get("already_queued")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        let suffix = if already { " (already queued)" } else { "" };
        out.push_str(&format!("\n  queued: PR #{number}{suffix}"));
    }
    for pr in failed {
        let number = pr.get("number").and_then(Value::as_u64).unwrap_or(0);
        let error = sanitize(pr.get("error").and_then(Value::as_str).unwrap_or(""));
        out.push_str(&format!("\n  failed: PR #{number}{error}"));
    }
    out
}

/// Prompts on stderr for confirmation before a destructive delete and returns
/// whether the user assented, reading the answer from real stdin.
///
/// A thin wrapper over [`confirm_removal_with`] that supplies the live stdin
/// reader; the prompt-and-decide logic is factored out so it stays testable
/// without driving real stdin.
async fn confirm_removal(has_risks: bool) -> bool {
    confirm_removal_with(has_risks, read_stdin_line()).await
}

/// Prints the confirmation prompt and resolves the (already-injected) read of the
/// user's answer into a yes/no decision. Any read error, a closed stdin (EOF), or
/// a join failure surfaces as `None` and is treated as "no", so a delete never
/// proceeds unattended.
async fn confirm_removal_with(
    has_risks: bool,
    read: impl std::future::Future<Output = Option<String>>,
) -> bool {
    use std::io::Write;
    eprint!("{}", confirm_prompt(has_risks));
    let _ = std::io::stderr().flush();
    read.await.as_deref().is_some_and(answer_is_yes)
}

/// Prompts on stderr before enqueuing and returns whether the user assented,
/// reading the answer from real stdin. A thin wrapper over [`confirm_enqueue_with`]
/// supplying the live stdin reader.
async fn confirm_enqueue(count: usize) -> bool {
    confirm_enqueue_with(count, read_stdin_line()).await
}

/// Prints the enqueue confirmation prompt and resolves the (already-injected) read
/// of the user's answer into a yes/no decision. A read error, closed stdin (EOF),
/// or join failure is treated as "no", so an enqueue never proceeds unattended.
async fn confirm_enqueue_with(
    count: usize,
    read: impl std::future::Future<Output = Option<String>>,
) -> bool {
    use std::io::Write;
    eprint!("Add {count} PR(s) to the merge queue? [y/N] ");
    let _ = std::io::stderr().flush();
    read.await.as_deref().is_some_and(answer_is_yes)
}

/// Reads one line from stdin on a dedicated thread (`spawn_blocking`) so it never
/// stalls an async worker while it waits for input. Returns `None` on any read
/// error, EOF, or join failure.
async fn read_stdin_line() -> Option<String> {
    tokio::task::spawn_blocking(|| read_line_from(&mut std::io::stdin().lock()))
        .await
        .ok()
        .flatten()
}

/// Reads one line from `reader`, mapping EOF and read errors to the same
/// `Option<String>` the stdin caller consumes. Split out of [`read_stdin_line`]
/// so the read logic is testable with an in-memory reader — real stdin can't be
/// driven from a test without blocking on a TTY.
fn read_line_from(reader: &mut impl std::io::BufRead) -> Option<String> {
    let mut answer = String::new();
    reader.read_line(&mut answer).ok().map(|_| answer)
}

/// The confirmation prompt shown before a delete — it names the risks when the
/// safety report flagged any. Pure, so the wording is unit-testable.
fn confirm_prompt(has_risks: bool) -> &'static str {
    if has_risks {
        "Delete this worktree despite the risks above? [y/N] "
    } else {
        "Delete this worktree? [y/N] "
    }
}

/// Whether a confirmation answer is an affirmative (`y`/`yes`, case-insensitive).
/// Split out so the yes/no decision is unit-testable without real stdin.
fn answer_is_yes(answer: &str) -> bool {
    matches!(answer.trim().to_lowercase().as_str(), "y" | "yes")
}

/// Fetches ahead/behind on demand for every worktree in a `tree` reply and folds
/// the counts back into each worktree object, so `worktrees tree` renders the same
/// `+ahead -behind` sync state the cheap snapshot no longer carries (#1306). A
/// best-effort enrichment: if there are no worktrees, the daemon lacks the
/// `ahead-behind` op (older daemon), or the call fails, `result` is left as-is and
/// the tree still renders — just with `-` for sync.
async fn enrich_ahead_behind(socket: &Path, result: &mut Value) {
    let paths = worktree_paths(result);
    if paths.is_empty() {
        return;
    }
    let Ok(reply) = call(socket, "ahead-behind", json!({ "paths": paths })).await else {
        return;
    };
    if let Some(results) = reply.get("results").and_then(Value::as_object) {
        merge_ahead_behind(result, results);
    }
}

/// Every worktree path in a `tree` reply, in render order — the batch the
/// on-demand `ahead-behind` op is asked about.
fn worktree_paths(result: &Value) -> Vec<String> {
    let mut paths = Vec::new();
    for repo in result
        .get("repos")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default()
    {
        for worktree in repo
            .get("worktrees")
            .and_then(Value::as_array)
            .map(Vec::as_slice)
            .unwrap_or_default()
        {
            if let Some(path) = worktree.get("path").and_then(Value::as_str) {
                paths.push(path.to_string());
            }
        }
    }
    paths
}

/// Folds `{ ahead, behind }` counts (keyed by worktree path) from an `ahead-behind`
/// reply back into a `tree` reply's worktree objects. A worktree whose path is
/// absent from `results` (no upstream) is left untouched. Pure, so the merge is
/// unit-testable without a socket.
fn merge_ahead_behind(result: &mut Value, results: &serde_json::Map<String, Value>) {
    for repo in result
        .get_mut("repos")
        .and_then(Value::as_array_mut)
        .into_iter()
        .flatten()
    {
        for worktree in repo
            .get_mut("worktrees")
            .and_then(Value::as_array_mut)
            .into_iter()
            .flatten()
        {
            // Take the worktree object up front so the insert reuses this handle
            // rather than a second, always-succeeding `as_object_mut` (a non-object
            // element in the array is skipped here).
            let Some(obj) = worktree.as_object_mut() else {
                continue;
            };
            let Some(path) = obj.get("path").and_then(Value::as_str).map(str::to_string) else {
                continue;
            };
            let Some(counts) = results.get(&path) else {
                continue;
            };
            // Fold both counts in together, or neither — a malformed entry missing
            // a side is left as no-sync rather than half-applied.
            if let (Some(ahead), Some(behind)) =
                (counts.get("ahead").cloned(), counts.get("behind").cloned())
            {
                obj.insert("ahead".to_string(), ahead);
                obj.insert("behind".to_string(), behind);
            }
        }
    }
}

/// Sends one `worktrees` service op over the control socket, returning its
/// payload or turning an `ok: false` reply into an error.
async fn call(socket: &Path, op: &str, payload: Value) -> Result<Value> {
    let reply = DaemonClient::new(socket)
        .request(DaemonEnvelope::service(SERVICE, op, payload))
        .await?;
    reply_payload(reply)
}

/// Unwraps a daemon reply into its payload, turning an `ok: false` reply into an
/// error. Pure (no socket), so both mappings are unit-testable.
fn reply_payload(reply: DaemonReply) -> Result<Value> {
    if reply.ok {
        Ok(reply.payload)
    } else {
        bail!(
            "daemon returned an error: {}",
            reply.error.as_deref().unwrap_or("unknown error")
        )
    }
}

/// Renders a `list` reply as a human-readable table: a header and one row per
/// open window (repo, the daemon-computed branch and its ahead/behind sync
/// state, the primary folder, and how long ago it was last seen). Returns a
/// placeholder line when nothing is open.
fn render_windows(result: &Value) -> String {
    let windows = result
        .get("windows")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    if windows.is_empty() {
        return "No open windows.".to_string();
    }
    let mut out = format!(
        "{:<22} {:<24} {:<9} {:<40} {:>5}",
        "REPO", "BRANCH", "SYNC", "FOLDER", "AGE"
    );
    for window in windows {
        let repo = sanitize(repo_name(window));
        let branch = sanitize(window.get("branch").and_then(Value::as_str).unwrap_or("-"));
        let sync = sync_summary(window);
        let folder_disp = folder_summary(window);
        let age = age_secs(window.get("last_seen").and_then(Value::as_str));
        out.push_str(&format!(
            "\n{repo:<22} {branch:<24} {sync:<9} {folder_disp:<40} {age:>4}s"
        ));
    }
    out
}

/// Renders a `tree` reply as a repo-grouped view: a header line per repository
/// (its name, GitHub `owner/name` when present, and root path), then one indented
/// row per worktree — a `*` marks the main working tree, followed by the branch,
/// its `+ahead -behind` sync state, an `open` flag when a live window has it open,
/// and the worktree path. Returns a placeholder when no repository is open.
fn render_tree(result: &Value) -> String {
    let repos = result
        .get("repos")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    if repos.is_empty() {
        return "No repositories open.".to_string();
    }
    let mut out = String::new();
    for (i, repo) in repos.iter().enumerate() {
        // A blank line separates repositories (but not before the first): the
        // previous worktree row has no trailing newline, so two are needed.
        if i > 0 {
            out.push_str("\n\n");
        }
        out.push_str(&repo_header(repo));
        for worktree in repo
            .get("worktrees")
            .and_then(Value::as_array)
            .map(Vec::as_slice)
            .unwrap_or_default()
        {
            out.push('\n');
            out.push_str(&worktree_row(worktree));
        }
    }
    out
}

/// The header line for one repo in the tree view: `<name>  (github: owner/name)
/// <root>`, with the GitHub clause omitted for a non-GitHub repo.
fn repo_header(repo: &Value) -> String {
    let name = sanitize(repo.get("main_repo").and_then(Value::as_str).unwrap_or("-"));
    let root = sanitize(repo.get("root").and_then(Value::as_str).unwrap_or(""));
    match github_summary(repo) {
        Some(github) => format!("{name}  ({github})  {root}"),
        None => format!("{name}  {root}"),
    }
}

/// A `github: owner/name` summary for a repo, or `None` when it has no GitHub
/// identity (a non-GitHub or remote-less repo).
fn github_summary(repo: &Value) -> Option<String> {
    let owner = repo.pointer("/github/owner").and_then(Value::as_str)?;
    let name = repo.pointer("/github/name").and_then(Value::as_str)?;
    Some(format!("github: {}/{}", sanitize(owner), sanitize(name)))
}

/// One indented worktree row: a `*` for the main working tree, the branch, the
/// `+ahead -behind` sync state, an `open` flag when a window has it open, and the
/// worktree path.
fn worktree_row(worktree: &Value) -> String {
    let marker = if worktree.get("is_main").and_then(Value::as_bool) == Some(true) {
        '*'
    } else {
        ' '
    };
    let branch = sanitize(
        worktree
            .get("branch")
            .and_then(Value::as_str)
            .unwrap_or("-"),
    );
    let sync = sync_summary(worktree);
    let open = if worktree.get("open").and_then(Value::as_bool) == Some(true) {
        "open"
    } else {
        ""
    };
    let path = sanitize(worktree.get("path").and_then(Value::as_str).unwrap_or(""));
    format!("  {marker} {branch:<24} {sync:<9} {open:<5} {path}")
}

/// The repo name to show for a window: the daemon-computed `main_repo` (which
/// names the *parent* repository of a linked worktree, not its worktree-folder
/// basename) when present, else the companion-reported `repo`, else `-`.
fn repo_name(window: &Value) -> &str {
    window
        .get("main_repo")
        .and_then(Value::as_str)
        .or_else(|| window.get("repo").and_then(Value::as_str))
        .unwrap_or("-")
}

/// A compact `+ahead -behind` divergence indicator for a window, or `-` when
/// the branch tracks no upstream (or there is no branch at all). The counts are
/// daemon-computed integers, so no sanitizing is needed.
fn sync_summary(window: &Value) -> String {
    let ahead = window.get("ahead").and_then(Value::as_u64);
    let behind = window.get("behind").and_then(Value::as_u64);
    match (ahead, behind) {
        (Some(ahead), Some(behind)) => format!("+{ahead} -{behind}"),
        _ => "-".to_string(),
    }
}

/// The primary folder of a window, with a `(+N)` suffix when it has more than
/// one workspace folder.
fn folder_summary(window: &Value) -> String {
    let folders = window
        .get("folders")
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let first = sanitize(folders.first().and_then(Value::as_str).unwrap_or(""));
    let extra = folders.len().saturating_sub(1);
    if extra > 0 {
        format!("{first} (+{extra})")
    } else {
        first
    }
}

/// Strips control characters (C0, DEL, C1) from an untrusted registry string so
/// a malicious `register` payload cannot inject terminal escape sequences into
/// the rendered table (#1137). The `--json` path stays verbatim.
fn sanitize(s: &str) -> String {
    s.chars().filter(|c| !c.is_control()).collect()
}

/// Seconds elapsed since an RFC 3339 timestamp (0 if absent/unparseable).
fn age_secs(ts: Option<&str>) -> i64 {
    ts.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
        .map_or(0, |t| {
            (Utc::now() - t.with_timezone(&Utc)).num_seconds().max(0)
        })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use serde_json::json;

    /// Mirrors the `omni-dev worktrees` argv surface for parse tests.
    #[derive(Parser)]
    struct Wrapper {
        #[command(subcommand)]
        cmd: WorktreesSubcommands,
    }

    fn parse(args: &[&str]) -> WorktreesSubcommands {
        let mut full = vec!["omni-dev"];
        full.extend_from_slice(args);
        Wrapper::try_parse_from(full).unwrap().cmd
    }

    #[test]
    fn list_parses_flags_and_defaults() {
        // Routing: `worktrees list` maps to the List variant.
        assert!(matches!(parse(&["list"]), WorktreesSubcommands::List(_)));
        // Flags, via the leaf parser (clap treats argv[0] as the command name).
        let cmd = ListCommand::try_parse_from(["list"]).unwrap();
        assert_eq!(cmd.output, TableOrJson::Table);
        assert!(!cmd.json);
        assert!(cmd.socket.is_none());

        let cmd =
            ListCommand::try_parse_from(["list", "-o", "json", "--socket", "/tmp/d.sock"]).unwrap();
        assert_eq!(cmd.output, TableOrJson::Json);
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));
    }

    #[test]
    fn list_deprecated_json_flag_still_parses() {
        // `--json` is captured separately; `execute` folds it into `output`.
        let cmd = ListCommand::try_parse_from(["list", "--json"]).unwrap();
        assert!(cmd.json);
        assert_eq!(cmd.output, TableOrJson::Table);
    }

    #[test]
    fn tree_parses_flags_and_defaults() {
        // Routing: `worktrees tree` maps to the Tree variant.
        assert!(matches!(parse(&["tree"]), WorktreesSubcommands::Tree(_)));
        let cmd = TreeCommand::try_parse_from(["tree"]).unwrap();
        assert_eq!(cmd.output, TableOrJson::Table);
        assert!(cmd.socket.is_none());

        let cmd =
            TreeCommand::try_parse_from(["tree", "-o", "json", "--socket", "/tmp/d.sock"]).unwrap();
        assert_eq!(cmd.output, TableOrJson::Json);
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));
    }

    #[test]
    fn focus_parses_path_and_socket() {
        // Routing: `worktrees focus` maps to the Focus variant.
        assert!(matches!(
            parse(&["focus", "/home/me/wt"]),
            WorktreesSubcommands::Focus(_)
        ));
        // The path is a required positional; `--socket` is optional.
        let cmd = FocusCommand::try_parse_from(["focus", "/home/me/wt"]).unwrap();
        assert_eq!(cmd.path, Path::new("/home/me/wt"));
        assert!(cmd.socket.is_none());

        let cmd = FocusCommand::try_parse_from(["focus", "/home/me/wt", "--socket", "/tmp/d.sock"])
            .unwrap();
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));

        // The path is required.
        assert!(FocusCommand::try_parse_from(["focus"]).is_err());
    }

    #[tokio::test]
    async fn focus_errors_on_a_nonexistent_path_before_any_socket_call() {
        // Canonicalisation fails for a path that does not exist, so `focus`
        // reports a clear error without needing a daemon.
        let cmd = FocusCommand {
            path: PathBuf::from("/nonexistent/omni-dev-focus-xyz"),
            socket: Some(PathBuf::from("/nonexistent/omni-dev-focus.sock")),
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(
            err.to_string().contains("cannot resolve worktree path"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn focus_sends_the_open_op_for_an_existing_folder() {
        // A real (temp) folder canonicalises, so `focus` sends the `open` op to
        // the daemon; the fake daemon acknowledges it. Routed through the top-level
        // `WorktreesCommand::execute` so its `Focus` dispatch arm is exercised too.
        let (_dir, sock, server) =
            fake_daemon_reply(json!({ "ok": true, "payload": { "ok": true } }));
        let target = tempfile::tempdir().unwrap();
        let cmd = WorktreesCommand {
            command: WorktreesSubcommands::Focus(FocusCommand {
                path: target.path().to_path_buf(),
                socket: Some(sock),
            }),
        };
        cmd.execute(None).await.unwrap();
        server.await.unwrap();
    }

    #[test]
    fn render_windows_handles_empty_replies() {
        assert_eq!(
            render_windows(&json!({ "windows": [] })),
            "No open windows."
        );
        assert_eq!(render_windows(&json!({})), "No open windows.");
    }

    #[test]
    fn render_windows_renders_rows() {
        let result = json!({ "windows": [{
            "key": "w1",
            "repo": "omni-dev",
            "branch": "issue-1011",
            "ahead": 2,
            "behind": 1,
            "folders": ["/home/me/omni-dev", "/home/me/docs"],
            "last_seen": "2000-01-01T00:00:00Z",
        }]});
        let table = render_windows(&result);
        assert!(table.contains("omni-dev"), "{table}");
        // The computed branch and its sync state both render.
        assert!(table.contains("issue-1011"), "{table}");
        assert!(table.contains("+2 -1"), "{table}");
        // Primary folder plus a (+1) for the second workspace folder.
        assert!(table.contains("/home/me/omni-dev (+1)"), "{table}");
        // A header line plus exactly one data row.
        assert_eq!(table.lines().count(), 2, "{table}");
    }

    #[test]
    fn render_windows_prefers_main_repo_over_companion_repo() {
        // A linked worktree: the companion reports the worktree-folder basename,
        // but the daemon-computed `main_repo` names the parent repo, and that is
        // what the REPO column shows.
        let result = json!({ "windows": [{
            "key": "w1",
            "repo": "issue-1250",
            "main_repo": "omni-dev",
            "branch": "issue-1250",
            "folders": ["/home/me/worktrees/issue-1250"],
            "last_seen": "2000-01-01T00:00:00Z",
        }]});
        let table = render_windows(&result);
        assert!(table.contains("omni-dev"), "{table}");
        // The misleading worktree-folder basename does not appear in REPO (it is
        // still visible in the FOLDER column path).
        let data_row = table.lines().nth(1).unwrap();
        assert!(data_row.starts_with("omni-dev"), "{data_row}");
    }

    #[test]
    fn repo_name_falls_back_to_companion_repo_then_dash() {
        assert_eq!(
            repo_name(&json!({ "main_repo": "omni-dev", "repo": "wt" })),
            "omni-dev"
        );
        assert_eq!(repo_name(&json!({ "repo": "wt" })), "wt");
        assert_eq!(repo_name(&json!({})), "-");
    }

    #[test]
    fn render_windows_strips_control_bytes() {
        // C0 (ESC, CR, BEL), DEL, and C1 (CSI) bytes in every string-valued
        // field must not reach the terminal (#1137).
        let result = json!({ "windows": [{
            "key": "w1",
            "repo": "evil\x1b[31mrepo",
            "branch": "br\ranch\x07\u{9b}2J",
            "folders": ["/tmp/a\x1b]0;owned\x07\u{7f}", "/tmp/b"],
            "last_seen": "2000-01-01T00:00:00Z",
        }]});
        let table = render_windows(&result);
        assert!(
            !table.contains(|c: char| c.is_control() && c != '\n'),
            "{table:?}"
        );
        // Visible text survives with only the control bytes removed.
        assert!(table.contains("evil[31mrepo"), "{table:?}");
        assert!(table.contains("branch2J"), "{table:?}");
        assert!(table.contains("/tmp/a]0;owned (+1)"), "{table:?}");
        // Embedded CR/LF cannot forge extra rows: header plus one data row.
        assert_eq!(table.lines().count(), 2, "{table:?}");
    }

    #[test]
    fn sync_summary_formats_or_dashes() {
        assert_eq!(sync_summary(&json!({ "ahead": 2, "behind": 1 })), "+2 -1");
        assert_eq!(sync_summary(&json!({ "ahead": 0, "behind": 0 })), "+0 -0");
        // Branch present but no upstream, or nothing at all → a dash.
        assert_eq!(sync_summary(&json!({ "branch": "main" })), "-");
        assert_eq!(sync_summary(&json!({})), "-");
    }

    #[test]
    fn folder_summary_strips_control_bytes() {
        assert_eq!(
            folder_summary(&json!({ "folders": ["/a\x1b[2J/b"] })),
            "/a[2J/b"
        );
    }

    #[test]
    fn folder_summary_counts_extra_folders() {
        assert_eq!(folder_summary(&json!({ "folders": [] })), "");
        assert_eq!(folder_summary(&json!({ "folders": ["/a"] })), "/a");
        assert_eq!(
            folder_summary(&json!({ "folders": ["/a", "/b", "/c"] })),
            "/a (+2)"
        );
    }

    #[test]
    fn age_secs_handles_absent_and_unparseable_and_past() {
        assert_eq!(age_secs(None), 0);
        assert_eq!(age_secs(Some("not-a-timestamp")), 0);
        assert!(age_secs(Some("2000-01-01T00:00:00Z")) > 0);
    }

    #[test]
    fn render_tree_handles_empty_replies() {
        assert_eq!(
            render_tree(&json!({ "repos": [] })),
            "No repositories open."
        );
        assert_eq!(render_tree(&json!({})), "No repositories open.");
    }

    #[test]
    fn worktree_paths_collects_every_worktree_in_render_order() {
        let result = json!({ "repos": [
            // The middle worktree has no `path` and is skipped, not collected.
            { "worktrees": [ { "path": "/a" }, { "branch": "detached" }, { "path": "/b" } ] },
            { "worktrees": [ { "path": "/c" } ] },
        ]});
        assert_eq!(worktree_paths(&result), vec!["/a", "/b", "/c"]);
        // No repos / no worktrees → an empty batch (nothing to fetch).
        assert!(worktree_paths(&json!({})).is_empty());
        assert!(worktree_paths(&json!({ "repos": [{ "worktrees": [] }] })).is_empty());
    }

    #[test]
    fn merge_ahead_behind_folds_counts_by_path_and_leaves_others() {
        // The on-demand `ahead-behind` op reports one worktree diverging and omits
        // the other (no upstream). The merge folds the counts onto the matching
        // path and leaves the untracked worktree without sync fields.
        let mut result = json!({ "repos": [{ "worktrees": [
            { "path": "/a", "branch": "main" },
            { "path": "/b", "branch": "feature" },
        ]}]});
        let results = json!({ "/a": { "ahead": 2, "behind": 1 } });
        merge_ahead_behind(&mut result, results.as_object().unwrap());

        let worktrees = result.pointer("/repos/0/worktrees").unwrap();
        let a = &worktrees[0];
        assert_eq!(a.get("ahead").and_then(Value::as_u64), Some(2));
        assert_eq!(a.get("behind").and_then(Value::as_u64), Some(1));
        // And it renders exactly as an eager snapshot would have.
        assert_eq!(sync_summary(a), "+2 -1");
        let b = &worktrees[1];
        assert!(b.get("ahead").is_none(), "{b:?}");
        assert!(b.get("behind").is_none(), "{b:?}");
        assert_eq!(sync_summary(b), "-");
    }

    #[test]
    fn merge_ahead_behind_skips_malformed_worktrees_and_counts() {
        // Every defensive guard, on malformed input that never comes from a real
        // daemon: a non-object array element, a worktree with no `path`, and a
        // results entry missing a side. None panics; none is half-applied.
        let mut result = json!({ "repos": [{ "worktrees": [
            "not-an-object",                       // non-object element → skipped
            { "branch": "detached" },              // object, but no path → skipped
            { "path": "/a", "branch": "main" },    // matched, but counts malformed
        ]}]});
        let results = json!({ "/a": { "ahead": 2 } }); // missing `behind`
        merge_ahead_behind(&mut result, results.as_object().unwrap());

        let worktrees = result.pointer("/repos/0/worktrees").unwrap();
        // Non-object element is untouched.
        assert_eq!(worktrees[0], json!("not-an-object"));
        // Pathless worktree: no sync fields inserted.
        assert!(worktrees[1].get("ahead").is_none(), "{:?}", worktrees[1]);
        // Malformed counts: neither side folded in (both-or-nothing).
        assert!(worktrees[2].get("ahead").is_none(), "{:?}", worktrees[2]);
        assert!(worktrees[2].get("behind").is_none(), "{:?}", worktrees[2]);
    }

    #[tokio::test]
    async fn enrich_ahead_behind_is_a_noop_when_there_are_no_worktrees() {
        // No worktrees → no batch to fetch → early return before any socket call,
        // so even a nonexistent socket leaves the tree untouched.
        let mut result = json!({ "repos": [] });
        let before = result.clone();
        enrich_ahead_behind(Path::new("/nonexistent/omni-dev-ab.sock"), &mut result).await;
        assert_eq!(result, before);
    }

    #[tokio::test]
    async fn enrich_ahead_behind_leaves_the_tree_when_the_daemon_is_unreachable() {
        // A real worktree but no daemon at the socket → the call fails and the tree
        // is returned as-is (rendered with `-` for sync), never erroring.
        let mut result =
            json!({ "repos": [{ "worktrees": [{ "path": "/x", "branch": "main" }] }] });
        enrich_ahead_behind(Path::new("/nonexistent/omni-dev-ab.sock"), &mut result).await;
        let wt = result.pointer("/repos/0/worktrees/0").unwrap();
        assert!(wt.get("ahead").is_none(), "{wt:?}");
        assert!(wt.get("behind").is_none(), "{wt:?}");
    }

    /// Spawns a minimal fake daemon on a short-path Unix socket that answers the
    /// one `ahead-behind` request with `reply` (the daemon's NDJSON reply shape).
    /// Returns the temp dir (kept alive for the socket's lifetime), the socket
    /// path, and the server task.
    fn fake_daemon_reply(
        reply: Value,
    ) -> (tempfile::TempDir, PathBuf, tokio::task::JoinHandle<()>) {
        use futures::{SinkExt, StreamExt};
        use tokio::net::UnixListener;
        use tokio_util::codec::{Framed, LinesCodec};

        // A short base path keeps the socket under the 104-byte `sockaddr_un` limit.
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let sock = dir.path().join("d.sock");
        let listener = UnixListener::bind(&sock).unwrap();
        let server = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();
            let mut framed = Framed::new(stream, LinesCodec::new());
            let _req = framed.next().await.unwrap().unwrap();
            framed
                .send(serde_json::to_string(&reply).unwrap())
                .await
                .unwrap();
        });
        (dir, sock, server)
    }

    /// A [`fake_daemon_reply`] that answers a **sequence** of requests — one fresh
    /// connection per reply, in order — so a two-phase client (a `merge-queue`
    /// check then execute) can be driven end-to-end over one socket.
    fn fake_daemon_replies(
        replies: Vec<Value>,
    ) -> (tempfile::TempDir, PathBuf, tokio::task::JoinHandle<()>) {
        use futures::{SinkExt, StreamExt};
        use tokio::net::UnixListener;
        use tokio_util::codec::{Framed, LinesCodec};

        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let sock = dir.path().join("d.sock");
        let listener = UnixListener::bind(&sock).unwrap();
        let server = tokio::spawn(async move {
            for reply in replies {
                let (stream, _) = listener.accept().await.unwrap();
                let mut framed = Framed::new(stream, LinesCodec::new());
                let _req = framed.next().await.unwrap().unwrap();
                framed
                    .send(serde_json::to_string(&reply).unwrap())
                    .await
                    .unwrap();
            }
        });
        (dir, sock, server)
    }

    #[tokio::test]
    async fn enrich_ahead_behind_folds_counts_from_a_live_socket() {
        let (_dir, sock, server) = fake_daemon_reply(
            json!({ "ok": true, "payload": { "results": { "/x": { "ahead": 3, "behind": 4 } } } }),
        );
        let mut result =
            json!({ "repos": [{ "worktrees": [{ "path": "/x", "branch": "main" }] }] });
        enrich_ahead_behind(&sock, &mut result).await;
        server.await.unwrap();

        let wt = result.pointer("/repos/0/worktrees/0").unwrap();
        assert_eq!(wt.get("ahead").and_then(Value::as_u64), Some(3));
        assert_eq!(wt.get("behind").and_then(Value::as_u64), Some(4));
    }

    #[tokio::test]
    async fn enrich_ahead_behind_ignores_a_reply_without_results() {
        // An `ok` reply carrying no `results` object (an older/oddly-shaped daemon)
        // leaves the tree unchanged rather than erroring.
        let (_dir, sock, server) = fake_daemon_reply(json!({ "ok": true, "payload": {} }));
        let mut result =
            json!({ "repos": [{ "worktrees": [{ "path": "/x", "branch": "main" }] }] });
        enrich_ahead_behind(&sock, &mut result).await;
        server.await.unwrap();

        let wt = result.pointer("/repos/0/worktrees/0").unwrap();
        assert!(wt.get("ahead").is_none(), "{wt:?}");
        assert!(wt.get("behind").is_none(), "{wt:?}");
    }

    #[test]
    fn render_tree_groups_repos_and_worktrees() {
        let result = json!({ "repos": [{
            "main_repo": "omni-dev",
            "github": { "owner": "rust-works", "name": "omni-dev" },
            "root": "/home/me/omni-dev",
            "worktrees": [
                { "path": "/home/me/omni-dev", "branch": "main", "ahead": 2, "behind": 0,
                  "is_main": true, "open": true, "window_key": "w1" },
                { "path": "/home/me/wt/issue-1300", "branch": "issue-1300", "ahead": 1, "behind": 3,
                  "is_main": false, "open": false },
            ],
        }]});
        let out = render_tree(&result);
        // Repo header carries the GitHub identity and root.
        let header = out.lines().next().unwrap();
        assert!(header.contains("omni-dev"), "{out}");
        assert!(header.contains("github: rust-works/omni-dev"), "{out}");
        assert!(header.contains("/home/me/omni-dev"), "{out}");
        // The main working tree is marked with `*`, its sync, and `open`.
        assert!(
            out.lines()
                .any(|l| l.contains("* main") && l.contains("+2 -0") && l.contains("open")),
            "{out}"
        );
        // The linked worktree is unmarked and not flagged open.
        let linked = out
            .lines()
            .find(|l| l.contains("issue-1300"))
            .unwrap_or_default();
        assert!(!linked.contains('*'), "{linked}");
        assert!(!linked.contains("open"), "{linked}");
        assert!(linked.contains("+1 -3"), "{linked}");
        // Header + two worktree rows.
        assert_eq!(out.lines().count(), 3, "{out}");
    }

    #[test]
    fn render_tree_separates_multiple_repos_with_blank_line() {
        let result = json!({ "repos": [
            {
                "main_repo": "alpha",
                "root": "/r/alpha",
                "worktrees": [
                    { "path": "/r/alpha", "branch": "main", "is_main": true, "open": false },
                ],
            },
            {
                "main_repo": "beta",
                "root": "/r/beta",
                "worktrees": [
                    { "path": "/r/beta", "branch": "main", "is_main": true, "open": false },
                ],
            },
        ]});
        let out = render_tree(&result);
        // Two headers, two worktree rows, and one blank separator between repos.
        assert!(
            out.contains("\n\nbeta"),
            "repos not blank-separated: {out:?}"
        );
        let alpha = out.find("alpha").unwrap();
        let beta = out.find("beta").unwrap();
        assert!(alpha < beta, "repo order not preserved: {out}");
        assert_eq!(out.lines().count(), 5, "{out:?}");
    }

    #[test]
    fn render_tree_omits_github_for_non_github_repo() {
        let result = json!({ "repos": [{
            "main_repo": "internal",
            "root": "/srv/internal",
            "worktrees": [
                { "path": "/srv/internal", "branch": "main", "is_main": true, "open": false },
            ],
        }]});
        let out = render_tree(&result);
        assert!(!out.contains("github:"), "{out}");
        assert!(out.lines().next().unwrap().contains("internal"), "{out}");
    }

    #[test]
    fn render_tree_strips_control_bytes() {
        // Control bytes in the repo name, github identity, branch, and path must
        // not reach the terminal (#1137), matching the `list` renderer.
        let result = json!({ "repos": [{
            "main_repo": "evil\x1b[31mrepo",
            "github": { "owner": "ow\x07ner", "name": "na\u{9b}2Jme" },
            "root": "/tmp/r\x1b]0;x\x07oot",
            "worktrees": [
                { "path": "/tmp/w\rt", "branch": "br\x1b[2Janch", "is_main": true, "open": true },
            ],
        }]});
        let out = render_tree(&result);
        assert!(
            !out.contains(|c: char| c.is_control() && c != '\n'),
            "{out:?}"
        );
        // Embedded CR/LF cannot forge extra lines: header plus one worktree row.
        assert_eq!(out.lines().count(), 2, "{out:?}");
    }

    #[test]
    fn github_summary_needs_both_owner_and_name() {
        assert_eq!(
            github_summary(&json!({ "github": { "owner": "o", "name": "n" } })).as_deref(),
            Some("github: o/n")
        );
        assert_eq!(github_summary(&json!({ "github": { "owner": "o" } })), None);
        assert_eq!(github_summary(&json!({})), None);
    }

    #[test]
    fn reply_payload_unwraps_ok_and_maps_errors() {
        // ok → payload.
        assert_eq!(
            reply_payload(DaemonReply::ok(json!({ "a": 1 }))).unwrap(),
            json!({ "a": 1 })
        );
        // ok: false with a message → that message.
        let err = reply_payload(DaemonReply::err("boom")).unwrap_err();
        assert!(err.to_string().contains("boom"), "{err}");
        // ok: false with no message → the "unknown error" fallback.
        let err = reply_payload(DaemonReply {
            ok: false,
            payload: Value::Null,
            error: None,
        })
        .unwrap_err();
        assert!(err.to_string().contains("unknown error"), "{err}");
    }

    // --- #1361 typed op-parity commands -------------------------------------

    #[test]
    fn new_subcommands_route_and_require_their_args() {
        assert!(matches!(
            parse(&["close", "/home/me/wt"]),
            WorktreesSubcommands::Close(_)
        ));
        assert!(matches!(
            parse(&["show-closed"]),
            WorktreesSubcommands::ShowClosed(_)
        ));
        assert!(matches!(
            parse(&["register", "--key", "w1"]),
            WorktreesSubcommands::Register(_)
        ));
        assert!(matches!(
            parse(&["heartbeat", "--key", "w1"]),
            WorktreesSubcommands::Heartbeat(_)
        ));
        assert!(matches!(
            parse(&["unregister", "--key", "w1"]),
            WorktreesSubcommands::Unregister(_)
        ));

        // Required args are enforced.
        assert!(CloseCommand::try_parse_from(["close"]).is_err());
        assert!(RegisterCommand::try_parse_from(["register"]).is_err());
        assert!(HeartbeatCommand::try_parse_from(["heartbeat"]).is_err());
        assert!(UnregisterCommand::try_parse_from(["unregister"]).is_err());
    }

    #[test]
    fn close_parses_flags() {
        let cmd = CloseCommand::try_parse_from([
            "close",
            "/home/me/wt",
            "--window-only",
            "--dry-run",
            "-y",
            "--socket",
            "/tmp/d.sock",
        ])
        .unwrap();
        assert_eq!(cmd.path, Path::new("/home/me/wt"));
        assert!(cmd.window_only && cmd.dry_run && cmd.yes);
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));

        // Defaults: no flags set.
        let cmd = CloseCommand::try_parse_from(["close", "/home/me/wt"]).unwrap();
        assert!(!cmd.window_only && !cmd.dry_run && !cmd.yes);
    }

    #[test]
    fn tree_follow_flag_parses() {
        let cmd = TreeCommand::try_parse_from(["tree", "--follow"]).unwrap();
        assert!(cmd.follow);
        let cmd = TreeCommand::try_parse_from(["tree", "-f", "-o", "json"]).unwrap();
        assert!(cmd.follow);
        assert_eq!(cmd.output, TableOrJson::Json);
        let cmd = TreeCommand::try_parse_from(["tree"]).unwrap();
        assert!(!cmd.follow);
    }

    #[test]
    fn show_closed_parses_optional_bool() {
        assert!(ShowClosedCommand::try_parse_from(["show-closed"])
            .unwrap()
            .value
            .is_none());
        assert_eq!(
            ShowClosedCommand::try_parse_from(["show-closed", "false"])
                .unwrap()
                .value,
            Some(false)
        );
        assert_eq!(
            ShowClosedCommand::try_parse_from(["show-closed", "true"])
                .unwrap()
                .value,
            Some(true)
        );
        // A non-boolean value is rejected.
        assert!(ShowClosedCommand::try_parse_from(["show-closed", "maybe"]).is_err());
    }

    #[test]
    fn register_collects_repeated_folders() {
        let cmd = RegisterCommand::try_parse_from([
            "register",
            "--key",
            "w1",
            "--folder",
            "/a",
            "--folder",
            "/b",
            "--repo-name",
            "r",
            "--pid",
            "42",
        ])
        .unwrap();
        assert_eq!(cmd.key, "w1");
        assert_eq!(cmd.folders, vec![PathBuf::from("/a"), PathBuf::from("/b")]);
        assert_eq!(cmd.repo_name.as_deref(), Some("r"));
        assert_eq!(cmd.pid, Some(42));
    }

    #[test]
    fn answer_is_yes_accepts_only_affirmatives() {
        for yes in ["y", "Y", "yes", "YES", " yes \n"] {
            assert!(answer_is_yes(yes), "{yes:?}");
        }
        for no in ["", "n", "no", "nope", "true", "\n"] {
            assert!(!answer_is_yes(no), "{no:?}");
        }
    }

    #[test]
    fn confirm_prompt_mentions_risks_only_when_present() {
        // The risky wording names the risks; the clean one does not. Both default
        // to No. (No failure-message args — the conditions are self-describing, and
        // an unevaluated arg would just read as an uncovered line.)
        assert!(confirm_prompt(true).contains("risks"));
        assert!(!confirm_prompt(false).contains("risks"));
        assert!(confirm_prompt(true).contains("[y/N]"));
        assert!(confirm_prompt(false).contains("[y/N]"));
    }

    #[test]
    fn read_line_from_maps_input_and_eof() {
        use std::io::Cursor;
        // A line (with or without a trailing newline) comes back verbatim; EOF is
        // an empty read (`Ok(0)`), which maps to `Some("")` — the decision layer
        // then treats it as "no".
        assert_eq!(
            read_line_from(&mut Cursor::new("y\n")).as_deref(),
            Some("y\n")
        );
        assert_eq!(read_line_from(&mut Cursor::new("")).as_deref(), Some(""));
        assert_eq!(
            read_line_from(&mut Cursor::new("no-newline")).as_deref(),
            Some("no-newline")
        );
    }

    #[test]
    fn render_safety_report_renders_fields_and_notes() {
        let report = json!({
            "removable": true,
            "is_main": false,
            "open": true,
            "window_key": "w1",
            "window_folder_count": 2,
            "risks": [{ "kind": "dirty", "detail": "uncommitted changes" }],
            "info": [{ "kind": "unpushed", "detail": "2 unpushed commits" }],
        });
        let out = render_safety_report(Path::new("/home/me/wt"), &report);
        assert!(out.contains("/home/me/wt"), "{out}");
        assert!(out.contains("removable:        true"), "{out}");
        assert!(
            out.contains("open in a window:  yes (key w1, 2 folder(s))"),
            "{out}"
        );
        assert!(out.contains("[dirty] uncommitted changes"), "{out}");
        assert!(out.contains("[unpushed] 2 unpushed commits"), "{out}");
    }

    #[test]
    fn render_safety_report_handles_no_window_and_no_notes() {
        let report = json!({ "removable": false, "is_main": true, "open": false });
        let out = render_safety_report(Path::new("/r"), &report);
        assert!(out.contains("removable:        false"), "{out}");
        assert!(out.contains("main working tree: true"), "{out}");
        assert!(out.contains("open in a window:  no"), "{out}");
        // No risks/info sections are emitted when both are absent.
        assert!(!out.contains("risks:"), "{out}");
        assert!(!out.contains("info:"), "{out}");
    }

    #[test]
    fn render_safety_report_strips_control_bytes() {
        // Daemon-supplied strings (window key, note kind/detail) must not inject
        // terminal escapes (#1137).
        let report = json!({
            "removable": true, "is_main": false, "open": true,
            "window_key": "w\x1b[31m1", "window_folder_count": 1,
            "risks": [{ "kind": "di\x07rty", "detail": "lost\r\nrow" }],
            "info": [],
        });
        let out = render_safety_report(Path::new("/r"), &report);
        assert!(
            !out.contains(|c: char| c.is_control() && c != '\n'),
            "{out:?}"
        );
    }

    /// Spawns a fake daemon that answers `replies.len()` sequential connections,
    /// each with the next reply, and **returns the request envelope(s) it
    /// received** (via the join handle) so a test can assert the exact wire shape
    /// — op and payload — that the client sent, not just the round-trip. Same
    /// short-path `/tmp` socket as `fake_daemon_reply`.
    fn fake_daemon_seq(
        replies: Vec<Value>,
    ) -> (
        tempfile::TempDir,
        PathBuf,
        tokio::task::JoinHandle<Vec<Value>>,
    ) {
        use futures::{SinkExt, StreamExt};
        use tokio::net::UnixListener;
        use tokio_util::codec::{Framed, LinesCodec};

        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let sock = dir.path().join("d.sock");
        let listener = UnixListener::bind(&sock).unwrap();
        let server = tokio::spawn(async move {
            let mut requests = Vec::new();
            for reply in replies {
                let (stream, _) = listener.accept().await.unwrap();
                let mut framed = Framed::new(stream, LinesCodec::new());
                let req = framed.next().await.unwrap().unwrap();
                requests.push(serde_json::from_str::<Value>(&req).unwrap());
                framed
                    .send(serde_json::to_string(&reply).unwrap())
                    .await
                    .unwrap();
            }
            requests
        });
        (dir, sock, server)
    }

    #[tokio::test]
    async fn close_window_only_sends_remove_false() {
        let (_dir, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "closed": true } })]);
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: true,
            dry_run: false,
            yes: false,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        // Exactly one op, and it is a non-destructive close: remove:false, never
        // confirmed. A payload-field rename would fail here.
        assert_eq!(reqs.len(), 1);
        assert_eq!(reqs[0]["op"], "close");
        assert_eq!(reqs[0]["payload"]["remove"], json!(false));
        assert!(
            reqs[0]["payload"].get("confirmed").is_none(),
            "{:?}",
            reqs[0]
        );
        // The path is canonicalized client-side before it is sent.
        let want = std::fs::canonicalize(target.path()).unwrap();
        assert_eq!(reqs[0]["payload"]["path"], json!(want.to_string_lossy()));
    }

    #[tokio::test]
    async fn close_window_only_dry_run_never_contacts_the_daemon() {
        // `--window-only --dry-run` must have no side effect: it prints what would
        // happen and returns without a socket call, so a nonexistent socket is fine.
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: true,
            dry_run: true,
            yes: false,
            socket: Some(PathBuf::from("/nonexistent/omni-dev-close-dry.sock")),
        }
        .execute()
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn close_dry_run_only_runs_phase_one() {
        // A single connection: the safety check. `--dry-run` never sends phase-2.
        let (_dir, sock, server) = fake_daemon_seq(vec![json!({
            "ok": true,
            "payload": { "removable": true, "is_main": false, "open": false,
                         "window_folder_count": 0, "risks": [], "info": [] }
        })]);
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: false,
            dry_run: true,
            yes: false,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        // Only the phase-1 safety check: remove:true, unconfirmed. No phase-2.
        assert_eq!(reqs.len(), 1);
        assert_eq!(reqs[0]["op"], "close");
        assert_eq!(reqs[0]["payload"]["remove"], json!(true));
        assert!(
            reqs[0]["payload"].get("confirmed").is_none(),
            "{:?}",
            reqs[0]
        );
    }

    #[tokio::test]
    async fn close_yes_executes_phase_two() {
        // Two connections: phase-1 safety report (removable), then phase-2 delete.
        let (_dir, sock, server) = fake_daemon_seq(vec![
            json!({ "ok": true, "payload": { "removable": true, "is_main": false,
                    "open": false, "window_folder_count": 0, "risks": [], "info": [] } }),
            json!({ "ok": true, "payload": { "removed": true } }),
        ]);
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: false,
            dry_run: false,
            yes: true,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        // Phase 1 is the unconfirmed safety check; phase 2 carries confirmed:true.
        assert_eq!(reqs.len(), 2);
        assert_eq!(reqs[0]["op"], "close");
        assert_eq!(reqs[0]["payload"]["remove"], json!(true));
        assert!(
            reqs[0]["payload"].get("confirmed").is_none(),
            "{:?}",
            reqs[0]
        );
        assert_eq!(reqs[1]["op"], "close");
        assert_eq!(reqs[1]["payload"]["remove"], json!(true));
        assert_eq!(reqs[1]["payload"]["confirmed"], json!(true));
        // A CLI is never a VS Code window, so it never claims a requester_key.
        assert!(
            reqs[1]["payload"].get("requester_key").is_none(),
            "{:?}",
            reqs[1]
        );
    }

    #[tokio::test]
    async fn close_refuses_a_non_removable_target() {
        // Phase-1 reports not-removable (e.g. the main tree); the command prints
        // the report then errors without a phase-2 execute (one connection only).
        let (_dir, sock, server) = fake_daemon_seq(vec![json!({
            "ok": true,
            "payload": { "removable": false, "is_main": true, "open": false,
                         "window_folder_count": 0, "risks": [], "info": [] }
        })]);
        let target = tempfile::tempdir().unwrap();
        let err = CloseCommand {
            path: target.path().to_path_buf(),
            window_only: false,
            dry_run: false,
            yes: true,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap_err();
        assert!(
            err.to_string().contains("not a removable worktree"),
            "{err}"
        );
        // Only the phase-1 check ran — no destructive phase-2 was sent.
        assert_eq!(server.await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn close_errors_on_a_nonexistent_path_before_any_socket_call() {
        let err = CloseCommand {
            path: PathBuf::from("/nonexistent/omni-dev-close-xyz"),
            window_only: false,
            dry_run: false,
            yes: true,
            socket: Some(PathBuf::from("/nonexistent/omni-dev-close.sock")),
        }
        .execute()
        .await
        .unwrap_err();
        assert!(
            err.to_string().contains("cannot resolve worktree path"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn show_closed_sets_and_reads() {
        // Set: one connection acknowledging set-show-closed.
        let (_dir, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "ok": true } })]);
        ShowClosedCommand {
            value: Some(false),
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        assert_eq!(reqs[0]["op"], "set-show-closed");
        assert_eq!(reqs[0]["payload"]["show_closed"], json!(false));

        // Read: one connection returning a `tree` snapshot's `show_closed`.
        let (_dir, sock, server) = fake_daemon_seq(vec![
            json!({ "ok": true, "payload": { "repos": [], "show_closed": false } }),
        ]);
        ShowClosedCommand {
            value: None,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        // The no-arg read is served by a plain `tree` fetch, not a dedicated op.
        assert_eq!(server.await.unwrap()[0]["op"], "tree");
    }

    #[tokio::test]
    async fn register_heartbeat_unregister_send_their_ops() {
        let (_dir, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "ok": true } })]);
        RegisterCommand {
            key: "w1".to_string(),
            folders: vec![PathBuf::from("/a")],
            repo_name: Some("r".to_string()),
            title: None,
            pid: Some(7),
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        // The RegisterRequest wire shape: op + every field the daemon reads.
        assert_eq!(reqs[0]["op"], "register");
        assert_eq!(reqs[0]["payload"]["key"], json!("w1"));
        assert_eq!(reqs[0]["payload"]["folders"], json!(["/a"]));
        assert_eq!(reqs[0]["payload"]["repo"], json!("r"));
        assert_eq!(reqs[0]["payload"]["pid"], json!(7));

        let (_dir, sock, server) = fake_daemon_seq(vec![
            json!({ "ok": true, "payload": { "known": true, "close": true } }),
        ]);
        HeartbeatCommand {
            key: "w1".to_string(),
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        assert_eq!(reqs[0]["op"], "heartbeat");
        assert_eq!(reqs[0]["payload"]["key"], json!("w1"));

        let (_dir, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "removed": true } })]);
        UnregisterCommand {
            key: "w1".to_string(),
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        assert_eq!(reqs[0]["op"], "unregister");
        assert_eq!(reqs[0]["payload"]["key"], json!("w1"));
    }

    #[tokio::test]
    async fn tree_follow_renders_each_pushed_frame() {
        use crate::daemon::testutil::fake_daemon_stream;

        // JSON follow: two non-empty frames printed as an NDJSON stream, then EOF.
        let (_dir, sock, server) = fake_daemon_stream(vec![
            json!({ "ok": true, "payload": { "repos": [], "show_closed": true } }),
            json!({ "ok": true, "payload": { "repos": [], "show_closed": false } }),
        ]);
        follow_tree_stream(&sock, TableOrJson::Json).await.unwrap();
        server.await.unwrap();

        // Table follow: empty-repos frames render "No repositories open." and never
        // trigger an ahead/behind socket call (the enrich guard early-returns).
        let (_dir, sock, server) = fake_daemon_stream(vec![
            json!({ "ok": true, "payload": { "repos": [], "show_closed": true } }),
        ]);
        follow_tree_stream(&sock, TableOrJson::Table).await.unwrap();
        server.await.unwrap();

        // Through `TreeCommand::execute` with `--follow`, covering the follow-dispatch
        // branch (not just the free `follow_tree_stream`).
        let (_dir, sock, server) = fake_daemon_stream(vec![
            json!({ "ok": true, "payload": { "repos": [], "show_closed": true } }),
        ]);
        TreeCommand {
            socket: Some(sock),
            output: TableOrJson::Json,
            follow: true,
        }
        .execute()
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn worktrees_command_routes_each_new_subcommand() {
        // Route every new variant through the outer `WorktreesCommand::execute` so
        // its dispatch arms are exercised (the wire-shape tests drive the leaf
        // `execute` directly).
        let target = tempfile::tempdir().unwrap();
        // Close: `--window-only --dry-run` contacts no daemon.
        WorktreesCommand {
            command: WorktreesSubcommands::Close(CloseCommand {
                path: target.path().to_path_buf(),
                window_only: true,
                dry_run: true,
                yes: false,
                socket: Some(PathBuf::from("/nonexistent/omni-dev-route.sock")),
            }),
        }
        .execute(None)
        .await
        .unwrap();

        // Rebase: a non-worktree path with `--dry-run` reaches no daemon and no
        // remote (it classifies as `not a git worktree` and stops).
        WorktreesCommand {
            command: WorktreesSubcommands::Rebase(RebaseCommand {
                paths: vec![target.path().to_path_buf()],
                dry_run: true,
                ..rebase_cmd()
            }),
        }
        .execute(None)
        .await
        .unwrap();

        // ShowClosed (set).
        let (_d, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "ok": true } })]);
        WorktreesCommand {
            command: WorktreesSubcommands::ShowClosed(ShowClosedCommand {
                value: Some(true),
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();

        // Reposition: `--undo` needs no path resolution, so one reply suffices.
        let (_d, sock, server) = fake_daemon_seq(vec![json!({
            "ok": true,
            "payload": { "trusted": true, "moved": 0, "skipped": 0, "results": [] },
        })]);
        WorktreesCommand {
            command: WorktreesSubcommands::Reposition(RepositionCommand {
                paths: Vec::new(),
                reference: None,
                dry_run: false,
                undo: true,
                output: TableOrJson::Table,
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();

        // Reload: an empty path list resolves nothing, so `list` is the only
        // request before the op.
        let (_d, sock, server) = fake_daemon_seq(vec![
            json!({ "ok": true, "payload": { "windows": [] } }),
            json!({ "ok": true, "payload": { "requested": 0, "signalled": 0, "unknown": [] } }),
        ]);
        WorktreesCommand {
            command: WorktreesSubcommands::Reload(ReloadCommand {
                paths: Vec::new(),
                output: TableOrJson::Table,
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();

        // Register.
        let (_d, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "ok": true } })]);
        WorktreesCommand {
            command: WorktreesSubcommands::Register(RegisterCommand {
                key: "w1".to_string(),
                folders: vec![],
                repo_name: None,
                title: None,
                pid: None,
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();

        // Heartbeat.
        let (_d, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "known": true } })]);
        WorktreesCommand {
            command: WorktreesSubcommands::Heartbeat(HeartbeatCommand {
                key: "w1".to_string(),
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();

        // Unregister.
        let (_d, sock, server) =
            fake_daemon_seq(vec![json!({ "ok": true, "payload": { "removed": true } })]);
        WorktreesCommand {
            command: WorktreesSubcommands::Unregister(UnregisterCommand {
                key: "w1".to_string(),
                socket: Some(sock),
            }),
        }
        .execute(None)
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn close_aborts_when_confirmation_is_declined() {
        // Phase-1 says removable; the injected confirmer declines → the "Aborted"
        // branch runs and no phase-2 delete is sent (one connection only). This
        // covers the interactive-decline path without driving real stdin.
        let (_dir, sock, server) = fake_daemon_seq(vec![json!({
            "ok": true,
            "payload": { "removable": true, "is_main": false, "open": false,
                         "window_folder_count": 0, "risks": [], "info": [] }
        })]);
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: false,
            dry_run: false,
            yes: false,
            socket: Some(sock),
        }
        .execute_with(|_has_risks| async { false })
        .await
        .unwrap();
        assert_eq!(server.await.unwrap().len(), 1);
    }

    #[tokio::test]
    async fn close_deletes_when_confirmation_is_accepted() {
        // Phase-1 removable, the injected confirmer accepts → phase-2 executes with
        // confirmed:true.
        let (_dir, sock, server) = fake_daemon_seq(vec![
            json!({ "ok": true, "payload": { "removable": true, "is_main": false,
                    "open": false, "window_folder_count": 0, "risks": [], "info": [] } }),
            json!({ "ok": true, "payload": { "removed": true } }),
        ]);
        let target = tempfile::tempdir().unwrap();
        CloseCommand {
            path: target.path().to_path_buf(),
            window_only: false,
            dry_run: false,
            yes: false,
            socket: Some(sock),
        }
        .execute_with(|_has_risks| async { true })
        .await
        .unwrap();
        let reqs = server.await.unwrap();
        assert_eq!(reqs.len(), 2);
        assert_eq!(reqs[1]["payload"]["confirmed"], json!(true));
    }

    #[tokio::test]
    async fn confirm_removal_with_decides_from_the_answer() {
        // A "yes"/"y" answer confirms; "no", an empty line, and a `None` (EOF/read
        // error) all decline — for both the risky and clean prompt wordings.
        assert!(confirm_removal_with(false, async { Some("y\n".to_string()) }).await);
        assert!(confirm_removal_with(true, async { Some("YES".to_string()) }).await);
        assert!(!confirm_removal_with(false, async { Some("n".to_string()) }).await);
        assert!(!confirm_removal_with(true, async { Some(String::new()) }).await);
        assert!(!confirm_removal_with(false, async { None }).await);
    }

    // ── worktrees rebase (#1400) ──────────────────────────────────────────

    /// A `RebaseCommand` with every field defaulted, for terse test construction.
    fn rebase_cmd() -> RebaseCommand {
        RebaseCommand {
            paths: Vec::new(),
            all: false,
            onto: None,
            autostash: false,
            dry_run: false,
            keep_conflicts: false,
            yes: false,
            output: TableOrJson::Table,
        }
    }

    #[test]
    fn rebase_parses_paths_and_flags() {
        let cmd = RebaseCommand::try_parse_from([
            "rebase",
            "/wt/a",
            "/wt/b",
            "--onto",
            "origin/release",
            "--autostash",
            "--dry-run",
            "--keep-conflicts",
            "-y",
            "-o",
            "json",
        ])
        .unwrap();
        assert_eq!(
            cmd.paths,
            vec![PathBuf::from("/wt/a"), PathBuf::from("/wt/b")]
        );
        assert_eq!(cmd.onto.as_deref(), Some("origin/release"));
        assert!(cmd.autostash && cmd.dry_run && cmd.keep_conflicts && cmd.yes);
        assert!(matches!(cmd.output, TableOrJson::Json));
    }

    #[test]
    fn rebase_defaults_are_conservative() {
        let cmd = RebaseCommand::try_parse_from(["rebase", "/wt/a"]).unwrap();
        assert!(!cmd.all && !cmd.autostash && !cmd.dry_run && !cmd.yes);
        // Aborting a conflict stays the default: the opt-in is `--keep-conflicts`,
        // so an unattended batch never leaves a worktree mid-rebase by surprise.
        assert!(!cmd.keep_conflicts);
        assert_eq!(cmd.onto, None);
        assert!(matches!(cmd.output, TableOrJson::Table));
    }

    #[test]
    fn rebase_requires_a_target() {
        // A bare `rebase` parses, but resolving its selection refuses rather than
        // silently rebasing everything.
        let err = rebase_cmd().selection(None).unwrap_err().to_string();
        assert!(err.contains("--all"), "expected a usage hint, got: {err}");
    }

    #[test]
    fn rebase_rejects_paths_together_with_all() {
        let cmd = RebaseCommand {
            paths: vec![PathBuf::from("/wt/a")],
            all: true,
            ..rebase_cmd()
        };
        let err = cmd.selection(None).unwrap_err().to_string();
        assert!(err.contains("not both"), "got: {err}");
    }

    #[test]
    fn rebase_selection_maps_paths_and_all() {
        let cmd = RebaseCommand {
            paths: vec![PathBuf::from("/wt/a")],
            ..rebase_cmd()
        };
        assert!(matches!(cmd.selection(None).unwrap(), Selection::Paths(p) if p.len() == 1));
        let all = RebaseCommand {
            all: true,
            ..rebase_cmd()
        };
        assert!(matches!(
            all.selection(None).unwrap(),
            Selection::All { .. }
        ));
    }

    #[test]
    fn rebase_prompt_agrees_in_number() {
        assert!(rebase_prompt(1).contains("1 worktree ("));
        assert!(rebase_prompt(3).contains("3 worktrees ("));
        // Always names the consequence.
        assert!(rebase_prompt(2).contains("rewrites branch history"));
    }

    #[tokio::test]
    async fn confirm_rebase_with_decides_from_the_answer() {
        assert!(confirm_rebase_with(1, async { Some("y\n".to_string()) }).await);
        assert!(confirm_rebase_with(2, async { Some("YES".to_string()) }).await);
        assert!(!confirm_rebase_with(1, async { Some("n".to_string()) }).await);
        assert!(!confirm_rebase_with(1, async { Some(String::new()) }).await);
        assert!(!confirm_rebase_with(1, async { None }).await);
    }

    #[test]
    fn fetch_line_reports_each_repos_single_fetch() {
        let ok = FetchOutcome {
            repo_root: PathBuf::from("/repo"),
            onto: "origin/main".to_string(),
            fetched: true,
            ok: true,
            detail: None,
        };
        assert!(fetch_line(&ok).contains("Fetched origin/main once for /repo"));

        let failed = FetchOutcome {
            detail: Some("host unreachable".to_string()),
            ok: false,
            ..ok.clone()
        };
        assert!(fetch_line(&failed).contains("FAILED"));

        let local = FetchOutcome {
            fetched: false,
            onto: "develop".to_string(),
            ..ok
        };
        assert!(fetch_line(&local).contains("nothing fetched"));
    }

    #[test]
    fn outcome_rows_render_each_status() {
        let row = |result| {
            outcome_row(&WorktreeOutcome {
                path: PathBuf::from("/wt"),
                branch: Some("feature".to_string()),
                onto: "origin/main".to_string(),
                result,
            })
        };
        assert!(row(RebaseResult::Rebased { behind: 2 }).contains("rebased"));
        assert!(row(RebaseResult::Rebased { behind: 2 }).contains("was 2 behind"));
        assert!(row(RebaseResult::WouldRebase { behind: 1 }).contains("would-rebase"));
        assert!(row(RebaseResult::UpToDate).contains("up-to-date"));
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::Dirty
        })
        .contains("--autostash"));
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::MainWorkingTree
        })
        .contains("main working tree"));
        assert!(row(RebaseResult::Conflict {
            detail: "CONFLICT (content)".to_string(),
            left_in_place: false,
        })
        .contains("conflict"));
        // A left-in-place conflict tells the user the worktree still needs
        // finishing — a different instruction than an aborted one (#1415).
        let kept = row(RebaseResult::Conflict {
            detail: "CONFLICT (content)".to_string(),
            left_in_place: true,
        });
        assert!(kept.contains("conflict"), "{kept}");
        assert!(kept.contains("git rebase --continue"), "{kept}");
        assert!(row(RebaseResult::FetchFailed {
            detail: "host unreachable".to_string()
        })
        .contains("fetch-failed"));
        // The remaining skip reasons render their human text.
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::DetachedHead
        })
        .contains("detached HEAD"));
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::OperationInProgress
        })
        .contains("in progress"));
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::NotAWorktree
        })
        .contains("not a git worktree"));
        assert!(row(RebaseResult::Skipped {
            reason: SkipReason::NoOntoRef
        })
        .contains("resolve the target ref"));
    }

    #[test]
    fn print_emits_both_json_and_table_without_error() {
        let fetches = vec![FetchOutcome {
            repo_root: PathBuf::from("/r"),
            onto: "origin/main".to_string(),
            fetched: true,
            ok: true,
            detail: None,
        }];
        let outcomes = vec![WorktreeOutcome {
            path: PathBuf::from("/wt"),
            branch: Some("feature".to_string()),
            onto: "origin/main".to_string(),
            result: RebaseResult::UpToDate,
        }];
        // The JSON branch (serializes the whole report) and the table branch.
        let json_cmd = RebaseCommand {
            dry_run: true,
            output: TableOrJson::Json,
            ..rebase_cmd()
        };
        json_cmd.print(true, &fetches, &outcomes).unwrap();
        rebase_cmd().print(false, &fetches, &outcomes).unwrap();
    }

    #[test]
    fn brief_collapses_a_multiline_git_error_to_one_capped_line() {
        assert_eq!(brief("\n\nfirst line\nsecond line\n"), "first line");
        let long = "x".repeat(200);
        let out = brief(&long);
        assert_eq!(out.chars().count(), 100);
        assert!(out.ends_with("..."));
        // Control characters are stripped (the table is untrusted-string safe).
        assert_eq!(brief("a\u{7}b"), "ab");
    }

    #[test]
    fn empty_report_renders_placeholders() {
        assert_eq!(render_fetches(&[]), "No repository selected.");
        assert_eq!(render_outcomes(&[]), "No worktrees selected.");
    }

    // The serialization guard is a `std::sync::Mutex` held across the `.await`
    // below on purpose: the git fetch it serializes runs *during* that await (on a
    // `spawn_blocking` thread inside `execute_with`). It is deadlock-safe — the
    // awaited work never re-acquires this lock — so the general "no std mutex
    // across await" rule does not apply to this test-only load limiter.
    #[allow(clippy::await_holding_lock)]
    #[tokio::test]
    async fn rebase_declined_confirmation_leaves_the_branch_untouched() {
        // The safety-critical branch of a history-rewriting command: declining the
        // prompt must return before `worktree_rebase::execute` is ever reached.
        // Shares the engine tests' git-load lock so the whole suite's concurrent
        // `git` spawns never starve the daemon's timing-sensitive poller tests.
        let _guard = worktree_rebase::test_serial_lock();
        let Some(scenario) = BehindScenario::build() else {
            return; // git unavailable — the engine tests cover the git behaviour.
        };
        let before = scenario.worktree_head();
        RebaseCommand {
            paths: vec![scenario.worktree.clone()],
            ..rebase_cmd()
        }
        .execute_with(None, |pending| async move {
            assert_eq!(pending, 1, "one worktree is behind and awaiting a rebase");
            false
        })
        .await
        .unwrap();
        assert_eq!(
            scenario.worktree_head(),
            before,
            "declining the confirm must not rebase"
        );
    }

    // Holds the git-load lock across `.await` for the same deadlock-safe reason as
    // the declined-confirm test above.
    #[allow(clippy::await_holding_lock)]
    #[tokio::test]
    async fn rebase_confirmed_rebases_the_behind_worktree() {
        // The accepted branch: confirming drives plan → execute → report, and the
        // behind worktree fast-forwards onto the freshly fetched origin/main.
        let _guard = worktree_rebase::test_serial_lock();
        let Some(scenario) = BehindScenario::build() else {
            return; // git unavailable — the engine tests cover the git behaviour.
        };
        let before = scenario.worktree_head();
        RebaseCommand {
            paths: vec![scenario.worktree.clone()],
            ..rebase_cmd()
        }
        .execute_with(None, |pending| async move {
            assert_eq!(pending, 1);
            true
        })
        .await
        .unwrap();
        assert_ne!(
            scenario.worktree_head(),
            before,
            "confirming the prompt must rebase the worktree"
        );
    }

    /// A repo whose one linked worktree is a commit behind `origin/main`, built with
    /// the real `git` CLI (the command under test shells out too). Returns `None` if
    /// any setup step fails, so the suite degrades rather than flaking.
    struct BehindScenario {
        _root: tempfile::TempDir,
        worktree: PathBuf,
    }

    impl BehindScenario {
        fn build() -> Option<Self> {
            use git2::Repository;
            let root = tempfile::tempdir().ok()?;
            let origin = root.path().join("origin.git");
            let local = root.path().join("local");
            let worktree = root.path().join("feature");
            std::fs::create_dir_all(&origin).ok()?;
            std::fs::create_dir_all(&local).ok()?;
            run(&origin, &["init", "--bare", "-b", "main"])?;
            run(&local, &["init", "-b", "main"])?;
            Self::identity(&local)?;
            std::fs::write(local.join("f.txt"), "one\n").ok()?;
            run(&local, &["add", "f.txt"])?;
            run(&local, &["commit", "-m", "one"])?;
            run(&local, &["remote", "add", "origin", origin.to_str()?])?;
            run(&local, &["push", "-u", "origin", "main"])?;
            run(
                &local,
                &[
                    "worktree",
                    "add",
                    "-b",
                    "feature",
                    worktree.to_str()?,
                    "main",
                ],
            )?;
            // Advance origin/main in-process with git2 (no `git clone` subprocess),
            // so `local` only learns of it when the command under test fetches.
            let repo = Repository::open_bare(&origin).ok()?;
            let parent = repo
                .find_commit(repo.refname_to_id("refs/heads/main").ok()?)
                .ok()?;
            let mut builder = repo.treebuilder(Some(&parent.tree().ok()?)).ok()?;
            let blob = repo.blob(b"two\n").ok()?;
            builder.insert("f.txt", blob, 0o100_644).ok()?;
            let tree = repo.find_tree(builder.write().ok()?).ok()?;
            let sig = git2::Signature::now("Other", "other@example.com").ok()?;
            repo.commit(
                Some("refs/heads/main"),
                &sig,
                &sig,
                "two",
                &tree,
                &[&parent],
            )
            .ok()?;
            Some(Self {
                _root: root,
                worktree,
            })
        }

        /// Pins identity and disables commit signing, so a developer's global
        /// `commit.gpgsign = true` cannot make these repos depend on gpg.
        fn identity(dir: &Path) -> Option<()> {
            run(dir, &["config", "user.name", "Test"])?;
            run(dir, &["config", "user.email", "test@example.com"])?;
            run(dir, &["config", "commit.gpgsign", "false"])
        }

        fn worktree_head(&self) -> String {
            let out = std::process::Command::new("git")
                .current_dir(&self.worktree)
                .args(["rev-parse", "HEAD"])
                .output();
            out.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
                .unwrap_or_default()
        }
    }

    /// Runs `git` in `dir`, returning `None` on any failure.
    fn run(dir: &Path, args: &[&str]) -> Option<()> {
        let output = std::process::Command::new("git")
            .current_dir(dir)
            .args(args)
            .output()
            .ok()?;
        output.status.success().then_some(())
    }

    // --- Merge-queue command (#1401) ---------------------------------------

    #[test]
    fn merge_queue_parses_paths_and_flags() {
        // Routing: `worktrees merge-queue` maps to the MergeQueue variant.
        assert!(matches!(
            parse(&["merge-queue", "/a"]),
            WorktreesSubcommands::MergeQueue(_)
        ));
        // Multiple positional paths plus `--check`.
        let cmd =
            MergeQueueCommand::try_parse_from(["merge-queue", "/a", "/b", "--check"]).unwrap();
        assert_eq!(cmd.paths.len(), 2);
        assert!(cmd.check);
        assert!(!cmd.yes);
        assert!(cmd.socket.is_none());
        // `-y` and `--socket`.
        let cmd = MergeQueueCommand::try_parse_from([
            "merge-queue",
            "/a",
            "-y",
            "--socket",
            "/tmp/d.sock",
        ])
        .unwrap();
        assert!(cmd.yes);
        assert_eq!(cmd.socket.as_deref(), Some(Path::new("/tmp/d.sock")));
        // At least one path is required.
        assert!(MergeQueueCommand::try_parse_from(["merge-queue"]).is_err());
    }

    #[test]
    fn render_eligibility_report_lists_eligible_and_skipped() {
        let report = json!({
            "eligible": [{ "number": 10, "branch": "feature", "url": "u", "path": "/wt/a" }],
            "skipped": [{ "path": "/wt/b", "kind": "dirty", "detail": "2 modified" }],
        });
        let out = render_eligibility_report(&report);
        assert!(out.contains("Eligible: 1 / Skipped: 1"), "{out}");
        assert!(out.contains("PR #10 [feature] /wt/a"), "{out}");
        assert!(out.contains("skipped [dirty]: /wt/b — 2 modified"), "{out}");
    }

    #[test]
    fn render_enqueue_result_marks_already_queued_and_failures() {
        let result = json!({
            "queued": [
                { "number": 10, "path": "/a" },
                { "number": 11, "path": "/b", "already_queued": true },
            ],
            "failed": [{ "number": 12, "path": "/c", "error": "merge queue not enabled" }],
            "skipped": [{ "path": "/d", "kind": "unpushed", "detail": "x" }],
        });
        let out = render_enqueue_result(&result);
        assert!(out.contains("Queued: 2 / Failed: 1 / Skipped: 1"), "{out}");
        assert!(out.contains("queued: PR #10"), "{out}");
        assert!(out.contains("PR #11 (already queued)"), "{out}");
        assert!(
            out.contains("failed: PR #12 — merge queue not enabled"),
            "{out}"
        );
    }

    #[test]
    fn render_eligibility_report_strips_control_bytes() {
        // Control bytes in every daemon-supplied string must not reach the terminal
        // (#1137), matching the close/list/tree renderers.
        let report = json!({
            "eligible": [{ "number": 1, "branch": "br\x1b[31manch", "path": "/a\rb" }],
            "skipped": [{ "path": "/e\x1b]0;x\x07vil", "kind": "d\x07irty", "detail": "l\u{9b}2J" }],
        });
        let out = render_eligibility_report(&report);
        assert!(
            !out.contains(|c: char| c.is_control() && c != '\n'),
            "{out:?}"
        );
    }

    #[tokio::test]
    async fn confirm_enqueue_with_decides_from_the_answer() {
        assert!(confirm_enqueue_with(3, async { Some("y\n".to_string()) }).await);
        assert!(confirm_enqueue_with(1, async { Some("YES".to_string()) }).await);
        assert!(!confirm_enqueue_with(3, async { Some("n".to_string()) }).await);
        assert!(!confirm_enqueue_with(3, async { Some(String::new()) }).await);
        assert!(!confirm_enqueue_with(3, async { None }).await);
    }

    #[tokio::test]
    async fn merge_queue_errors_on_a_nonexistent_path_before_any_socket_call() {
        // Canonicalization fails for a path that does not exist, so the command
        // reports a clear error without needing a daemon. Also drives `execute`.
        let cmd = MergeQueueCommand {
            paths: vec![PathBuf::from("/nonexistent/omni-dev-mq-xyz")],
            check: true,
            yes: false,
            socket: Some(PathBuf::from("/nonexistent/omni-dev-mq.sock")),
        };
        let err = cmd.execute().await.unwrap_err();
        assert!(
            err.to_string().contains("cannot resolve worktree path"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn merge_queue_check_prints_the_report_and_never_confirms() {
        let target = tempfile::tempdir().unwrap();
        let (_dir, sock, server) = fake_daemon_replies(vec![json!({
            "ok": true,
            "payload": {
                "eligible": [{ "path": "/a", "number": 9, "url": "u", "branch": "feature" }],
                "skipped": [{ "path": "/b", "kind": "dirty", "detail": "2 modified" }],
            }
        })]);
        let cmd = MergeQueueCommand {
            paths: vec![target.path().to_path_buf()],
            check: true,
            yes: false,
            socket: Some(sock),
        };
        // `--check` returns after phase 1, so the confirm closure must never run.
        cmd.execute_with(|_| async { panic!("must not confirm on --check") })
            .await
            .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn merge_queue_reports_nothing_to_enqueue_when_none_eligible() {
        let target = tempfile::tempdir().unwrap();
        let (_dir, sock, server) = fake_daemon_replies(vec![json!({
            "ok": true,
            "payload": {
                "eligible": [],
                "skipped": [{ "path": "/b", "kind": "no-pr", "detail": "no open PR" }],
            }
        })]);
        let cmd = MergeQueueCommand {
            paths: vec![target.path().to_path_buf()],
            check: false,
            yes: false,
            socket: Some(sock),
        };
        // Nothing eligible → no confirm, no phase-2 call.
        cmd.execute_with(|_| async { panic!("must not confirm when nothing is eligible") })
            .await
            .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn merge_queue_aborts_when_confirmation_is_declined() {
        let target = tempfile::tempdir().unwrap();
        let (_dir, sock, server) = fake_daemon_replies(vec![json!({
            "ok": true,
            "payload": {
                "eligible": [{ "path": "/a", "number": 9, "url": "u", "branch": "feature" }],
                "skipped": [],
            }
        })]);
        let cmd = MergeQueueCommand {
            paths: vec![target.path().to_path_buf()],
            check: false,
            yes: false,
            socket: Some(sock),
        };
        // Declining aborts before the phase-2 call, so only one reply is consumed.
        cmd.execute_with(|count| async move {
            assert_eq!(count, 1);
            false
        })
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn merge_queue_enqueues_after_confirmation() {
        let target = tempfile::tempdir().unwrap();
        let (_dir, sock, server) = fake_daemon_replies(vec![
            json!({
                "ok": true,
                "payload": {
                    "eligible": [{ "path": "/a", "number": 9, "url": "u", "branch": "feature" }],
                    "skipped": [],
                }
            }),
            json!({
                "ok": true,
                "payload": {
                    "queued": [{ "path": "/a", "number": 9 }],
                    "skipped": [],
                    "failed": [],
                }
            }),
        ]);
        let cmd = MergeQueueCommand {
            paths: vec![target.path().to_path_buf()],
            check: false,
            yes: false,
            socket: Some(sock),
        };
        // Confirming drives the phase-2 execute, consuming the second reply.
        cmd.execute_with(|_| async { true }).await.unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn merge_queue_check_routes_through_the_worktrees_dispatch() {
        // Drives `WorktreesCommand::execute` → `MergeQueue` arm → `execute` (the
        // real-`confirm_enqueue` wiring), which `--check` returns from before any
        // confirmation, so no stdin is touched.
        let target = tempfile::tempdir().unwrap();
        let (_dir, sock, server) = fake_daemon_replies(vec![json!({
            "ok": true,
            "payload": { "eligible": [], "skipped": [] }
        })]);
        let cmd = WorktreesCommand {
            command: WorktreesSubcommands::MergeQueue(MergeQueueCommand {
                paths: vec![target.path().to_path_buf()],
                check: true,
                yes: false,
                socket: Some(sock),
            }),
        };
        cmd.execute(None).await.unwrap();
        server.await.unwrap();
    }

    // --- Reposition (#1407) ---------------------------------------------------

    #[test]
    fn reposition_parses_flags_and_enforces_the_undo_split() {
        let WorktreesSubcommands::Reposition(cmd) = parse(&[
            "reposition",
            "--reference",
            "/wt/ref",
            "/wt/a",
            "/wt/b",
            "--dry-run",
            "-o",
            "json",
        ]) else {
            panic!("expected the Reposition variant");
        };
        assert_eq!(cmd.reference.as_deref(), Some(Path::new("/wt/ref")));
        assert_eq!(
            cmd.paths,
            vec![PathBuf::from("/wt/a"), PathBuf::from("/wt/b")]
        );
        assert!(cmd.dry_run);
        assert!(!cmd.undo);
        assert_eq!(cmd.output, TableOrJson::Json);

        // `--undo` stands alone: no reference needed.
        let WorktreesSubcommands::Reposition(undo) = parse(&["reposition", "--undo"]) else {
            panic!("expected the Reposition variant");
        };
        assert!(undo.undo);
        assert!(undo.reference.is_none());
    }

    #[test]
    fn reposition_rejects_a_missing_reference_and_undo_combinations() {
        // Without `--undo`, a reference is mandatory — otherwise there is no
        // geometry to copy and the request cannot mean anything.
        assert!(RepositionCommand::try_parse_from(["reposition", "/wt/a"]).is_err());
        // `--undo` restores a recorded batch, so pairing it with a reference or a
        // dry run would be contradictory rather than merely redundant.
        assert!(RepositionCommand::try_parse_from([
            "reposition",
            "--undo",
            "--reference",
            "/wt/ref",
        ])
        .is_err());
        assert!(RepositionCommand::try_parse_from(["reposition", "--undo", "--dry-run"]).is_err());
    }

    #[test]
    fn window_key_for_matches_a_canonicalized_folder() {
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let wt = dir.path().join("tree");
        std::fs::create_dir(&wt).unwrap();
        let canonical = std::fs::canonicalize(&wt).unwrap();
        let windows = json!({
            "windows": [
                { "key": "other", "folders": ["/definitely/not/here"] },
                { "key": "wanted", "folders": [canonical.to_string_lossy()] },
            ]
        });
        assert_eq!(
            window_key_for(&windows, &wt, "repositioned").unwrap(),
            "wanted"
        );
    }

    #[test]
    fn window_key_for_errors_when_no_window_has_it_open() {
        // The CLI names its targets one at a time, so an unmatched one is a
        // mistake worth reporting — unlike a tree multi-select, where a stale row
        // is expected and skipped.
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let err = window_key_for(&json!({ "windows": [] }), dir.path(), "repositioned")
            .expect_err("an unopened worktree must not resolve");
        assert!(err.to_string().contains("no VS Code window has"), "{err:#}");
        // The verb names the caller's action, so the same helper serves both
        // commands without either one's error mentioning the other.
        let err = window_key_for(&json!({ "windows": [] }), dir.path(), "reloaded")
            .expect_err("an unopened worktree must not resolve");
        assert!(err.to_string().contains("can be reloaded"), "{err:#}");
        // A path that does not exist at all fails earlier, on canonicalization.
        let missing = dir.path().join("gone");
        let err = window_key_for(&json!({ "windows": [] }), &missing, "repositioned")
            .expect_err("a nonexistent path must not resolve");
        assert!(err.to_string().contains("cannot resolve"), "{err:#}");
    }

    #[test]
    fn reload_command_requires_at_least_one_path() {
        // Unlike the tree view's sweep, the CLI names each target, so an empty
        // invocation is a mistake rather than an empty batch.
        assert!(ReloadCommand::try_parse_from(["reload"]).is_err());
        let cmd = ReloadCommand::try_parse_from(["reload", "/wt/a", "/wt/b"]).unwrap();
        assert_eq!(cmd.paths.len(), 2);
        assert!(matches!(cmd.output, TableOrJson::Table));
        assert!(cmd.socket.is_none());
    }

    #[test]
    fn render_reload_reports_what_was_signalled_not_reloaded() {
        // "Signalled" is the only honest word: the directive rides each window's
        // ~10s heartbeat, so nothing has reloaded when this prints.
        let out = render_reload(&json!({ "requested": 2, "signalled": 2, "unknown": [] }));
        assert_eq!(out, "Signalled 2 of 2 windows to reload.");
        assert!(!out.contains("Reloaded"), "{out}");
        // Singular when exactly one window was signalled.
        let one = render_reload(&json!({ "requested": 1, "signalled": 1, "unknown": [] }));
        assert_eq!(one, "Signalled 1 of 1 window to reload.");
    }

    #[test]
    fn render_reload_names_windows_that_had_already_closed() {
        // A window that closed between the `list` and the op landing is named,
        // never silently dropped from the count.
        let out = render_reload(&json!({
            "requested": 3,
            "signalled": 1,
            "unknown": ["w2", "w3"],
        }));
        assert!(
            out.starts_with("Signalled 1 of 3 windows to reload."),
            "{out}"
        );
        assert!(out.contains("No longer open"), "{out}");
        assert!(out.contains("w2, w3"), "{out}");
    }

    #[test]
    fn render_reload_tolerates_a_reply_missing_every_field() {
        // Forward-compatible like the other renderers: a field the daemon did not
        // send reads as zero rather than panicking.
        assert_eq!(
            render_reload(&json!({})),
            "Signalled 0 of 0 windows to reload."
        );
    }

    #[test]
    fn render_reposition_explains_a_missing_permission() {
        let out = render_reposition(&json!({ "trusted": false, "results": [] }));
        assert!(out.contains("Accessibility permission"), "{out}");
        assert!(out.contains("daemon restart"), "{out}");
    }

    #[test]
    fn render_reposition_reports_a_blocked_batch() {
        let out = render_reposition(&json!({
            "trusted": true,
            "blocked": { "reason": "reference-ambiguous", "detail": "2 windows match “main”" },
            "results": [],
        }));
        assert!(out.contains("Nothing was moved"), "{out}");
        assert!(out.contains("reference-ambiguous"), "{out}");
        assert!(out.contains("2 windows match"), "{out}");
    }

    #[test]
    fn render_reposition_renders_the_reference_and_per_target_outcomes() {
        let out = render_reposition(&json!({
            "trusted": true,
            "reference": {
                "key": "r",
                "title": "ref-tree",
                "frame": { "x": 10.4, "y": 20.6, "width": 800.0, "height": 600.0 },
            },
            "moved": 1,
            "skipped": 1,
            "results": [
                { "key": "a", "title": "a-tree", "outcome": "moved", "detail": "moved into position" },
                { "key": "b", "title": "b-tree", "outcome": "ambiguous", "detail": "2 match" },
            ],
        }));
        assert!(
            out.contains("Reference: ref-tree 800×600 at (10, 21)"),
            "{out}"
        );
        assert!(out.contains("Moved: 1 / Skipped: 1"), "{out}");
        assert!(out.contains("moved: a-tree"), "{out}");
        assert!(out.contains("ambiguous: b-tree"), "{out}");
    }

    #[test]
    fn render_reposition_falls_back_to_the_key_and_notes_an_empty_batch() {
        // A target the daemon could not name (no title reported) is still
        // identified, by key.
        let out = render_reposition(&json!({
            "trusted": true,
            "results": [{ "key": "keyless", "outcome": "no-window", "detail": "gone" }],
        }));
        assert!(out.contains("no-window: keyless"), "{out}");
        // An undo with nothing recorded reports rather than printing a bare header.
        let empty = render_reposition(&json!({ "trusted": true, "results": [] }));
        assert!(empty.contains("(nothing to report)"), "{empty}");
    }

    #[test]
    fn render_reposition_strips_control_bytes_from_daemon_strings() {
        // A window title is companion-supplied metadata, so it cannot be allowed
        // to inject escape sequences into the operator's terminal (#1137).
        let out = render_reposition(&json!({
            "trusted": true,
            "results": [{
                "key": "k",
                "title": "evil\u{1b}[31mred",
                "outcome": "moved",
                "detail": "ok\u{7}",
            }],
        }));
        assert!(!out.contains('\u{1b}'), "{out:?}");
        assert!(!out.contains('\u{7}'), "{out:?}");
    }

    #[test]
    fn render_frame_formats_or_dashes() {
        assert_eq!(render_frame(None), "-");
        assert_eq!(
            render_frame(Some(
                &json!({ "x": 1.5, "y": -2.4, "width": 100.0, "height": 50.0 })
            )),
            "100×50 at (2, -2)"
        );
        // A malformed frame degrades to zeroes rather than panicking.
        assert_eq!(render_frame(Some(&json!({}))), "0×0 at (0, 0)");
    }

    #[test]
    fn print_reposition_emits_both_formats() {
        let reply = json!({ "trusted": true, "results": [], "moved": 0, "skipped": 0 });
        print_reposition(TableOrJson::Table, &reply).unwrap();
        print_reposition(TableOrJson::Json, &reply).unwrap();
    }

    #[tokio::test]
    async fn reposition_maps_paths_to_window_keys_and_sends_the_op() {
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let reference = dir.path().join("ref");
        let target = dir.path().join("tgt");
        std::fs::create_dir(&reference).unwrap();
        std::fs::create_dir(&target).unwrap();
        let (canon_ref, canon_tgt) = (
            std::fs::canonicalize(&reference).unwrap(),
            std::fs::canonicalize(&target).unwrap(),
        );

        // Two round trips: the `list` that resolves paths to window keys, then the
        // op itself.
        let (_sock_dir, sock, server) = fake_daemon_replies(vec![
            json!({ "ok": true, "payload": { "windows": [
                { "key": "ref-key", "folders": [canon_ref.to_string_lossy()] },
                { "key": "tgt-key", "folders": [canon_tgt.to_string_lossy()] },
            ] } }),
            json!({ "ok": true, "payload": {
                "trusted": true,
                "moved": 1,
                "skipped": 0,
                "results": [{ "key": "tgt-key", "outcome": "moved", "detail": "moved into position" }],
            } }),
        ]);

        RepositionCommand {
            paths: vec![target],
            reference: Some(reference),
            dry_run: false,
            undo: false,
            output: TableOrJson::Json,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn reposition_undo_skips_the_list_lookup_entirely() {
        // Undo addresses whatever the daemon recorded, so it needs no path
        // resolution and issues exactly one request.
        let (_dir, sock, server) = fake_daemon_reply(json!({
            "ok": true,
            "payload": { "trusted": true, "moved": 2, "skipped": 0, "results": [] },
        }));
        RepositionCommand {
            paths: Vec::new(),
            reference: None,
            dry_run: false,
            undo: true,
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn reposition_fails_before_the_op_when_a_target_has_no_window() {
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let reference = dir.path().join("ref");
        let target = dir.path().join("tgt");
        std::fs::create_dir(&reference).unwrap();
        std::fs::create_dir(&target).unwrap();
        let canon_ref = std::fs::canonicalize(&reference).unwrap();

        // Only the reference is open, so the target cannot be resolved — and the
        // `reposition` op is never sent, which is why one canned reply suffices.
        let (_sock_dir, sock, server) = fake_daemon_reply(json!({
            "ok": true,
            "payload": { "windows": [
                { "key": "ref-key", "folders": [canon_ref.to_string_lossy()] },
            ] },
        }));
        let err = RepositionCommand {
            paths: vec![target],
            reference: Some(reference),
            dry_run: true,
            undo: false,
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .expect_err("an unopened target must abort the command");
        assert!(err.to_string().contains("no VS Code window has"), "{err:#}");
        server.await.unwrap();
    }

    #[tokio::test]
    async fn reposition_surfaces_a_daemon_error() {
        let (_dir, sock, server) = fake_daemon_reply(json!({
            "ok": false,
            "error": "unknown worktrees op: reposition",
        }));
        let err = RepositionCommand {
            paths: Vec::new(),
            reference: None,
            dry_run: false,
            undo: true,
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .expect_err("an `ok:false` reply must not be reported as success");
        assert!(err.to_string().contains("unknown worktrees op"), "{err:#}");
        server.await.unwrap();
    }

    /// Two open worktrees plus the canned `list` reply that resolves both, so a
    /// reload test only has to supply the op's own reply.
    fn reload_fixture() -> (tempfile::TempDir, PathBuf, PathBuf, Value) {
        let dir = tempfile::tempdir_in("/tmp").unwrap();
        let a = dir.path().join("a");
        let b = dir.path().join("b");
        std::fs::create_dir(&a).unwrap();
        std::fs::create_dir(&b).unwrap();
        let list = json!({ "ok": true, "payload": { "windows": [
            { "key": "key-a", "folders": [std::fs::canonicalize(&a).unwrap().to_string_lossy()] },
            { "key": "key-b", "folders": [std::fs::canonicalize(&b).unwrap().to_string_lossy()] },
        ] } });
        (dir, a, b, list)
    }

    #[tokio::test]
    async fn reload_resolves_paths_to_window_keys_before_sending_the_op() {
        let (_dir, a, b, list) = reload_fixture();
        // Two requests: the `list` that maps paths to windows, then the op itself.
        // `fake_daemon_seq` (not `_replies`) so the sent payloads can be asserted.
        let (_sock_dir, sock, server) = fake_daemon_seq(vec![
            list,
            json!({ "ok": true, "payload": {
                "requested": 2, "signalled": 2, "unknown": [],
            } }),
        ]);

        ReloadCommand {
            paths: vec![a, b],
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();

        // The op addresses *windows*: the daemon must receive the resolved keys,
        // never the paths the user typed.
        let requests = server.await.unwrap();
        assert_eq!(requests[1]["op"], "reload");
        assert_eq!(
            requests[1]["payload"]["target_keys"],
            json!(["key-a", "key-b"])
        );
        assert!(
            requests[1]["payload"].get("requester_key").is_none(),
            "a CLI process is not a window, so it must not claim to be one"
        );
    }

    #[tokio::test]
    async fn reload_json_output_passes_the_reply_through_verbatim() {
        let (_dir, a, _b, list) = reload_fixture();
        let (_sock_dir, sock, server) = fake_daemon_replies(vec![
            list,
            json!({ "ok": true, "payload": {
                "requested": 1, "signalled": 0, "unknown": ["key-a"],
            } }),
        ]);
        // The `-o json` arm is the machine-readable surface, so it must not go
        // through the human renderer.
        ReloadCommand {
            paths: vec![a],
            output: TableOrJson::Json,
            socket: Some(sock),
        }
        .execute()
        .await
        .unwrap();
        server.await.unwrap();
    }

    #[tokio::test]
    async fn reload_fails_before_the_op_when_a_target_has_no_window() {
        let (_dir, a, b, _list) = reload_fixture();
        // Only `a` is open, so `b` cannot be resolved and the `reload` op is never
        // sent — which is why one canned reply suffices. Unlike the tree view's
        // silent skip, the CLI named this target explicitly, so it is an error.
        let (_sock_dir, sock, server) = fake_daemon_reply(json!({
            "ok": true,
            "payload": { "windows": [
                { "key": "key-a", "folders": [std::fs::canonicalize(&a).unwrap().to_string_lossy()] },
            ] },
        }));
        let err = ReloadCommand {
            paths: vec![a, b],
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .expect_err("an unopened target must abort the command");
        assert!(err.to_string().contains("can be reloaded"), "{err:#}");
        server.await.unwrap();
    }

    #[tokio::test]
    async fn reload_surfaces_a_daemon_error() {
        let (_dir, a, _b, list) = reload_fixture();
        let (_sock_dir, sock, server) = fake_daemon_replies(vec![
            list,
            json!({ "ok": false, "error": "unknown worktrees op: reload" }),
        ]);
        // An older daemon that predates #1417 rejects the op; that must surface
        // rather than read as a successful no-op.
        let err = ReloadCommand {
            paths: vec![a],
            output: TableOrJson::Table,
            socket: Some(sock),
        }
        .execute()
        .await
        .expect_err("an `ok:false` reply must not be reported as success");
        assert!(err.to_string().contains("unknown worktrees op"), "{err:#}");
        server.await.unwrap();
    }
}