mahbot 0.7.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
//! `mahbot chrome` — browser automation CLI over the shared core.
//!
//! Dispatched from `main()` before temp-root/lock init (runs alongside the
//! daemon), so it must not rely on the `/tmp/mahbot` temp root or initialized
//! config; chrome-use resolves to the product's own copy at its standard
//! install directory, which needs no config to work out.
//! Sessions are namespaced `mahbot-chrome-*` so they can never collide with
//! the interactive tool's `agent-tab-*`.
//!
//! stdout is exactly ONE line of JSON (the [`OutEnvelope`]); stderr carries
//! human-readable diagnostics. Exit codes follow the contract: 0 success,
//! 1 site/data step failure (schedulable), 2 environment failure (fix the
//! environment, don't blind-retry), 3 usage error. Every step runs to the two
//! clocks the [chrome policy](crate::chrome) defines — chrome-use's clock,
//! declared to it, and the product's own kill above that — as [`StepClocks`]
//! derives them for this surface. The wait action never exposes `--load`; its one
//! raw-argv use is `open`'s internal best-effort post-navigation settle.

use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use crate::chrome::actions;
use crate::chrome::contract::{
    ChromeResponse, ERROR_PAGE_PROBE_JS, EXPECT_TIMEOUT_NOTE, ErrorPageProbe, ExpectOutcome,
    OutEnvelope, OutKind, chrome_leftover_pipe_note, chrome_use_warning, classify_call_failure,
    eval_count, eval_result, expect_outcome, extract_net_error_code, extract_output,
    extract_snapshot_text, is_session_unresponsive_error, is_unreachable_tab_error,
    net_error_phrase, parse_error_page_probe, parse_first, sanitize_timeout_message,
    self_launched_browser_error, self_launched_browser_note, truncated_output_error,
    unreachable_tab_message, with_condition_timeout_note,
};
use crate::chrome::forms::{
    ExpectCond, ExtractGate, WaitTarget, count_eval_js, describe, expect_args, extract_gate,
    parse_count_op, parse_predicate, parse_state, text_value_argv, validate_extract_getters,
    wait_args, wait_target,
};
use crate::chrome::spawn::{CliRun, CliSpawn, CliTimeout, spawn_cli};
use crate::chrome::{
    CHROME_USE_DECLARED_BUDGET, CHROME_USE_OWN_BUDGET, CLI_EPHEMERAL_PREFIX, CLI_SESSION_PREFIX,
    ChromeCallClocks, CliRecovery, DEFAULT_OPEN_TIMEOUT, DEFAULT_STEP_TIMEOUT, KILL_SLACK,
    SESSION_STOP_TIMEOUT, clocks, is_blank_page_url, kill_bound, probe_clocks, validate_url,
};
use crate::tools::chrome_daemon::{
    CliStatus, SessionRecovery, cli_path, cli_probe, cli_version, ensure_ready_for_actions,
    readiness, recover_unresponsive_session,
};
use crate::util::{TOOL_OUTPUT_BUDGET_BYTES, truncate_sandwich};
use serde_json::{Value, json};

/// Product-side bound on the daemon-free `session list` enumerate `session
/// status` opens with: it only asks the daemon what sessions exist (it never
/// drives the browser), so a probe bound well under chrome-use's own budget is
/// what the product wants there — the relay self-heal would be pure waste.
const SESSION_LIST_TIMEOUT: Duration = Duration::from_secs(8);

/// `session status` probe bound: the product's own bounded liveness question —
/// "does the session answer at all" — so it deliberately does not wait out
/// chrome-use's own client tolerance, and declares that same short bound to
/// chrome-use ([`probe_clocks`]) rather than a longer one it would never let run.
/// A session that does not answer within it is [`Liveness::Unresponsive`] to
/// [`probe_session_liveness`]. Opt-in — never part of the default per-command path.
const SESSION_PROBE_TIMEOUT: Duration = Duration::from_secs(20);

/// The manual recovery flow for a wedged named session — named only where the
/// automatic recovery did not confirm or never started a stop
/// ([`wedge_recovery_note`]). References only verbs that exist in the mahbot
/// surface.
const SESSION_RECOVERY_FLOW: &str = "`mahbot chrome session stop <name>`, then re-run the action with \
     `--session <name>` to re-create it (cookies persist in the profile; open \
     tabs do not)";

/// The sentence appended to a wedge-shaped envelope: what the automatic recovery
/// did, or — where the stop was only issued and not confirmed, or never started —
/// the manual flow that remains. Shares the recovery summary with the tool so the
/// two surfaces cannot drift.
fn wedge_recovery_note(recovery: SessionRecovery) -> String {
    match recovery {
        SessionRecovery::Stopped => recovery.summary().to_string(),
        SessionRecovery::Unanswered | SessionRecovery::NotStarted => {
            // The summary itself says what happened (the stop was issued and
            // keeps running, or none could be started); what is appended is only
            // the manual flow left to the caller.
            format!(
                "{} If it is still wedged, recover with {SESSION_RECOVERY_FLOW}.",
                recovery.summary()
            )
        }
    }
}

/// What [`recover_session_wedge`] should do for one envelope.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WedgeAction {
    /// chrome-use's own session-unresponsive verdict: recover directly.
    Recover,
    /// The product's own clock ended the step: confirm liveness first, so a
    /// merely slow site never costs its session.
    Probe,
    /// No wedge evidence: leave the session (and the envelope) alone.
    Leave,
}

/// The wedge fact is WHO ended the step, not the envelope's kind on its own.
/// chrome-use's own session-unresponsive classification is a diagnosis, so it
/// recovers directly. A deadline-expiration label — Timeout, or the Redesign
/// `open --expect --structural` gives a deadline expiration — counts only when
/// the PRODUCT's own clock ended the step ([`CLI_STEP_KILLED_BY_CLOCK`]): only
/// then was chrome-use cut off before it could classify anything, leaving the
/// session's liveness genuinely unknown, whereas the same labels also carry
/// chrome-use's own honest timeout verdicts. Every other failure is left alone —
/// a failed `expect` / `open --expect` / `wait` produces a Timeout as its normal
/// verdict, and neither it nor an unprobed error may stop a session and lose its
/// tabs. Ephemeral sessions are left to the caller's own cleanup (the `<name>`
/// verbs are unactionable for them).
///
/// Pure so tests pin the keying.
#[must_use]
fn wedge_action(env: &OutEnvelope, named: bool, killed_by_clock: bool) -> WedgeAction {
    if !named {
        return WedgeAction::Leave;
    }
    if env.kind == OutKind::Environment
        && envelope_error(env).is_some_and(is_session_unresponsive_error)
    {
        return WedgeAction::Recover;
    }
    if matches!(env.kind, OutKind::Timeout | OutKind::Redesign) && killed_by_clock {
        return WedgeAction::Probe;
    }
    WedgeAction::Leave
}

/// The envelope's `error` text, when it carries one.
fn envelope_error(env: &OutEnvelope) -> Option<&str> {
    env.payload.get("error").and_then(Value::as_str)
}

/// Append a sentence to an envelope's `error` text; a no-op when it carries none.
fn append_error_note(env: &mut OutEnvelope, note: &str) {
    if let Some(obj) = env.payload.as_object_mut()
        && let Some(err) = obj.get("error").and_then(Value::as_str)
    {
        obj.insert("error".into(), json!(format!("{err} — {note}")));
    }
}

/// Recover a wedge the envelope points at (bounded) and put what was done into
/// its error text (see [`wedge_action`] for which envelopes count). An ephemeral
/// session is left to the caller's own cleanup, and a session the agent chose is
/// the only one whose recovery text can name the manual flow.
async fn recover_session_wedge(
    env: &mut OutEnvelope,
    session: &str,
    named: bool,
    killed_by_clock: bool,
) {
    let note = match wedge_action(env, named, killed_by_clock) {
        WedgeAction::Leave => return,
        WedgeAction::Recover => wedge_recovery_note(recover_unresponsive_session(session).await),
        WedgeAction::Probe => {
            // The product's own kill preempted chrome-use's verdict, so the wedge
            // is only a candidate: confirm it with the same bounded `get url`
            // liveness probe the `session status` verb uses.
            match probe_session_liveness(session).await {
                Liveness::Unresponsive => {
                    wedge_recovery_note(recover_unresponsive_session(session).await)
                }
                Liveness::Answered => {
                    "The session answered a liveness probe, so this was a slow call rather than a \
                     wedged session."
                        .to_string()
                }
                Liveness::Inconclusive => {
                    "The session's liveness could not be established, so its daemon was left \
                     alone; retry the call, and stop the session by hand if it stays \
                     unresponsive."
                        .to_string()
                }
            }
        }
    };
    append_error_note(env, &note);
}

/// What one bounded liveness probe of a named session established.
#[derive(Debug, Clone, Copy)]
enum Liveness {
    /// The session produced a normal answer: it is alive, so a clock-killed step
    /// was a slow call rather than a wedge.
    Answered,
    /// No answer within [`SESSION_PROBE_TIMEOUT`]: the session is wedged.
    Unresponsive,
    /// The probe could not be run, or its outcome could not be established (no
    /// binary, spawn failure, unparseable envelope): nothing was learned about
    /// the session, so nothing is recovered on its account.
    Inconclusive,
}

/// Bounded liveness probe of a named session (read-only `get url`, the probe the
/// `session status` verb uses). [`Liveness::Inconclusive`] is deliberately not
/// [`Liveness::Answered`] and not [`Liveness::Unresponsive`]: a probe that could
/// not run answers nothing about the session, so its daemon is left alone.
async fn probe_session_liveness(session: &str) -> Liveness {
    let Some(path) = cli_path() else {
        return Liveness::Inconclusive;
    };
    match spawn_step(
        &path,
        &["get", "url"],
        Some(session),
        StepClocks::probe(SESSION_PROBE_TIMEOUT),
        None,
    )
    .await
    {
        Ok(_) => Liveness::Answered,
        Err(f) if f.kind == OutKind::Timeout => Liveness::Unresponsive,
        Err(_) => Liveness::Inconclusive,
    }
}

/// Top-level `mahbot chrome -h` — rendered from the shared action registry.
#[must_use]
fn top_help() -> String {
    let mut out = String::from("mahbot chrome — browser automation CLI\n\n");
    out.push_str("Usage:\n");
    out.push_str("  mahbot chrome <action> [options]\n\n");
    out.push_str("Actions:\n");
    for d in actions::ACTIONS.iter().filter(|a| a.cli.is_some()) {
        let Some(cli) = d.cli.as_ref() else {
            continue;
        };
        if cli.syntax.is_empty() {
            let _ = writeln!(out, "  {}", d.name);
        } else {
            let _ = writeln!(out, "  {} {}", d.name, cli.syntax);
        }
        let _ = writeln!(out, "      {}", d.purpose);
    }
    out.push_str("\nGlobal flags:\n");
    out.push_str(
        "  --session <name>   use/name a session — letters, digits, '-' or '_' only (not valid for status / session subcommands)\n\n",
    );
    out.push_str("Output (stdout, one JSON line):\n");
    out.push_str(
        "  {\"schema\":1,\"action\":\"...\",\"ok\":true|false,\"kind\":\"...\",...payload}\n\n",
    );
    out.push_str("Exit codes:\n");
    out.push_str("  0  ok / empty (including a legitimately empty region)\n");
    out.push_str("  1  site/data step failure (timeout, network, redesign, not-found, error)\n");
    out.push_str("  2  environment failure (no chrome-use CLI/relay/Chrome/display)\n");
    out.push_str("  3  usage error\n\n");
    out.push_str("Help:\n");
    out.push_str("  mahbot chrome -h            this help\n");
    out.push_str("  mahbot chrome <action> -h   per-action help (flags, kinds, examples)\n");
    out
}

/// Per-action `mahbot chrome <action> -h`.
#[must_use]
fn action_help(name: &str) -> String {
    let Some(d) = actions::desc(name) else {
        return String::new();
    };
    let Some(cli) = d.cli.as_ref() else {
        return String::new();
    };

    let mut out = String::new();
    let _ = write!(out, "mahbot chrome {} — {}\n\n", d.name, d.purpose);
    out.push_str("Usage:\n");
    if cli.syntax.is_empty() {
        let _ = writeln!(out, "  mahbot chrome {}", d.name);
    } else {
        let _ = writeln!(out, "  mahbot chrome {} {}", d.name, cli.syntax);
    }

    let mut flag_rows: Vec<(&str, &str)> = cli.flags.to_vec();
    if cli.session {
        flag_rows.push((
            "--session <name>",
            "use/name a session — letters, digits, '-' or '_' only (not valid for status / session subcommands)",
        ));
    }
    if !flag_rows.is_empty() {
        let width = flag_rows.iter().map(|(f, _)| f.len()).max().unwrap_or(0);
        out.push_str("\nFlags:\n");
        for (flag, desc) in flag_rows {
            let _ = writeln!(out, "  {flag:<width$}  {desc}");
        }
    }

    out.push_str("\nKinds (stdout \"kind\" → exit code):\n");
    let kinds = cli
        .kinds
        .iter()
        .map(|k| format!("{} ({})", k.as_str(), k.exit_code()))
        .collect::<Vec<_>>()
        .join(" · ");
    let _ = writeln!(out, "  {kinds}");

    if !cli.details.is_empty() {
        let _ = write!(out, "\n{}\n", cli.details);
    }

    out.push_str("\nExamples:\n");
    for ex in cli.examples {
        let _ = writeln!(out, "  {ex}");
    }
    out
}

/// A per-action help request: the first positional token names a CLI action
/// and any token after it is `-h`/`--help`. Flags/session values before the
/// action word are skipped. Returns the action's registry name.
fn action_help_request(args: &[String]) -> Option<&'static str> {
    let (i, word) = first_positional(args)?;
    let d = actions::desc(word)?;
    d.cli
        .as_ref()
        .is_some_and(|_| args[i + 1..].iter().any(|t| t == "-h" || t == "--help"))
        .then_some(d.name)
}

/// A session the CLI resolved for an action that runs in one.
struct CliSession {
    name: String,
    ephemeral: bool,
}

/// Parsed CLI invocation.
struct Invocation {
    action: Action,
    session: Option<String>,
}

/// One parsed `mahbot chrome` action.
///
/// `timeout` on `Open`/`Wait`/`Expect` is the deadline the action declares for
/// the step (the user's `--timeout`, or the action's own default — forwarded to
/// chrome-use for wait/expect, and for `open` the budget that bounds the waits the
/// operation drives); `bound` on the rest is the user's own `--timeout`, or `None`
/// when they gave none — those verbs forward no `--timeout` chrome-use honours, so
/// their clock is the one mahbot declares to chrome-use (its own client tolerance
/// less the 2 s margin), a bound below that clock is REFUSED as a usage error
/// (nothing could honour it) and a longer one widens only the product's own kill
/// (see [`StepClocks`]).
enum Action {
    Status,
    Open {
        url: String,
        expect: Option<String>,
        structural: bool,
        timeout: Duration,
    },
    Count {
        selector: String,
        bound: Option<Duration>,
    },
    Wait {
        target: WaitTarget,
        timeout: Duration,
    },
    Expect {
        cond: ExpectCond,
        timeout: Duration,
    },
    Eval {
        js: String,
        bound: Option<Duration>,
    },
    Extract {
        schema_file: String,
        limit: Option<usize>,
        bound: Option<Duration>,
    },
    Click {
        selector: String,
        if_present: bool,
        bound: Option<Duration>,
    },
    Fill {
        selector: String,
        source: TextInput,
        bound: Option<Duration>,
    },
    Type {
        selector: String,
        text: String,
        key_events: bool,
        bound: Option<Duration>,
    },
    Press {
        key: String,
        selector: Option<String>,
        hold: Option<u64>,
        bound: Option<Duration>,
    },
    SessionStop {
        name: String,
        force: bool,
    },
    SessionStatus {
        name: String,
    },
}

impl Action {
    /// The shared-registry name of this action — the envelope's `action` field
    /// and the help lookup key. The `session` subcommands emit under
    /// `"session"`, the word the owner typed.
    fn name(&self) -> &'static str {
        match self {
            Action::Status => "status",
            Action::Open { .. } => "open",
            Action::Count { .. } => "count",
            Action::Wait { .. } => "wait",
            Action::Expect { .. } => "expect",
            Action::Eval { .. } => "eval",
            Action::Extract { .. } => "extract",
            Action::Click { .. } => "click",
            Action::Fill { .. } => "fill",
            Action::Type { .. } => "type",
            Action::Press { .. } => "press",
            Action::SessionStop { .. } | Action::SessionStatus { .. } => "session",
        }
    }
}

/// Where `fill` gets its text.
enum TextInput {
    /// Inline positional text (multi-word joined with spaces).
    Inline(String),
    /// `--file <path>` passthrough — chrome-use reads the file.
    File(String),
    /// `--stdin` — mahbot reads its own stdin and pipes the bytes.
    Stdin,
}

impl TextInput {
    /// The chrome-use argv suffix carrying the value (leading-dash inline
    /// text shielded with `--`; see [`forms::text_value_argv`]).
    fn argv(&self) -> Vec<String> {
        match self {
            Self::Inline(t) => text_value_argv(t),
            Self::File(p) => vec!["--file".into(), p.clone()],
            Self::Stdin => vec!["--stdin".into()],
        }
    }
}

/// A failed chrome-use step: the classified [`OutKind`] plus the error text
/// (empty for a mahbot-side deadline kill). The session-wedge recovery and its
/// text are applied after dispatch (see [`recover_session_wedge`]) — the one
/// place the resolved session name is in scope.
#[derive(Debug)]
struct StepFailure {
    kind: OutKind,
    message: String,
}

impl StepFailure {
    /// The failure envelope for `action`, layering the failure detail onto
    /// `base` params: a deadline kill reports `timeout_ms` (the product's own
    /// bound for the step — its kill) plus a factual `error` that says the
    /// product's own bound ended the call and which clock chrome-use was working
    /// to; any other failure reports the chrome-use `error` text with the
    /// accurate remediation layered on (unreachable-tab guidance), and Network
    /// failures carry the extracted net error token as `error_code` when the
    /// message contains one. The session-wedge recovery and its text are applied
    /// later, where the session name is known (see
    /// [`recover_session_wedge`]).
    fn envelope(self, action: &str, base: Value, clocks: StepClocks) -> OutEnvelope {
        let mut obj = match base {
            Value::Object(m) => m,
            other => {
                let mut m = serde_json::Map::new();
                m.insert("error".into(), other);
                m
            }
        };
        if self.kind == OutKind::Timeout && self.message.is_empty() {
            obj.insert("timeout_ms".into(), json!(clocks.call.kill.as_millis()));
            obj.insert(
                "error".into(),
                json!(deadline_error(
                    clocks.call.chrome_side,
                    clocks.call.kill,
                    "the step did not complete"
                )),
            );
        } else {
            let message = if is_unreachable_tab_error(&self.message) {
                unreachable_tab_message(&self.message)
            } else {
                self.message.clone()
            };
            obj.insert("error".into(), json!(message));
        }
        if self.kind == OutKind::Network
            && let Some(code) = extract_net_error_code(&self.message)
        {
            obj.insert("error_code".into(), json!(code));
        }
        out_env(action, false, self.kind, Value::Object(obj))
    }
}

/// Outcome of one spawned chrome-use step.
type StepOutcome = Result<ChromeResponse, StepFailure>;

/// Whether any session-scoped chrome-use spawn has been attempted this
/// process — an ephemeral session can only exist after such a spawn, so
/// cleanup is skipped (no stderr noise) when every failure happened before
/// any spawn attempt. Never read before `run_cli` resets it.
static CHROME_USE_SPAWNED: AtomicBool = AtomicBool::new(false);

/// Whether any chrome-use step this process ran exited while a process it left
/// behind still held the step's output channel
/// ([`CliOutput::leftover_pipes`]) — including the ephemeral close's own spawn,
/// whose envelope is already out, so [`run_cli`] reports that leftover on stderr.
/// The CLI handles ONE invocation per process, so this is the invocation's whole
/// record; `dispatch` turns a step's leftover into a note on the envelope
/// ([`chrome_leftover_pipe_note`]) — on a failure too — and never into a failure.
/// Never read before `run_cli` resets it.
static CHROME_USE_LEFTOVER_PIPES: AtomicBool = AtomicBool::new(false);

/// Whether the PRODUCT's own clock ended a step whose outcome IS the caller's
/// verdict ([`CliRun::TimedOut`]) ABOVE the clock chrome-use itself was working
/// to — the step's kill rode above its `chrome_side` (every step declares that
/// clock to chrome-use through `AGENT_BROWSER_DEFAULT_TIMEOUT`, see
/// [`crate::chrome::spawn::apply_chrome_side`]). chrome-use was then cut off
/// before it could classify anything, so what a named session's liveness is
/// remains unknown to it, which is exactly what [`recover_session_wedge`] keys
/// on. ONE rule decides the mark, applied where the step ends ([`spawn_step`]):
/// only a step that may run chrome-use's relay self-heal
/// ([`CliRecovery::Allowed`]) attributes anything, and only when its kill sat
/// above that `chrome_side`. The product's own probes and the operation's
/// best-effort sub-steps are [`CliRecovery::Suppressed`] — the product bounds
/// them on purpose and their outcome never becomes the envelope — so they can
/// never attribute. [`dispatch`] resets the mark before the action and reads it
/// right after the action returns; a later step of a composite operation cannot
/// erase an earlier decisive step's mark, and the wedge probe's own steps cannot
/// leak into it.
static CLI_STEP_KILLED_BY_CLOCK: AtomicBool = AtomicBool::new(false);

/// `mahbot chrome` CLI entry — returns the process exit code.
pub async fn run_cli(args: &[String]) -> i32 {
    // Re-enterable pub API: clear the previous call's spawn bookkeeping.
    CHROME_USE_SPAWNED.store(false, Ordering::Relaxed);
    CHROME_USE_LEFTOVER_PIPES.store(false, Ordering::Relaxed);
    CLI_STEP_KILLED_BY_CLOCK.store(false, Ordering::Relaxed);
    if let Some(a) = args.first()
        && (a == "-h" || a == "--help")
    {
        print!("{}", top_help());
        return 0;
    }
    // A `-h`/`--help` after the action word is per-action help — anywhere in
    // the tail, not just immediately after it (so `open <url> --help` and
    // `--session x open -h` work too). A `-h` before any action word falls
    // through to top-level help / normal parsing: no valid invocation starts
    // with a `-h`-looking token, parse_flags rejects those as usage errors, so
    // per-action help only turns would-be usage errors into help.
    if let Some(action) = action_help_request(args) {
        print!("{}", action_help(action));
        return 0;
    }

    let invocation = match parse_invocation(args) {
        Ok(inv) => inv,
        Err(msg) => {
            let action = recognized_action(args);
            eprintln!("mahbot chrome: {msg}");
            eprintln!("run 'mahbot chrome --help' for usage.");
            out_env(&action, false, OutKind::Usage, json!({ "error": msg })).emit();
            return 3;
        }
    };

    let (envelope, session) = dispatch(&invocation).await;
    envelope.emit();
    if let Some(s) = session.as_ref().filter(|s| s.ephemeral)
        && CHROME_USE_SPAWNED.load(Ordering::Relaxed)
    {
        // The close runs after the envelope is emitted, so a helper it leaves
        // behind cannot be noted in it — it is reported here, like every other
        // diagnostic of that close.
        CHROME_USE_LEFTOVER_PIPES.store(false, Ordering::Relaxed);
        close_ephemeral(&s.name).await;
        if CHROME_USE_LEFTOVER_PIPES.load(Ordering::Relaxed) {
            eprintln!("mahbot chrome: {}", chrome_leftover_pipe_note(&s.name));
        }
    }
    envelope.kind.exit_code()
}

// ── Parsing ──────────────────────────────────────────────────────

/// Flag sets for one action: `(value flags, boolean flags)`.
type FlagSet = (&'static [&'static str], &'static [&'static str]);

/// Per-action accepted flags — the single source for both the parser and the
/// CliHelp lockstep test. `(action word, value flags, boolean flags)`.
const ACTION_FLAGS: &[(&str, &[&str], &[&str])] = &[
    ("status", &[], &[]),
    ("open", &["expect", "timeout"], &["structural"]),
    ("count", &["timeout"], &[]),
    ("wait", &["timeout", "url", "text"], &[]),
    ("expect", &["timeout"], &[]),
    ("eval", &["timeout"], &[]),
    ("extract", &["schema-file", "limit", "timeout"], &[]),
    ("click", &["timeout"], &["if-present"]),
    ("fill", &["file", "timeout"], &["stdin"]),
    ("type", &["timeout"], &["key-events"]),
    ("press", &["selector", "hold", "timeout"], &[]),
    ("session", &[], &["force"]),
];

/// Accepted flags for `word`: `(value flags, boolean flags)`.
fn action_flags(word: &str) -> FlagSet {
    ACTION_FLAGS
        .iter()
        .find(|(name, ..)| *name == word)
        .map_or((&[], &[]), |(_, v, b)| (*v, *b))
}

fn parse_invocation(args: &[String]) -> Result<Invocation, String> {
    let (session, remaining) = extract_global_session(args)?;
    let action_word = remaining
        .first()
        .ok_or_else(|| "no action given".to_string())?;
    let rest = &remaining[1..];
    let allowed = action_flags(action_word);
    match action_word.as_str() {
        "status" => parse_status(session.as_deref(), rest),
        "open" => parse_open(session.as_deref(), rest, allowed),
        "count" => parse_count(session.as_deref(), rest, allowed),
        "wait" => parse_wait(session.as_deref(), rest, allowed),
        "expect" => parse_expect(session.as_deref(), rest, allowed),
        "eval" => parse_eval(session.as_deref(), rest, allowed),
        "extract" => parse_extract(session.as_deref(), rest, allowed),
        "click" => parse_click(session.as_deref(), rest, allowed),
        "fill" => parse_fill(session.as_deref(), rest, allowed),
        "type" => parse_type(session.as_deref(), rest, allowed),
        "press" => parse_press(session.as_deref(), rest, allowed),
        "session" => parse_session(session.as_deref(), rest, allowed),
        other => Err(format!("unknown action '{other}'")),
    }
}

/// Pull the global `--session` flag out of anywhere before the `--`
/// end-of-options marker, returning the session value (last occurrence wins)
/// and the remaining tokens (everything from `--` on kept verbatim).
fn extract_global_session(args: &[String]) -> Result<(Option<String>, Vec<String>), String> {
    let mut session: Option<String> = None;
    let mut remaining = Vec::new();
    let mut i = 0;
    while i < args.len() {
        let a = &args[i];
        // `--` ends option parsing (the fill/type text escape hatch): the
        // rest is verbatim text, never scanned for --session.
        if a == "--" {
            remaining.extend_from_slice(&args[i..]);
            break;
        }
        if a == "--session" {
            i += 1;
            let val = args
                .get(i)
                .ok_or_else(|| "missing value for --session".to_string())?;
            validate_session_name(val)?;
            session = Some(val.clone());
            i += 1;
        } else if let Some(val) = a.strip_prefix("--session=") {
            if val.is_empty() {
                return Err("missing value for --session".to_string());
            }
            validate_session_name(val)?;
            session = Some(val.to_string());
            i += 1;
        } else {
            remaining.push(a.clone());
            i += 1;
        }
    }
    Ok((session, remaining))
}

/// Index and token of the first positional argument: skips `--session <name>`
/// (with its value), `--session=<name>`, and any other `-`-prefixed flag.
fn first_positional(args: &[String]) -> Option<(usize, &str)> {
    let mut i = 0;
    while i < args.len() {
        let a = &args[i];
        if a == "--session" {
            i += 2;
        } else if a.starts_with('-') {
            i += 1;
        } else {
            return Some((i, a));
        }
    }
    None
}

/// The action word to put on a usage envelope — the first non-flag token when
/// it is a recognized action, else `"usage"`.
fn recognized_action(args: &[String]) -> String {
    match first_positional(args) {
        Some((_, a)) if actions::is_cli_action(a) => a.to_string(),
        _ => "usage".to_string(),
    }
}

/// Split action args into positional tokens and a `--flag` map, handling both
/// `--flag value` and `--flag=value`. `value_flags` name flags that take a
/// value; `bool_flags` name boolean flags.
fn parse_flags(
    args: &[String],
    value_flags: &[&str],
    bool_flags: &[&str],
) -> Result<(Vec<String>, Flags), String> {
    parse_flags_impl(args, value_flags, bool_flags, false)
}

/// [`parse_flags`] for the fill/type text actions: a single-dash token is
/// taken as literal positional text (these actions have no single-dash
/// flags, so `fill '#q' -tail` fills the text `-tail` naturally instead of
/// demanding the `--` shield). Unknown `--flags` are still rejected.
fn parse_flags_text(
    args: &[String],
    value_flags: &[&str],
    bool_flags: &[&str],
) -> Result<(Vec<String>, Flags), String> {
    parse_flags_impl(args, value_flags, bool_flags, true)
}

fn parse_flags_impl(
    args: &[String],
    value_flags: &[&str],
    bool_flags: &[&str],
    single_dash_is_text: bool,
) -> Result<(Vec<String>, Flags), String> {
    let mut positionals = Vec::new();
    let mut flags = Flags::default();
    let mut i = 0;
    while i < args.len() {
        let a = &args[i];
        // `--` ends option parsing: the rest are positionals verbatim (the
        // escape hatch for a leading-dash fill/type text).
        if a == "--" {
            positionals.extend(args[i + 1..].iter().cloned());
            break;
        }
        if let Some(body) = a.strip_prefix("--") {
            let (name, eq_value) = match body.split_once('=') {
                Some((n, v)) => (n, Some(v)),
                None => (body, None),
            };
            if value_flags.contains(&name) {
                let value = match eq_value {
                    Some(v) if !v.is_empty() => v.to_string(),
                    Some(_) => return Err(format!("missing value for --{name}")),
                    None => {
                        i += 1;
                        args.get(i)
                            .ok_or_else(|| format!("missing value for --{name}"))?
                            .clone()
                    }
                };
                flags.values.insert(name.to_string(), value);
                i += 1;
            } else if bool_flags.contains(&name) {
                if eq_value.is_some() {
                    return Err(format!("flag --{name} does not take a value"));
                }
                flags.bools.insert(name.to_string());
                i += 1;
            } else {
                return Err(format!("unknown flag --{name}"));
            }
            continue;
        }
        if a.starts_with('-') && a.len() > 1 && !single_dash_is_text {
            return Err(format!("unknown flag {a}"));
        }
        positionals.push(a.clone());
        i += 1;
    }
    Ok((positionals, flags))
}

/// Positional/flag accumulator for one action.
#[derive(Default)]
struct Flags {
    values: HashMap<String, String>,
    bools: HashSet<String>,
}

impl Flags {
    #[must_use]
    fn value(&self, name: &str) -> Option<&str> {
        self.values.get(name).map(String::as_str)
    }

    #[must_use]
    fn has(&self, name: &str) -> bool {
        self.bools.contains(name)
    }
}

fn parse_status(session: Option<&str>, rest: &[String]) -> Result<Invocation, String> {
    if session.is_some() {
        return Err("--session is not valid for status".to_string());
    }
    if !rest.is_empty() {
        return Err("unexpected arguments for status".to_string());
    }
    Ok(Invocation {
        action: Action::Status,
        session: None,
    })
}

fn parse_open(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let url = take_positional(&positionals, 0, "url")?;
    reject_extra_positionals(&positionals, 1)?;
    Ok(Invocation {
        action: Action::Open {
            url,
            expect: flags.value("expect").map(String::from),
            structural: flags.has("structural"),
            timeout: parse_forwarded_timeout(&flags)?.unwrap_or(DEFAULT_OPEN_TIMEOUT),
        },
        session: session.map(String::from),
    })
}

fn parse_count(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let selector = take_positional(&positionals, 0, "selector")?;
    reject_extra_positionals(&positionals, 1)?;
    Ok(Invocation {
        action: Action::Count {
            selector,
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_wait(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let target = wait_target(
        positionals.first().map(String::as_str),
        flags.value("url"),
        flags.value("text"),
    )?;
    reject_extra_positionals(&positionals, 1)?;
    Ok(Invocation {
        action: Action::Wait {
            target,
            timeout: parse_forwarded_timeout(&flags)?.unwrap_or(DEFAULT_STEP_TIMEOUT),
        },
        session: session.map(String::from),
    })
}

fn parse_expect(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let cond = parse_expect_cond(&positionals)?;
    Ok(Invocation {
        action: Action::Expect {
            cond,
            timeout: parse_forwarded_timeout(&flags)?.unwrap_or(DEFAULT_STEP_TIMEOUT),
        },
        session: session.map(String::from),
    })
}

/// chrome-use 1.5.101 selector-first expect grammar, validated against the
/// safe allowlist: `<sel> <visible|hidden|present>`, `count <sel> <op> <n>`,
/// `text|value <sel> <pred> <value>`, `attr <sel> <name> <pred> <value>`,
/// `url <pred> <pattern>`. Trailing words of a text/value/attr/url payload
/// are joined with spaces (shell quoting is the normal path).
#[expect(clippy::too_many_lines)]
fn parse_expect_cond(ps: &[String]) -> Result<ExpectCond, String> {
    let usage = "expect <selector> <visible|hidden|present> | count <sel> <op> <n> | text|value <sel> <equals|contains|matches> <value> | attr <sel> <name> <pred> <value> | url <pred> <pattern>";
    let first = ps
        .first()
        .map(String::as_str)
        .ok_or_else(|| format!("missing condition — {usage}"))?;
    match first {
        "count" => {
            if ps.len() != 4 {
                return Err("expect count takes exactly: count <selector> <op> <n>".to_string());
            }
            let op = parse_count_op(&ps[2]).ok_or_else(|| {
                format!(
                    "invalid count op '{}' — use == != > < >= <= (or eq ne gt lt ge le)",
                    ps[2]
                )
            })?;
            let n: u64 = ps[3]
                .parse()
                .map_err(|_| format!("count comparison needs a number, got '{}'", ps[3]))?;
            Ok(ExpectCond::Count {
                selector: ps[1].clone(),
                op,
                n,
            })
        }
        "text" | "value" => {
            if ps.len() < 4 {
                return Err(format!(
                    "expect {first} takes: {first} <selector> <equals|contains|matches> <value>"
                ));
            }
            let predicate = parse_predicate(&ps[2]).ok_or_else(|| {
                format!(
                    "invalid predicate '{}' — use equals|contains|matches",
                    ps[2]
                )
            })?;
            let value = ps[3..].join(" ");
            let selector = ps[1].clone();
            Ok(if first == "text" {
                ExpectCond::Text {
                    selector,
                    predicate,
                    value,
                }
            } else {
                ExpectCond::Value {
                    selector,
                    predicate,
                    value,
                }
            })
        }
        "attr" => {
            if ps.len() < 5 {
                return Err(
                    "expect attr takes: attr <selector> <name> <equals|contains|matches> <value>"
                        .to_string(),
                );
            }
            let predicate = parse_predicate(&ps[3]).ok_or_else(|| {
                format!(
                    "invalid predicate '{}' — use equals|contains|matches",
                    ps[3]
                )
            })?;
            Ok(ExpectCond::Attr {
                selector: ps[1].clone(),
                name: ps[2].clone(),
                predicate,
                value: ps[4..].join(" "),
            })
        }
        "url" => {
            if ps.len() < 3 {
                return Err("expect url takes: url <equals|contains|matches> <pattern>".to_string());
            }
            let predicate = parse_predicate(&ps[1]).ok_or_else(|| {
                format!(
                    "invalid predicate '{}' — use equals|contains|matches",
                    ps[1]
                )
            })?;
            Ok(ExpectCond::Url {
                predicate,
                pattern: ps[2..].join(" "),
            })
        }
        sel => {
            if ps.len() != 2 {
                return Err(format!("expect takes a selector and one state — {usage}"));
            }
            let state = parse_state(&ps[1]).ok_or_else(|| {
                format!(
                    "unsupported condition '{}' — allowed states: visible|hidden|present (count/text/value/attr/url have their own forms)",
                    ps[1]
                )
            })?;
            Ok(ExpectCond::State {
                selector: sel.to_string(),
                state,
            })
        }
    }
}

fn parse_eval(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let js = take_positional(&positionals, 0, "js")?;
    reject_extra_positionals(&positionals, 1)?;
    Ok(Invocation {
        action: Action::Eval {
            js,
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_extract(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    if !positionals.is_empty() {
        return Err(format!("unexpected argument '{}'", positionals[0]));
    }
    let schema_file = flags
        .value("schema-file")
        .ok_or_else(|| "missing required --schema-file <path>".to_string())?
        .to_string();
    Ok(Invocation {
        action: Action::Extract {
            schema_file,
            limit: parse_limit_flag(&flags)?,
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_click(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let selector = take_positional(&positionals, 0, "selector")?;
    reject_extra_positionals(&positionals, 1)?;
    Ok(Invocation {
        action: Action::Click {
            selector,
            if_present: flags.has("if-present"),
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_fill(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags_text(rest, value_flags, bool_flags)?;
    let usage = "fill <selector> <text | --file <path> | --stdin>";
    let selector = match positionals.first() {
        Some(s) => s.clone(),
        None => return Err(format!("missing selector — {usage}")),
    };
    let inline = positionals.get(1..).unwrap_or_default();
    let file = flags.value("file");
    let stdin = flags.has("stdin");
    let source = match (file, stdin, inline.is_empty()) {
        (Some(f), false, true) => TextInput::File(f.to_string()),
        (None, true, true) => TextInput::Stdin,
        (None, false, false) => TextInput::Inline(inline.join(" ")),
        _ => return Err(format!("exactly one text source required — {usage}")),
    };
    Ok(Invocation {
        action: Action::Fill {
            selector,
            source,
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_type(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags_text(rest, value_flags, bool_flags)?;
    let usage = "type <selector> <text> [--key-events]";
    let selector = match positionals.first() {
        Some(s) => s.clone(),
        None => return Err(format!("missing selector — {usage}")),
    };
    let text = positionals.get(1..).unwrap_or_default().join(" ");
    if text.is_empty() {
        return Err(format!("missing text — {usage}"));
    }
    Ok(Invocation {
        action: Action::Type {
            selector,
            text,
            key_events: flags.has("key-events"),
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_press(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    let (value_flags, bool_flags) = allowed;
    let (positionals, flags) = parse_flags(rest, value_flags, bool_flags)?;
    let usage = "press <key> [--selector <sel>] [--hold <ms>]";
    let key = match positionals.first() {
        Some(s) => s.clone(),
        None => return Err(format!("missing key — {usage}")),
    };
    reject_extra_positionals(&positionals, 1)?;
    let hold = match flags.value("hold") {
        Some(v) => Some(
            v.parse::<u64>()
                .map_err(|_| format!("--hold must be an integer (ms): {v}"))?,
        ),
        None => None,
    };
    Ok(Invocation {
        action: Action::Press {
            key,
            selector: flags.value("selector").map(String::from),
            hold,
            bound: parse_bound_timeout(&flags)?,
        },
        session: session.map(String::from),
    })
}

fn parse_session(
    session: Option<&str>,
    rest: &[String],
    allowed: FlagSet,
) -> Result<Invocation, String> {
    if session.is_some() {
        return Err("--session is not valid for session stop/status".to_string());
    }
    let sub = rest
        .first()
        .map(String::as_str)
        .ok_or_else(|| "usage: session stop <name> | session status <name>".to_string())?;
    match sub {
        "stop" => {
            let (value_flags, bool_flags) = allowed;
            let (positionals, flags) = parse_flags(&rest[1..], value_flags, bool_flags)?;
            let name = take_positional(&positionals, 0, "name")?;
            validate_session_name(&name)?;
            reject_extra_positionals(&positionals, 1)?;
            Ok(Invocation {
                action: Action::SessionStop {
                    name,
                    force: flags.has("force"),
                },
                session: None,
            })
        }
        "status" => {
            let (value_flags, bool_flags) = allowed;
            let (positionals, flags) = parse_flags(&rest[1..], value_flags, bool_flags)?;
            if flags.has("force") {
                return Err("--force is not valid for session status".to_string());
            }
            let name = take_positional(&positionals, 0, "name")?;
            validate_session_name(&name)?;
            reject_extra_positionals(&positionals, 1)?;
            Ok(Invocation {
                action: Action::SessionStatus { name },
                session: None,
            })
        }
        other => Err(format!(
            "unknown session subcommand '{other}' (expected 'stop' or 'status')"
        )),
    }
}

fn take_positional(positionals: &[String], idx: usize, name: &str) -> Result<String, String> {
    positionals
        .get(idx)
        .cloned()
        .ok_or_else(|| format!("missing positional: {name}"))
}

fn reject_extra_positionals(positionals: &[String], expected: usize) -> Result<(), String> {
    if positionals.len() > expected {
        return Err(format!("unexpected argument '{}'", positionals[expected]));
    }
    Ok(())
}

/// The value of a forwarded `--timeout` — the deadline this verb IS given
/// (`wait`, `expect`, and `open`, whose `--expect` wait forwards its remaining
/// budget), so chrome-use honours it in full. A value at or above chrome-use's own
/// client tolerance ([`CHROME_USE_OWN_BUDGET`]) is refused: chrome-use cannot work
/// past that tolerance, so a deadline at it makes the tool run out of tolerance
/// instead of reporting its own reason, and its session-unresponsive verdict
/// (which stops the session and loses its tabs) replaces the honest one. At least
/// 1 s, and `None` when the user gave no `--timeout` (the verb's own default then
/// applies at its parse site).
fn parse_forwarded_timeout(flags: &Flags) -> Result<Option<Duration>, String> {
    let Some(secs) = parse_timeout_secs(flags)? else {
        return Ok(None);
    };
    if secs >= CHROME_USE_OWN_BUDGET.as_secs() {
        return Err(format!(
            "--timeout {secs}s is at or above chrome-use's own client tolerance ({}s): \
             chrome-use cannot work past it, so a deadline that long makes it run out of \
             tolerance instead of reporting its own reason, and its session-unresponsive \
             verdict replaces the honest one. Use less than {}s.",
            CHROME_USE_OWN_BUDGET.as_secs(),
            CHROME_USE_OWN_BUDGET.as_secs()
        ));
    }
    Ok(Some(Duration::from_secs(secs)))
}

/// The value of a bound `--timeout` — every verb chrome-use takes no per-call
/// deadline for (count, eval, extract, click, fill, type, press). Nothing can make
/// chrome-use give up earlier on those, so a bound below the clock the product
/// declares to chrome-use ([`CHROME_USE_DECLARED_BUDGET`]) CANNOT be honoured: it
/// is refused as a usage error rather than accepted and silently discarded. A
/// bound at or above that clock is accepted — the call is never cut off below it,
/// and above the clock it widens the product's own kill
/// ([`StepClocks::tool_clock`]) — and `None` (no flag) leaves the call at the
/// declared clock.
fn parse_bound_timeout(flags: &Flags) -> Result<Option<Duration>, String> {
    let Some(secs) = parse_timeout_secs(flags)? else {
        return Ok(None);
    };
    let declared = CHROME_USE_DECLARED_BUDGET.as_secs();
    if secs < declared {
        return Err(format!(
            "--timeout {secs}s cannot be honoured: chrome-use takes no per-call deadline for this \
             verb, so the call runs to the {declared}s clock mahbot declares to chrome-use and \
             mahbot never cuts the call off below it — the reported bound would not be the one you \
             asked for. Give at least {declared}s (that is the shortest bound the call can be given), \
             or omit --timeout. `wait`/`expect` — and `open`'s --expect wait — are the verbs that \
             forward a deadline chrome-use honours in full."
        ));
    }
    Ok(Some(Duration::from_secs(secs)))
}

/// The user's own `--timeout` in seconds, or `None` when they gave none — the one
/// seconds parser behind [`parse_forwarded_timeout`] and [`parse_bound_timeout`],
/// which the parse sites call by name. There is deliberately no per-step default
/// any more: a step's declared clock is [`CHROME_USE_DECLARED_BUDGET`] unless the
/// verb forwards a deadline chrome-use honours in full (`wait`/`expect`), and the
/// actions that declare a deadline of their own (`wait`, `expect`, `open`) apply
/// their default at their parse site.
fn parse_timeout_secs(flags: &Flags) -> Result<Option<u64>, String> {
    match flags.value("timeout") {
        Some(v) => {
            let secs: u64 = v
                .parse()
                .map_err(|_| format!("--timeout must be an integer: {v}"))?;
            if secs < 1 {
                return Err("--timeout must be at least 1 second".to_string());
            }
            Ok(Some(secs))
        }
        None => Ok(None),
    }
}

fn parse_limit_flag(flags: &Flags) -> Result<Option<usize>, String> {
    match flags.value("limit") {
        Some(v) => {
            let n: usize = v
                .parse()
                .map_err(|_| format!("--limit must be an integer: {v}"))?;
            Ok(Some(n))
        }
        None => Ok(None),
    }
}

// ── Session resolution ───────────────────────────────────────────

/// Resolve the session name and whether it is ephemeral: a named `--session`
/// becomes `mahbot-chrome-<name>` (idempotent for an already-prefixed name);
/// no flag yields an ephemeral `mahbot-chrome-ephemeral-<suffix>` session.
///
/// The flag's own value is validated at extraction ([`extract_global_session`]),
/// so a name reaching here is already a plain identifier.
fn resolve_session(flag: Option<&str>) -> (String, bool) {
    match flag {
        Some(name) => {
            let name = if name.starts_with(CLI_SESSION_PREFIX) {
                name.to_string()
            } else {
                format!("{CLI_SESSION_PREFIX}{name}")
            };
            (name, false)
        }
        None => (
            format!("{CLI_EPHEMERAL_PREFIX}{}", crate::generate_suffix()),
            true,
        ),
    }
}

/// Session namespaces the CLI must never stop without an explicit `--force`:
/// the interactive tool's per-run sessions and link enrichment's sessions.
const PROTECTED_SESSION_PREFIXES: [&str; 2] = ["agent-tab-", "link-enricher-"];

/// Validate a caller-supplied session name. The name becomes part of a command
/// recipe the agent is told to run (`mahbot chrome session stop <name> --force`
/// in a leftover note), so it is restricted to the plain identifier the tool
/// mints for its own sessions; anything else is a usage error rather than a
/// name that could turn that recipe into a different command. The alphabet is
/// chrome-use's own (`validation::is_valid_session_name`: alphanumerics, '-' and
/// '_'): a '.' is refused, because chrome-use refuses it on `session stop` — a
/// name the product accepted but chrome-use will not stop is a session that can
/// be neither cleared by hand nor recovered, its daemon and tabs left behind.
fn validate_session_name(name: &str) -> Result<(), String> {
    let plain = !name.is_empty()
        && name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_'));
    if plain {
        Ok(())
    } else {
        Err(format!(
            "invalid session name {name:?} — use letters, digits, '-' or '_'"
        ))
    }
}

/// Resolve a session name for a session-word subcommand. A bare name is
/// treated as a CLI session name and prefixed (consistent with `--session`);
/// CLI-namespace and protected-namespace names pass through as-is (status may
/// probe protected sessions read-only; only stop gates them behind --force).
/// The name is validated when the invocation is parsed
/// ([`validate_session_name`]).
fn resolve_session_target(name: &str) -> String {
    if name.starts_with(CLI_SESSION_PREFIX)
        || PROTECTED_SESSION_PREFIXES
            .iter()
            .any(|p| name.starts_with(p))
    {
        return name.to_string();
    }
    format!("{CLI_SESSION_PREFIX}{name}")
}

/// `session stop` gating on top of [`resolve_session_target`].
fn resolve_stop_target(name: &str, force: bool) -> Result<String, String> {
    let target = resolve_session_target(name);
    if !force
        && PROTECTED_SESSION_PREFIXES
            .iter()
            .any(|p| target.starts_with(p))
    {
        return Err(format!(
            "{name} is not a mahbot-chrome session; pass --force to stop it anyway"
        ));
    }
    Ok(target)
}

// ── Dispatch ─────────────────────────────────────────────────────

/// The pure argument checks the pre-action arm runs BEFORE the readiness gate:
/// the usage errors an action can answer without probing anything, so they are
/// never buried under an environment refusal. Only checks that are pure and
/// already exist inside the action live here — `open`'s URL and `--expect`
/// validation, which `open` re-runs for real.
fn validate_action_args(action: &Action) -> Option<OutEnvelope> {
    let Action::Open { url, expect, .. } = action else {
        return None;
    };
    open_args(url, expect.as_deref()).err()
}

async fn dispatch(invocation: &Invocation) -> (OutEnvelope, Option<CliSession>) {
    match &invocation.action {
        Action::Status => (stamped(status()).await, None),
        Action::SessionStop { name, force } => (stamped(session_stop(name, *force)).await, None),
        Action::SessionStatus { name } => (stamped(session_status(name)).await, None),
        action => {
            // The action's own clock: every envelope this arm returns — refusals
            // included — is stamped with it (see [`stamp_elapsed`]).
            let started = Instant::now();
            // The session this action runs in, resolved first so the argument
            // refusal below carries it like every other action envelope.
            let (name, ephemeral) = resolve_session(invocation.session.as_deref());
            // Pure argument checks come next: a malformed URL or `--expect`
            // selector is a usage error whatever the environment looks like, so
            // it is answered as one, without a readiness probe that could bury
            // the precise usage error under an environment refusal (see
            // [`validate_action_args`]).
            if let Some(mut refusal) = validate_action_args(action) {
                surface_session(&mut refusal, &name);
                stamp_elapsed(&mut refusal, started);
                return (refusal, None);
            }
            // The pre-action gate: no action is dispatched until the connection
            // to the owner's REAL browser is established, so the work can never
            // quietly land in a browser chrome-use launches itself. The
            // probe/cleanup verbs (`status`, `session …`) run before this arm and
            // are deliberately not gated — they are what reports and repairs.
            //
            // A missing helper is its own case, checked BEFORE the gate: the
            // gate's refusal is about a connection it cannot even probe, so
            // running it (and its recovery pass) would answer an absent helper
            // with the long speculative readiness refusal instead of this
            // precise missing-install report — the same envelope `require_cli`
            // builds.
            if cli_path().is_none() {
                let error = "chrome-use CLI not found";
                eprintln!("mahbot chrome: {error}");
                let mut env = env_failure(action.name(), json!({}), error);
                // As in the readiness refusal below, the resolved session is
                // surfaced like every other action envelope's.
                surface_session(&mut env, &name);
                stamp_elapsed(&mut env, started);
                return (env, None);
            }
            if let Err(refusal) = ensure_ready_for_actions().await {
                eprintln!("mahbot chrome: {refusal}");
                let mut env = out_env(
                    action.name(),
                    false,
                    OutKind::Environment,
                    json!({ "error": refusal }),
                );
                // The refused action's session is surfaced like every other
                // action envelope's, so a swallowed or defaulted `--session` is
                // observable here too.
                surface_session(&mut env, &name);
                stamp_elapsed(&mut env, started);
                return (env, None);
            }
            CLI_STEP_KILLED_BY_CLOCK.store(false, Ordering::Relaxed);
            let mut env = match action {
                Action::Open {
                    url,
                    expect,
                    structural,
                    timeout,
                } => open(url, expect.as_deref(), *structural, *timeout, &name).await,
                Action::Count { selector, bound } => count(selector, *bound, &name).await,
                Action::Wait { target, timeout } => wait(target, *timeout, &name).await,
                Action::Expect { cond, timeout } => expect(cond, *timeout, &name).await,
                Action::Eval { js, bound } => eval(js, *bound, &name).await,
                Action::Extract {
                    schema_file,
                    limit,
                    bound,
                } => extract(schema_file, *limit, *bound, &name).await,
                Action::Click {
                    selector,
                    if_present,
                    bound,
                } => click(selector, *if_present, *bound, &name).await,
                Action::Fill {
                    selector,
                    source,
                    bound,
                } => fill(selector, source, *bound, &name).await,
                Action::Type {
                    selector,
                    text,
                    key_events,
                    bound,
                } => r#type(selector, text, *key_events, *bound, &name).await,
                Action::Press {
                    key,
                    selector,
                    hold,
                    bound,
                } => press(key, selector.as_deref(), *hold, *bound, &name).await,
                Action::Status | Action::SessionStop { .. } | Action::SessionStatus { .. } => {
                    unreachable!("handled by the outer match")
                }
            };
            // Read the kill attribution before ANY recovery call — hygiene
            // only: the wedge probe's own steps are probes
            // ([`CliRecovery::Suppressed`]), so they cannot re-attribute this
            // action's ending.
            let killed_by_clock = CLI_STEP_KILLED_BY_CLOCK.load(Ordering::Relaxed);
            // Surface the resolved session (named or defaulted ephemeral) so
            // a swallowed or silently defaulted `--session` is always
            // observable in the envelope.
            surface_session(&mut env, &name);
            // The call's own wall time rides every envelope this arm returns — a
            // failing call's duration is read straight off it — while
            // `timeout_ms` (the declared/kill bound) stays a separate fact.
            stamp_elapsed(&mut env, started);
            // A process chrome-use left behind still holding a step's output
            // channel is a NOTE whatever the call's own outcome — also on a
            // failure: the call finished, its exit status and output are the
            // answer, and the leftover was started deliberately and is left
            // running. It sits alongside `error` and never changes the kind or
            // the exit code.
            if CHROME_USE_LEFTOVER_PIPES.load(Ordering::Relaxed) {
                surface_leftover_note(&mut env, &name);
            }
            // A session that stopped answering is RECOVERED here — the one place
            // the resolved session name is in scope — and the envelope's error
            // text says what was done ([`recover_session_wedge`]). `named` is the
            // resolution's own answer, not re-derived from the name.
            recover_session_wedge(&mut env, &name, !ephemeral, killed_by_clock).await;
            (env, Some(CliSession { name, ephemeral }))
        }
    }
}

/// Insert the leftover-helper note as a top-level envelope field on the result —
/// success or failure (the payload is flattened, so a payload key is a top-level
/// stdout key, sitting alongside `error`). The note names the chrome-use session
/// the call drove, so it is built from the resolved session name
/// ([`chrome_leftover_pipe_note`]).
fn surface_leftover_note(env: &mut OutEnvelope, name: &str) {
    if let Some(obj) = env.payload.as_object_mut() {
        obj.insert("leftover".into(), json!(chrome_leftover_pipe_note(name)));
    }
}

/// Insert the resolved session name as a top-level envelope field (the
/// payload is flattened, so a payload key is a top-level stdout key). The
/// `session` subcommand envelopes carry their own `session` key and skip
/// this — they run session-unscoped.
fn surface_session(env: &mut OutEnvelope, name: &str) {
    if let Some(obj) = env.payload.as_object_mut() {
        obj.insert("session".into(), json!(name));
    }
}

/// Stamp one action envelope with how long the call took, from the instant the
/// action's own clock started. Every envelope of an action that RAN carries it —
/// a success, a failure of any kind, and an argument/environment refusal alike —
/// because a call's duration is a fact about the call itself, not something a
/// reader should have to infer or time for itself; the one envelope without it is
/// the usage refusal of a command line that never parsed ([`run_cli`]), where no
/// action ever started. `timeout_ms`, the declared or killed bound, stays a
/// separate fact.
fn stamp_elapsed(env: &mut OutEnvelope, started: Instant) {
    if let Some(obj) = env.payload.as_object_mut() {
        obj.insert("elapsed_ms".into(), json!(started.elapsed().as_millis()));
    }
}

/// [`stamp_elapsed`] around one of the diagnostic/control verbs, whose calls the
/// dispatch arm does not clock itself.
async fn stamped<F: std::future::Future<Output = OutEnvelope>>(f: F) -> OutEnvelope {
    let started = Instant::now();
    let mut env = f.await;
    stamp_elapsed(&mut env, started);
    env
}

// ── Step runner ──────────────────────────────────────────────────

/// The clocks one CLI step runs to. The two-clock rule itself — which clock is
/// declared to chrome-use, and where the product's own kill rides — is
/// [the chrome policy](crate::chrome)'s; each constructor below states which
/// shape a step of this surface takes. A product-side kill reports the product's
/// own bound for the step — its `kill` — as the envelope's `timeout_ms`.
#[derive(Debug, Clone, Copy)]
struct StepClocks {
    /// The two clocks of this step ([`crate::chrome::ChromeCallClocks`]).
    call: ChromeCallClocks,
    /// Whether chrome-use's own relay self-heal may run inside this step.
    recovery: CliRecovery,
}

impl StepClocks {
    /// Assemble a step's clocks from the declared deadline and the recovery
    /// policy bound ONCE for the step — the derived pair plus that same binding,
    /// so a step's kill and the policy it spawns with can never disagree.
    fn of(declared: Option<Duration>, recovery: CliRecovery) -> Self {
        Self {
            call: clocks(declared, recovery),
            recovery,
        }
    }

    /// `wait`/`expect`: the step forwards its declared deadline as `--timeout`,
    /// which chrome-use honours in full, so the declared value IS the
    /// chrome-side clock; its relay self-heal is allowed.
    fn forwarded(deadline: Duration) -> Self {
        Self::of(Some(deadline), CliRecovery::Allowed)
    }

    /// Every other agent-facing verb (click, count, eval, extract, fill, type,
    /// press): the step forwards no `--timeout` chrome-use
    /// honours, so [`CHROME_USE_DECLARED_BUDGET`] — one [`KILL_SLACK`] under
    /// chrome-use's own client tolerance — is the clock it is given.
    /// `own_bound` is the caller's `--timeout`, which [`parse_bound_timeout`]
    /// accepts only at or above that clock (a shorter one is refused there:
    /// chrome-use takes no per-call deadline for these verbs, so nothing could
    /// honour it). A larger bound is one the PRODUCT must really let the call run
    /// to, so it widens the kill alone: chrome-use cannot work past its own
    /// tolerance, and declaring a larger deadline to it would make it run out of
    /// that tolerance and fail as a wedged session instead of reporting its own
    /// reason (see [the chrome policy](crate::chrome)). The reported `timeout_ms`
    /// is then the bound the call really ran to, never one it was killed below.
    /// Its relay self-heal is allowed.
    fn tool_clock(own_bound: Option<Duration>) -> Self {
        let recovery = CliRecovery::Allowed;
        let bound = own_bound.unwrap_or(CHROME_USE_DECLARED_BUDGET);
        Self {
            call: ChromeCallClocks {
                chrome_side: CHROME_USE_DECLARED_BUDGET,
                kill: kill_bound(bound, recovery),
            },
            recovery,
        }
    }

    /// The composite operation's OWN step (the `open` navigation): chrome-use
    /// runs it to its own client tolerance — it neither reads nor honours a
    /// `--timeout` for `open`'s navigate — so the clock declared to it is
    /// [`CHROME_USE_DECLARED_BUDGET`] and the kill is
    /// [`crate::chrome::kill_bound`] above it; its relay self-heal is allowed.
    /// The operation's budget is deliberately NOT a kill bound here: a navigation
    /// chrome-use was still working on must be allowed to report its own reason,
    /// so the operation may legitimately outlive `--timeout`.
    fn operation() -> Self {
        Self::of(None, CliRecovery::Allowed)
    }

    /// A step of an operation the product bounds itself: `bound` is the
    /// product-side budget for the step, `chrome_side` the clock declared to
    /// chrome-use. It is a probe in everything but name (see the
    /// [chrome policy](crate::chrome)): it declares `chrome_side` honestly to
    /// chrome-use, suppresses the relay self-heal, and its kill rides
    /// [`KILL_SLACK`] above `bound` ([`kill_bound`]) — so a product bound, never
    /// chrome-use's verdict, is what ends it. Its failure never fails the
    /// operation. Every one of the operation's best-effort sub-steps (the
    /// error-page probe, the post-navigation settle, the content capture) takes this
    /// shape.
    fn bounded(chrome_side: Duration, bound: Duration) -> Self {
        let recovery = CliRecovery::Suppressed;
        Self {
            call: ChromeCallClocks {
                chrome_side,
                kill: kill_bound(bound, recovery),
            },
            recovery,
        }
    }

    /// The product's own probe (`session stop`, `session status`, the ephemeral
    /// close): `bound` IS the kill and [`probe_clocks`] declares that same bound to
    /// chrome-use — capped at [`CHROME_USE_DECLARED_BUDGET`] for a probe whose own
    /// bound sits above it, as the `session stop` paths do — and its relay self-heal
    /// is suppressed: the documented exception, where the product reports "the tool
    /// did not answer" instead of spending the recovery window.
    fn probe(bound: Duration) -> Self {
        let recovery = CliRecovery::Suppressed;
        Self {
            call: probe_clocks(bound),
            recovery,
        }
    }
}

/// Run one chrome-use step under `clocks` and classify its output. `session`
/// scopes the call via `--session`; `None` leaves the call session-unscoped
/// (`session stop` names its session via the positional instead). `input` pipes
/// a stdin payload (only `fill --stdin` uses one).
async fn spawn_step(
    path: &Path,
    args: &[&str],
    session: Option<&str>,
    clocks: StepClocks,
    input: Option<&[u8]>,
) -> StepOutcome {
    // A session-scoped spawn attempt may materialize the ephemeral session
    // (a vanished-binary race still sets this; the close attempt is then a
    // harmless best-effort no-op).
    if session.is_some() {
        CHROME_USE_SPAWNED.store(true, Ordering::Relaxed);
    }
    let failed = |kind: OutKind, message: String| Err(StepFailure { kind, message });
    match spawn_cli(CliSpawn {
        path,
        args,
        session,
        json: true,
        capture_stderr: true,
        timeout: CliTimeout::Bounded(clocks.call.kill),
        cancel_kills: true,
        input: input.map(<[u8]>::to_vec),
        chrome_side: clocks.call.chrome_side,
        recovery: clocks.recovery,
    })
    .await
    {
        CliRun::SpawnFailure => failed(
            OutKind::Environment,
            "chrome-use CLI could not be spawned/found".to_string(),
        ),
        CliRun::TimedOut => {
            // The product's own clock ended this step. That is wedge evidence
            // only when the step's outcome IS the caller's verdict
            // ([`CliRecovery::Allowed`]) and its kill rode above the clock
            // chrome-use itself was working to: every step declares that clock
            // to chrome-use (as `AGENT_BROWSER_DEFAULT_TIMEOUT`), so a kill above
            // it means the tool did not answer its own deadline. A step the
            // product bounds itself (probes and the operation's best-effort
            // sub-steps) is [`CliRecovery::Suppressed`] and can never attribute
            // (see [`CLI_STEP_KILLED_BY_CLOCK`]).
            if matches!(clocks.recovery, CliRecovery::Allowed)
                && clocks.call.kill > clocks.call.chrome_side
            {
                CLI_STEP_KILLED_BY_CLOCK.store(true, Ordering::Relaxed);
            }
            Err(StepFailure {
                kind: OutKind::Timeout,
                message: String::new(),
            })
        }
        CliRun::Output(out) => {
            // The child's exit decides the step; a process it left behind holding
            // the output channel is recorded here and reported as a NOTE on the
            // envelope, never as a failure (see [`chrome_leftover_pipe_note`]).
            // OR-ed, not stored: a composite operation runs several steps, and one
            // step's clean finish must not erase an earlier step's leftover.
            CHROME_USE_LEFTOVER_PIPES.fetch_or(out.leftover_pipes, Ordering::Relaxed);
            classify_step_output(
                args.first() == Some(&"expect"),
                out.truncated,
                out.status.code(),
                out.status.success(),
                &out.stdout,
                String::from_utf8_lossy(&out.stderr).trim(),
            )
        }
    }
}

/// Classify one chrome-use output (the `CliRun::Output` payload) into a step
/// outcome. Pure so tests pin the mapping. `expect` is the one chrome-use
/// action whose `--json` envelope can succeed on a non-zero exit — a failed
/// assertion arrives as `success:true` with exit 1, the verdict riding in
/// `data` — so envelope-success is trusted over the exit code there and
/// nowhere else. `truncated` is the product's own flag
/// ([`crate::chrome::spawn::CliOutput::truncated`]): whenever there is no parsed
/// envelope — whatever the exit status — an answer the product cut off names
/// itself rather than reading as chrome-use's malformed output. The parse is the
/// shared tolerant one ([`crate::chrome::contract::parse_first`]), so trailing
/// bytes a leftover process wrote after the envelope are ignored.
fn classify_step_output(
    expect_style: bool,
    truncated: bool,
    exit_code: Option<i32>,
    status_success: bool,
    stdout: &[u8],
    stderr: &str,
) -> StepOutcome {
    let failed = |kind: OutKind, message: String| Err(StepFailure { kind, message });
    // Fallback message for a non-success exit / unparseable stdout.
    let fallback = || fallback_step_message(exit_code, stderr);
    let classified = |resp: ChromeResponse| {
        let code = resp.code.clone();
        let msg = resp
            .error
            .filter(|e| !e.trim().is_empty())
            .unwrap_or_else(fallback);
        let kind = classify_call_failure(code.as_deref(), &msg);
        failed(kind, sanitize_timeout_message(kind, &msg))
    };
    let parsed: Option<ChromeResponse> = parse_first::<ChromeResponse>(stdout);
    if !status_success {
        return match parsed {
            Some(resp) if expect_style && resp.is_success() => {
                self_launched_failure(&resp).map_or(Ok(resp), Err)
            }
            Some(resp) => classified(resp),
            None if truncated => failed(OutKind::Error, truncated_output_error()),
            None => {
                // The stderr fallback can also carry a timeout phrasing plus the
                // canned hint (chrome-use prints diagnostics there when stdout is
                // non-JSON), so it is classified and sanitized too.
                let msg = fallback();
                let kind = classify_call_failure(None, &msg);
                failed(kind, sanitize_timeout_message(kind, &msg))
            }
        };
    }
    match parsed {
        Some(resp) if resp.is_success() => self_launched_failure(&resp).map_or(Ok(resp), Err),
        Some(resp) => classified(resp),
        None => failed(
            OutKind::Error,
            if truncated {
                truncated_output_error()
            } else if stderr.is_empty() {
                "chrome-use returned non-JSON output".to_string()
            } else {
                stderr.to_string()
            },
        ),
    }
}

/// A successful step whose envelope carries chrome-use's browser-replacement
/// note is a plain failure, never a quiet success: the command ran in a browser
/// chrome-use launched ITSELF rather than the owner's real one, so the page state
/// the caller assumed is not there and nothing it read or wrote happened in the
/// owner's session.
///
/// Classified [`OutKind::Environment`] (rc 2 — fix the environment rather than
/// blind-retry), and deliberately NOT the daemon-down path: the daemon and the
/// relay may be healthy, and only the connection to the real browser was lost, so
/// daemon health is left untouched and auto-recovery is not woken.
fn self_launched_failure(resp: &ChromeResponse) -> Option<StepFailure> {
    self_launched_browser_note(resp).map(|note| StepFailure {
        kind: OutKind::Environment,
        message: self_launched_browser_error(&note),
    })
}

#[must_use]
fn fallback_step_message(status_code: Option<i32>, stderr: &str) -> String {
    if stderr.is_empty() {
        match status_code {
            Some(c) => format!("chrome-use exited with code {c}"),
            None => "chrome-use exited with a non-zero status".to_string(),
        }
    } else {
        stderr.to_string()
    }
}

// ── Actions ─────────────────────────────────────────────────────

/// The `out_env` kind contract: a kind emitted for a registered CLI action
/// must be declared in that action's shared-registry kind list, so the help
/// text cannot drift from runtime emission. Unregistered pseudo-actions (the
/// `"usage"` fallback) and non-CLI actions are exempt. Kept a pure predicate
/// so the unit test exercises it in every build profile, not just debug.
fn kind_contract_holds(action: &str, kind: OutKind) -> bool {
    match actions::desc(action) {
        None => true,
        Some(d) => d.cli.as_ref().is_none_or(|c| c.kinds.contains(&kind)),
    }
}

/// Construct an [`OutEnvelope`].
#[must_use]
fn out_env(action: &str, ok: bool, kind: OutKind, payload: Value) -> OutEnvelope {
    debug_assert!(
        kind_contract_holds(action, kind),
        "kind {} not declared for action {action} — update the shared registry",
        kind.as_str()
    );
    OutEnvelope {
        action: action.to_string(),
        ok,
        kind,
        payload,
    }
}

/// Construct an environment-failure envelope from the action params.
#[must_use]
fn env_failure(action: &str, params: Value, error: &str) -> OutEnvelope {
    let mut obj = match params {
        Value::Object(m) => m,
        other => {
            let mut m = serde_json::Map::new();
            m.insert("error".into(), other);
            m
        }
    };
    obj.insert("error".into(), json!(error));
    out_env(action, false, OutKind::Environment, Value::Object(obj))
}

/// Resolve the chrome-use CLI path, or build the environment-failure envelope.
///
/// The standalone `mahbot chrome` CLI deliberately resolves ONLY the product's
/// own copy ([`crate::tools::chrome_daemon::cli_path`], the location the
/// helper's own installer uses) — never a `chrome-use` found on the owner's
/// search path or in a home location, so a foreign or leftover copy is never
/// run. A host whose product-owned install has not happened yet therefore
/// reports `chrome-use CLI not found` even when some other `chrome-use` is on
/// PATH.
fn require_cli(action: &str, params: Value) -> Result<std::path::PathBuf, OutEnvelope> {
    cli_path().ok_or_else(|| env_failure(action, params, "chrome-use CLI not found"))
}

/// `status` — pure preflight: the readiness report and RAW probes only (never
/// Chrome auto-launch, no environment mutation, no recovery — only the on-demand
/// verbs and the pre-action gate recover).
///
/// It reports what WAS established about the owner's real browser
/// ([`crate::tools::chrome_daemon::Readiness::report`]) rather than a list of raw
/// probes, and states the machine-readable `verdict` it amounts to: `ready`
/// (rc 0), `blocked` (rc 2 — a fact established the connection cannot work), or
/// `not-proven` (rc 0 — nothing could be established either way, and the
/// pre-action gate would still dispatch an action). A CLI that cannot state its
/// version is rc 2 as well. The `chrome_use`, `relay_up`, `chrome_running` and
/// `display` keys stay for scripts that read them — the last three as the raw
/// tri-state facts of the same snapshot the report renders.
async fn status() -> OutEnvelope {
    // One `--version` spawn in the happy path: a parsed version proves the
    // CLI is available; `cli_probe` only re-runs to classify WHY it didn't.
    let (chrome_use, version_ok) = match cli_version().await {
        Some(v) => (v.to_string(), true),
        None => match cli_path() {
            None => ("missing".to_string(), false),
            Some(_) => match cli_probe().await {
                CliStatus::Available => ("present".to_string(), true),
                CliStatus::Missing => ("missing".to_string(), false),
                CliStatus::Transient(f) => (f.to_string(), false),
            },
        },
    };
    // One bounded, daemon-free readiness snapshot (cached for HEALTH_TTL), every
    // fact tri-state — including the display fact, taken from the snapshot
    // instead of re-probed here.
    let readiness = readiness().await;

    let mut payload = serde_json::Map::new();
    payload.insert("chrome_use".into(), json!(chrome_use));
    payload.insert("relay_up".into(), json!(readiness.relay_up));
    payload.insert("chrome_running".into(), json!(readiness.chrome_running));
    payload.insert("display".into(), json!(readiness.display));
    payload.insert(
        "ready_for_actions".into(),
        json!(readiness.ready_for_actions()),
    );
    // The three-way state in one word, so a machine read does not have to infer
    // it from the report text: `ready` is PROVEN, `blocked` means a fact
    // established the connection cannot work, and `not-proven` means nothing was
    // established either way (the gate still dispatches an action there).
    payload.insert(
        "verdict".into(),
        json!(if readiness.ready_for_actions() {
            "ready"
        } else if readiness.blocked() {
            "blocked"
        } else {
            "not-proven"
        }),
    );
    payload.insert("readiness".into(), json!(readiness.report()));

    if readiness.ready_for_actions() {
        return out_env("status", true, OutKind::Ok, Value::Object(payload));
    }
    // Three states, mirroring the pre-action gate. A CLI that cannot state its
    // version, and a fact that RULES THE CONNECTION OUT, are both environment
    // failures. A snapshot that established nothing wrong — not proven, nothing
    // ruled out — is a reporting gap: the gate would still dispatch an action,
    // so the report's own verdict is the answer and this is not reported as a
    // broken environment (the readiness snapshot cannot name an absent/broken
    // CLI, which is why the version probe's verdict leads there).
    let error = if !version_ok {
        Some(format!(
            "chrome-use CLI: {chrome_use}. {}",
            readiness.refusal()
        ))
    } else if readiness.blocked() {
        Some(readiness.refusal())
    } else {
        None
    };
    let Some(error) = error else {
        return out_env("status", true, OutKind::Ok, Value::Object(payload));
    };
    eprintln!("mahbot chrome: {error}");
    payload.insert("error".into(), json!(error));
    out_env(
        "status",
        false,
        OutKind::Environment,
        Value::Object(payload),
    )
}

/// The honest `error` text for a committed-Chrome-error-page network failure:
/// the specific cause when the net error code was extracted, the generic
/// fallback otherwise (never lose the honest generic reason).
fn network_failure_error(code: Option<&str>) -> String {
    match code {
        Some(c) => {
            format!(
                "Chrome rendered an error page — {c} ({})",
                net_error_phrase(c)
            )
        }
        None => "Chrome rendered an error page (site unreachable or refused)".to_string(),
    }
}

/// The factual `error` text for a product-side deadline kill; `what` names the
/// step that did not complete. `chrome_side` is the clock that step declared to
/// chrome-use and `kill` the product's own bound that ended it.
///
/// It says plainly that the PRODUCT's own bound ended the call — never that
/// chrome-use timed out. When the kill rode above the declared clock (a step
/// whose outcome is the caller's verdict) it says so, so a truncated call is
/// never mistaken for chrome-use's own verdict; otherwise (a probe, or the
/// operation's own budget ending one of its best-effort sub-steps) it says the
/// bound was the product's own.
fn deadline_error(chrome_side: Duration, kill: Duration, what: &str) -> String {
    let kill = kill.as_millis();
    if kill > chrome_side.as_millis() {
        format!(
            "deadline reached after {kill}ms — {what}: that is the product's own bound, which \
             rode above the {}ms deadline chrome-use itself was working to, so chrome-use never \
             reported its own reason",
            chrome_side.as_millis()
        )
    } else {
        // The product bounded this step itself: its own probe bound, or the
        // operation's own budget ending one of its best-effort sub-steps.
        format!(
            "deadline reached after {kill}ms — {what}: that is the product's own bound for the \
             step (chrome-use was given {}ms), so a product-side bound — not chrome-use's \
             verdict — ended the call",
            chrome_side.as_millis()
        )
    }
}

/// Post-navigation settle cap for `open`'s plain path: a best-effort
/// `wait --load networkidle` (what the interactive tool does after its open)
/// so the first step after `open` on heavy SPAs is not racing a still-settling
/// page under daemon contention.
const SETTLE_CAP: Duration = Duration::from_secs(10);

/// Budget reserved for content capture so the settle can never consume the
/// whole remaining operation budget and silently drop the open's snapshot.
const CAPTURE_RESERVE: Duration = Duration::from_millis(2500);

/// Below this a settle spawn is not worth its startup cost — skipped.
const MIN_SETTLE_BUDGET: Duration = Duration::from_secs(1);

/// Settle budget for `open`'s plain path: capped at [`SETTLE_CAP`], must leave
/// [`CAPTURE_RESERVE`] for content capture, skipped (None) when the remainder
/// is too small for both. Pure so tests pin the policy.
fn settle_budget(remaining: Duration) -> Option<Duration> {
    let budget = remaining.saturating_sub(CAPTURE_RESERVE).min(SETTLE_CAP);
    (budget >= MIN_SETTLE_BUDGET).then_some(budget)
}

/// The pure argument checks of `open`, run both by [`dispatch`] (before the
/// readiness gate) and by [`open`] itself. The URL must be one this product may
/// navigate to, and the `--expect` selector goes through the shared wait-target
/// policy (so the numeric silent-sleep form is rejected here too) — both checked
/// before navigating, never after. The resolved wait target is the check's own
/// by-product.
fn open_args(url: &str, expect: Option<&str>) -> Result<Option<WaitTarget>, OutEnvelope> {
    if let Err(e) = validate_url(url) {
        return Err(out_env(
            "open",
            false,
            OutKind::Usage,
            json!({ "url": url, "error": e.to_string() }),
        ));
    }
    expect
        .map(|sel| wait_target(Some(sel), None, None))
        .transpose()
        .map_err(|e| {
            out_env(
                "open",
                false,
                OutKind::Usage,
                json!({ "url": url, "error": e }),
            )
        })
}

/// `open` — navigate to `url`, optionally wait for `--expect` (redesign-aware
/// via `--structural`), report the committed final URL and best-effort attach
/// the page content (compact accessibility snapshot). On the plain path (no
/// `--expect`, which already serves as the settle) the navigation is followed
/// by a best-effort network settle, capped at [`SETTLE_CAP`]. `timeout` bounds
/// the waits the operation drives — the error-page probe, the settle /
/// `--expect` wait, and the content capture — NOT the navigation, which runs to
/// the clock mahbot declares to chrome-use: the operation may therefore
/// legitimately outlive `timeout`.
#[expect(clippy::too_many_lines)]
async fn open(
    url: &str,
    expect: Option<&str>,
    structural: bool,
    timeout: Duration,
    session: &str,
) -> OutEnvelope {
    let wait_for = match open_args(url, expect) {
        Ok(wait_for) => wait_for,
        Err(refusal) => return refusal,
    };
    let path = match require_cli("open", json!({ "url": url })) {
        Ok(p) => p,
        Err(e) => return e,
    };

    let started = Instant::now();
    let total = timeout + KILL_SLACK;
    // The operation's own step: neither `timeout` (the budget `--timeout`
    // declared, the CLI's whole-operation default otherwise) nor `total` bounds
    // the NAVIGATION. `--timeout` is no deadline chrome-use honours for `open`'s
    // navigate: chrome-use runs it to the clock mahbot declares to it, and killing it
    // on the operation's budget would end a call chrome-use was still working on
    // — losing chrome-use's own reason for the failure, which is exactly what the
    // product's kill must never do. The navigation's kill therefore rides
    // `kill_bound` above chrome-use's own budget, and the operation may
    // legitimately outlive `--timeout`. `total` is then the operation's own
    // bookkeeping — `timeout` plus the slack reserved for the error-page probe —
    // for that probe, the settle and the content capture; the `--expect` wait
    // declares what remains of `timeout` instead (see below), and every other step
    // of the operation rides [`StepClocks::bounded`].
    let operation = StepClocks::operation();
    let resp = match spawn_step(&path, &["open", url], Some(session), operation, None).await {
        Ok(resp) => resp,
        Err(f) => return f.envelope("open", json!({ "url": url }), operation),
    };
    let final_url = resp
        .data
        .as_ref()
        .and_then(|d| d.get("url"))
        .and_then(Value::as_str)
        .unwrap_or(url)
        .to_string();
    if is_blank_page_url(&final_url) {
        return out_env(
            "open",
            false,
            OutKind::Network,
            json!({
                "url": url,
                "error": "navigation never committed — tab still on about:blank"
            }),
        );
    }
    // One of the operation's best-effort sub-steps (the policy exception in
    // [`crate::chrome`]): the product reserves it up to the 2 s slack so it runs
    // even when the navigation consumed the whole declared deadline, and its kill
    // rides that slack above the reserved bound. Best-effort (inconclusive =
    // pass); skipped when nothing is left. One eval yields both the error-page
    // verdict and the net error token Chrome renders in `div.error-code`
    // (empty until the neterror script runs — the generic message covers it).
    let probe_budget = total.saturating_sub(started.elapsed()).min(KILL_SLACK);
    if probe_budget >= Duration::from_millis(500)
        && let Ok(probe) = spawn_step(
            &path,
            &["eval", ERROR_PAGE_PROBE_JS],
            Some(session),
            StepClocks::bounded(CHROME_USE_DECLARED_BUDGET, probe_budget),
            None,
        )
        .await
        && let Some(ErrorPageProbe {
            is_error_page: true,
            code,
        }) = parse_error_page_probe(&probe)
    {
        let mut payload = json!({
            "url": url,
            "error": network_failure_error(code.as_deref()),
        });
        if let Some(code) = code {
            payload["error_code"] = json!(code);
        }
        return out_env("open", false, OutKind::Network, payload);
    }
    if let Some(target) = wait_for {
        let target_desc = target.describe();
        let mut payload = json!({ "url": final_url, "target": target_desc });
        // A missed `--expect` after a committed navigation is a suspected
        // redesign when `--structural` was declared, a plain timeout otherwise.
        let (timeout_kind, hint) = if structural {
            (
                OutKind::Redesign,
                "possible DOM redesign or structural change",
            )
        } else {
            (
                OutKind::Timeout,
                "selector not found — may be structural change, empty region, or content-dependent",
            )
        };
        // The wait's declared deadline is what REMAINS of the caller's own
        // --timeout — never the operation's bookkeeping `total`, whose extra
        // slack belongs to the error-page probe. A declaration that crosses
        // chrome-use's own client tolerance is what makes chrome-use run out of
        // tolerance instead of answering, and `parse_forwarded_timeout` already
        // refuses a --timeout at or above it, so the remaining part of one stays
        // below it.
        let wait_budget = timeout.saturating_sub(started.elapsed());
        // No budget left for the wait (or its capture) — emit the same timeout
        // envelope the wait-timeout arm produces, without content. What ran out
        // here is the OPERATION's own budget, spent before the wait step was given
        // any of it, so no deadline was ever declared to chrome-use for that step:
        // the envelope reports the operation's own budget, and says so rather than
        // naming a chrome-use clock that was never given a value.
        if wait_budget < Duration::from_millis(250) {
            payload["timeout_ms"] = json!(timeout.as_millis());
            payload["error"] = json!(format!(
                "no budget left for the `--expect` wait: the operation's own --timeout budget \
                 ({}ms) was spent by the navigation and the steps around it before the wait \
                 could be given any of it, so chrome-use was never given a deadline for that \
                 step — this is the product's own bound on `open`, not chrome-use's verdict. \
                 Raise --timeout or retry now that the page is open.",
                timeout.as_millis()
            ));
            payload["hint"] = json!(hint);
            return out_env("open", false, timeout_kind, payload);
        }
        // The wait forwards its remaining budget as --timeout, which chrome-use
        // honours in full — so that remaining budget IS the step's chrome-side
        // clock — and its failure IS the caller's verdict, so it runs to the
        // forwarded clocks with the relay self-heal allowed: a relay drop during
        // the wait is healed exactly the way it is for any reported step.
        let wait_clocks = StepClocks::forwarded(wait_budget);
        let wargs = wait_args(&target, wait_budget.as_millis());
        let refs: Vec<&str> = wargs.iter().map(String::as_str).collect();
        let waited = spawn_step(&path, &refs, Some(session), wait_clocks, None).await;
        // The page is open regardless of the wait outcome, so the content
        // rides on failure envelopes too: it is exactly what diagnoses a
        // redesign. Same remaining-budget rule as the plain path — slow
        // waits simply exhaust it and the content is omitted.
        let content =
            capture_open_content(&path, session, total.saturating_sub(started.elapsed())).await;
        if let Some(content) = content {
            payload["content"] = json!(content);
        }
        return match waited {
            Ok(_) => out_env("open", true, OutKind::Ok, payload),
            Err(f) if f.kind == OutKind::Timeout => {
                // chrome-use's own verdict reports the deadline it was given; a
                // product kill reports the product's own bound (the kill).
                let reported = if f.message.is_empty() {
                    wait_clocks.call.kill
                } else {
                    wait_clocks.call.chrome_side
                };
                payload["timeout_ms"] = json!(reported.as_millis());
                // The chrome-side timeout message when chrome-use phrased the
                // timeout itself, a factual deadline text on a product kill.
                if f.message.is_empty() {
                    payload["error"] = json!(deadline_error(
                        wait_clocks.call.chrome_side,
                        wait_clocks.call.kill,
                        "the target never appeared"
                    ));
                } else {
                    payload["error"] = json!(f.message);
                }
                payload["hint"] = json!(hint);
                out_env("open", false, timeout_kind, payload)
            }
            Err(f) => f.envelope("open", payload, wait_clocks),
        };
    }
    // Best-effort settle: heavy SPAs keep the network busy right after the
    // navigation commits, so the first following step can otherwise start
    // racing a still-settling page. Raw argv — the `wait` action deliberately
    // does not expose `--load`. chrome-use honours no `--timeout` for this form:
    // its clock is the AGENT_BROWSER_DEFAULT_TIMEOUT this step declares (the
    // remaining operation budget), and the product's own bound on the step is the
    // same number — one of the operation's best-effort sub-steps, so its kill
    // rides [`KILL_SLACK`] above that bound and the operation still stays within
    // its budget plus that slack. The result is discarded — a settle timeout
    // never downgrades a committed navigation.
    if let Some(budget) = settle_budget(total.saturating_sub(started.elapsed())) {
        let _ = spawn_step(
            &path,
            &["wait", "--load", "networkidle"],
            Some(session),
            StepClocks::bounded(budget, budget),
            None,
        )
        .await;
    }
    // Content capture is best-effort and bounded: it runs on the budget the
    // navigation (+ error probe + settle) left over, and a failure, exhaustion,
    // or content-free page simply omits `content` — a successful navigation is
    // never downgraded. The step declares the product's clock for a verb it
    // forwards no `--timeout` to ([`CHROME_USE_DECLARED_BUDGET`]) while the
    // product's own bound on it is the remaining operation budget — the
    // operation's last best-effort sub-step, so its kill rides [`KILL_SLACK`]
    // above that bound.
    let mut payload = json!({ "url": final_url });
    let budget = total.saturating_sub(started.elapsed());
    if let Some(content) = capture_open_content(&path, session, budget).await {
        payload["content"] = json!(content);
    }
    out_env("open", true, OutKind::Ok, payload)
}

/// Best-effort compact page snapshot for the `open` envelope — the same
/// content form the interactive chrome tool surfaces after an open. One of the
/// operation's bounded sub-steps ([`StepClocks::bounded`]): skipped
/// under 500 ms of budget (mirroring the error-page probe); a failed step,
/// an unrecognized response shape, or a content-free page yields no content.
/// Non-empty content is byte-capped so the single-line JSON envelope stays
/// valid and within the tool-output budget.
async fn capture_open_content(path: &Path, session: &str, budget: Duration) -> Option<String> {
    if budget < Duration::from_millis(500) {
        return None;
    }
    let resp = spawn_step(
        path,
        &["snapshot", "-c"],
        Some(session),
        StepClocks::bounded(CHROME_USE_DECLARED_BUDGET, budget),
        None,
    )
    .await
    .ok()?;
    let text = resp.data.as_ref().and_then(extract_snapshot_text)?;
    let text = text.trim();
    if text.is_empty() {
        return None;
    }
    Some(truncate_sandwich(
        text,
        TOOL_OUTPUT_BUDGET_BYTES,
        "page content",
    ))
}

/// Run the count-eval shim for `selector` — shared by the `count` action and
/// the `extract` honest-empty gate. The eval shim is a step of the action that
/// called it, so it runs to that action's clocks.
async fn count_via_eval(
    path: &Path,
    selector: &str,
    session: &str,
    clocks: StepClocks,
) -> Result<u64, StepFailure> {
    let js = count_eval_js(selector);
    let args = ["eval".to_string(), js];
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    let resp = spawn_step(path, &refs, Some(session), clocks, None).await?;
    eval_count(&resp).ok_or(StepFailure {
        kind: OutKind::Error,
        message: "count eval returned a non-numeric result".to_string(),
    })
}

/// `count` — eval shim over `querySelectorAll` (chrome-use has no `count` verb).
async fn count(selector: &str, bound: Option<Duration>, session: &str) -> OutEnvelope {
    let path = match require_cli("count", json!({ "selector": selector })) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let clocks = StepClocks::tool_clock(bound);
    match count_via_eval(&path, selector, session, clocks).await {
        Ok(n) => {
            let kind = if n == 0 { OutKind::Empty } else { OutKind::Ok };
            out_env(
                "count",
                true,
                kind,
                json!({ "selector": selector, "count": n }),
            )
        }
        Err(f) => f.envelope("count", json!({ "selector": selector }), clocks),
    }
}

/// `wait` — bounded wait for a safe [`WaitTarget`] (the numeric sleep form is
/// rejected at parse time). The requested `--timeout` IS chrome-use's deadline
/// (its wait forms honour it in full), so its honest timeout error surfaces at
/// the declared clock; the product's kill rides the relay-recovery window +
/// [`crate::chrome::KILL_SLACK`] above it (see [`StepClocks::forwarded`]).
async fn wait(target: &WaitTarget, timeout: Duration, session: &str) -> OutEnvelope {
    let base = json!({ "target": target.describe() });
    let path = match require_cli("wait", base.clone()) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let clocks = StepClocks::forwarded(timeout);
    let args = wait_args(target, timeout.as_millis());
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(_) => out_env(
            "wait",
            true,
            OutKind::Ok,
            json!({ "target": target.describe() }),
        ),
        Err(mut f) => {
            with_condition_timeout_note("wait", f.kind, &mut f.message);
            f.envelope("wait", base, clocks)
        }
    }
}

/// Map a parsed expect verdict to the envelope: pass → rc 0; deadline
/// expiration → kind timeout; a plain false (only reachable if a future
/// chrome-use stops folding it into timedOut) → kind error. Pure so tests
/// pin the mapping.
fn expect_envelope(condition: &str, outcome: ExpectOutcome) -> OutEnvelope {
    let mut payload = json!({ "condition": condition });
    if let Some(actual) = outcome.actual {
        payload["actual"] = actual;
    }
    if outcome.pass {
        payload["pass"] = json!(true);
        return out_env("expect", true, OutKind::Ok, payload);
    }
    payload["pass"] = json!(false);
    let kind = if outcome.timed_out {
        payload["timed_out"] = json!(true);
        payload["error"] = json!(format!(
            "condition was not met within the deadline{EXPECT_TIMEOUT_NOTE}"
        ));
        OutKind::Timeout
    } else {
        payload["error"] = json!("condition is false");
        OutKind::Error
    };
    out_env("expect", false, kind, payload)
}

/// `expect` — assert a condition with a bounded wait. The envelope (not the
/// exit code) is authoritative: a failed assertion arrives as
/// `success:true` with the verdict in `data`, which [`expect_outcome`]
/// parses (see [`spawn_step`]).
async fn expect(cond: &ExpectCond, timeout: Duration, session: &str) -> OutEnvelope {
    let condition = describe(cond);
    let base = json!({ "condition": condition });
    let path = match require_cli("expect", base.clone()) {
        Ok(p) => p,
        Err(e) => return e,
    };
    // Same rule as wait: the forwarded `--timeout` is chrome-use's own clock,
    // and the product's kill rides the relay-recovery window + slack above it.
    let clocks = StepClocks::forwarded(timeout);
    let args = expect_args(cond, timeout.as_millis());
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(resp) => match resp.data.as_ref().and_then(expect_outcome) {
            Some(outcome) => expect_envelope(&condition, outcome),
            None => out_env(
                "expect",
                false,
                OutKind::Error,
                json!({ "condition": condition, "error": "expect returned an unrecognized payload" }),
            ),
        },
        Err(mut f) => {
            with_condition_timeout_note("expect", f.kind, &mut f.message);
            f.envelope("expect", base, clocks)
        }
    }
}

/// `eval` — run JS and emit the unwrapped result as a JSON value (number,
/// string, object, or null).
async fn eval(js: &str, bound: Option<Duration>, session: &str) -> OutEnvelope {
    let path = match require_cli("eval", json!({ "js": js })) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let clocks = StepClocks::tool_clock(bound);
    match spawn_step(&path, &["eval", js], Some(session), clocks, None).await {
        Ok(resp) => {
            let result = eval_result(&resp).cloned().unwrap_or(Value::Null);
            out_env("eval", true, OutKind::Ok, json!({ "result": result }))
        }
        Err(f) => f.envelope("eval", json!({ "js": js }), clocks),
    }
}

/// `extract` — schema-driven rows extraction, gated by a count so an empty
/// region is reported honestly (chrome-use's rows-mode `extract` returns
/// phantom rows when the rows selector matches 0).
async fn extract(
    schema_file: &str,
    limit: Option<usize>,
    bound: Option<Duration>,
    session: &str,
) -> OutEnvelope {
    let schema = match std::fs::read_to_string(schema_file) {
        Ok(s) => match serde_json::from_str::<Value>(&s) {
            Ok(v) => v,
            Err(e) => {
                return out_env(
                    "extract",
                    false,
                    OutKind::Usage,
                    json!({ "error": format!("schema-file is not valid JSON: {e}") }),
                );
            }
        },
        Err(e) => {
            return out_env(
                "extract",
                false,
                OutKind::Usage,
                json!({ "error": format!("cannot read --schema-file: {e}") }),
            );
        }
    };

    if let Err(err) = validate_extract_getters(&schema) {
        return out_env("extract", false, OutKind::Usage, json!({ "error": err }));
    }

    let path = match require_cli("extract", json!({})) {
        Ok(p) => p,
        Err(e) => return e,
    };

    let clocks = StepClocks::tool_clock(bound);
    // Honest-empty gate: when the rows selector matches 0, report empty without
    // invoking chrome-use's phantom-row `extract`.
    if let Some(rows_sel) = schema.get("rows").and_then(Value::as_str) {
        let count = count_via_eval(&path, rows_sel, session, clocks).await;
        match extract_gate(count) {
            ExtractGate::Empty => {
                return out_env(
                    "extract",
                    true,
                    OutKind::Empty,
                    extract_output(&json!([]), limit),
                );
            }
            ExtractGate::Proceed => {}
            ExtractGate::Fail(f) => return f.envelope("extract", json!({}), clocks),
        }
    }

    // `--schema-file` is the installed chrome-use 1.5.101's own documented
    // form (`extract --help`); rows-mode extract answers
    // `data = {extracted: [...], count, origin, meta}` and single-object mode
    // with the bare object; extract_output tolerates a plain array too.
    let args = [
        "extract".to_string(),
        "--schema-file".to_string(),
        schema_file.to_string(),
    ];
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(resp) => out_env(
            "extract",
            true,
            OutKind::Ok,
            extract_output(resp.data.as_ref().unwrap_or(&Value::Null), limit),
        ),
        Err(f) => f.envelope("extract", json!({}), clocks),
    }
}

/// `click` — click a selector, forwarding `--if-present` verbatim when set
/// (chrome-use itself treats an `--if-present` miss as a no-op success).
async fn click(
    selector: &str,
    if_present: bool,
    bound: Option<Duration>,
    session: &str,
) -> OutEnvelope {
    let path = match require_cli("click", json!({ "selector": selector })) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let mut args = vec!["click".to_string(), selector.to_string()];
    if if_present {
        args.push("--if-present".to_string());
    }
    let refs: Vec<&str> = args.iter().map(String::as_str).collect();
    let clocks = StepClocks::tool_clock(bound);
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(_) => out_env("click", true, OutKind::Ok, json!({ "selector": selector })),
        Err(f) => f.envelope("click", json!({ "selector": selector }), clocks),
    }
}

/// Read this process's stdin to EOF for `fill --stdin`, capped so an
/// oversized/accidental stream is a usage error, not a memory event.
const STDIN_TEXT_CAP: usize = 10 * 1024 * 1024;
async fn read_stdin_capped() -> Result<Vec<u8>, String> {
    use tokio::io::AsyncReadExt;
    let mut buf = Vec::new();
    let mut stdin = tokio::io::stdin().take((STDIN_TEXT_CAP + 1) as u64);
    stdin
        .read_to_end(&mut buf)
        .await
        .map_err(|e| format!("cannot read stdin: {e}"))?;
    if buf.len() > STDIN_TEXT_CAP {
        return Err(format!(
            "stdin text exceeds the {STDIN_TEXT_CAP} byte cap — write it to a file and use --file"
        ));
    }
    if buf.is_empty() {
        return Err(
            "no text on stdin — pipe it, e.g. `cat post.md | mahbot chrome fill \".editor\" --stdin`"
                .to_string(),
        );
    }
    Ok(buf)
}

/// The fill/type/press success handling, pure for tests: a success envelope
/// carrying chrome-use's degraded-success `warning` ([`chrome_use_warning`]) is
/// classified as kind error (rc 1) with the warning surfaced — the action may not
/// have taken effect, so it must never exit 0. A clean success emits kind ok with
/// the action params plus chrome-use's `data`.
///
/// The browser-replacement note is never one of these: [`classify_step_output`]
/// already turned such an envelope into an Environment failure before it could
/// reach here (see [`self_launched_browser_error`]).
fn text_input_ok_envelope(action: &str, base: &Value, resp: ChromeResponse) -> OutEnvelope {
    let warning = chrome_use_warning(&resp);
    let mut payload = base.as_object().cloned().unwrap_or_default();
    if let Some(d) = resp.data {
        payload.insert("data".into(), d);
    }
    if let Some(warning) = warning {
        payload.insert("warning".into(), warning);
        return out_env(action, false, OutKind::Error, Value::Object(payload));
    }
    out_env(action, true, OutKind::Ok, Value::Object(payload))
}

/// `fill` — clear + verified fill. Exactly one text source (inline text,
/// --file passthrough, or mahbot-piped --stdin).
async fn fill(
    selector: &str,
    source: &TextInput,
    bound: Option<Duration>,
    session: &str,
) -> OutEnvelope {
    let action = "fill";
    let base = json!({ "selector": selector });
    let path = match require_cli(action, base.clone()) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let mut argv = vec!["fill".to_string(), selector.to_string()];
    argv.extend(source.argv());
    let input = match source {
        TextInput::Stdin => match read_stdin_capped().await {
            Ok(bytes) => Some(bytes),
            Err(e) => {
                return out_env(
                    action,
                    false,
                    OutKind::Usage,
                    json!({ "selector": selector, "error": e }),
                );
            }
        },
        TextInput::File(f) => {
            if !std::path::Path::new(f).is_file() {
                return out_env(
                    action,
                    false,
                    OutKind::Usage,
                    json!({ "selector": selector, "error": format!("--file does not exist: {f}") }),
                );
            }
            None
        }
        TextInput::Inline(_) => None,
    };
    let refs: Vec<&str> = argv.iter().map(String::as_str).collect();
    let clocks = StepClocks::tool_clock(bound);
    match spawn_step(&path, &refs, Some(session), clocks, input.as_deref()).await {
        Ok(resp) => text_input_ok_envelope(action, &base, resp),
        Err(f) => f.envelope(action, base, clocks),
    }
}

/// `type` — character-by-character typing (appends, does not clear).
async fn r#type(
    selector: &str,
    text: &str,
    key_events: bool,
    bound: Option<Duration>,
    session: &str,
) -> OutEnvelope {
    let action = "type";
    let base = json!({ "selector": selector, "text": text });
    let path = match require_cli(action, base.clone()) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let mut argv = vec!["type".to_string(), selector.to_string()];
    if key_events {
        argv.push("--key-events".to_string());
    }
    // Leading-dash text rides after the `--` shield; action flags must come
    // BEFORE it (everything after is a verbatim value).
    argv.extend(text_value_argv(text));
    let refs: Vec<&str> = argv.iter().map(String::as_str).collect();
    let clocks = StepClocks::tool_clock(bound);
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(resp) => text_input_ok_envelope(action, &base, resp),
        Err(f) => f.envelope(action, base, clocks),
    }
}

/// `press` — press a key at the focused element, optionally trying to focus a
/// `--selector` first (a no-op for non-focusable elements) and holding `--hold`
/// ms before release.
async fn press(
    key: &str,
    selector: Option<&str>,
    hold: Option<u64>,
    bound: Option<Duration>,
    session: &str,
) -> OutEnvelope {
    let action = "press";
    let mut base_obj = serde_json::Map::new();
    base_obj.insert("key".into(), json!(key));
    if let Some(sel) = selector {
        base_obj.insert("selector".into(), json!(sel));
    }
    if let Some(h) = hold {
        base_obj.insert("hold_ms".into(), json!(h));
    }
    let base = Value::Object(base_obj);
    let path = match require_cli(action, base.clone()) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let mut argv = vec!["press".to_string(), key.to_string()];
    if let Some(sel) = selector {
        argv.extend(["--selector".to_string(), sel.to_string()]);
    }
    if let Some(h) = hold {
        argv.extend(["--hold".to_string(), h.to_string()]);
    }
    let refs: Vec<&str> = argv.iter().map(String::as_str).collect();
    let clocks = StepClocks::tool_clock(bound);
    match spawn_step(&path, &refs, Some(session), clocks, None).await {
        Ok(resp) => text_input_ok_envelope(action, &base, resp),
        Err(f) => f.envelope(action, base, clocks),
    }
}

/// `session stop` — name-gated stop of a CLI session (protects the interactive
/// tool's `agent-tab-*` sessions unless `--force`).
async fn session_stop(name: &str, force: bool) -> OutEnvelope {
    let target = match resolve_stop_target(name, force) {
        Ok(t) => t,
        Err(error) => {
            eprintln!("mahbot chrome: {error}");
            return out_env(
                "session",
                false,
                OutKind::Usage,
                json!({ "session": name, "error": error }),
            );
        }
    };
    let path = match require_cli("session", json!({ "session": target })) {
        Ok(p) => p,
        Err(e) => return e,
    };
    // A product probe (the probe exception in [`StepClocks::probe`], see the
    // policy in [`crate::chrome`]), not agent work: bounded by the product itself
    // with chrome-use's relay self-heal suppressed. The bound is the shared
    // [`SESSION_STOP_TIMEOUT`] — a product bound, not a declared deadline
    // (chrome-use forwards no `--timeout` to `session stop`).
    let clocks = StepClocks::probe(SESSION_STOP_TIMEOUT);
    match spawn_step(&path, &["session", "stop", &target], None, clocks, None).await {
        Ok(_) => out_env("session", true, OutKind::Ok, json!({ "session": target })),
        Err(f) => f.envelope("session", json!({ "session": target }), clocks),
    }
}

/// `session status <name>` — opt-in liveness probe for a named session. A
/// daemon-free `session list` preflight (never creates a session or spawns a
/// daemon) reports a stopped session as `empty` (rc 0); a listed session is
/// probed with a real bounded `get url`. Any timeout on that probe is wedge
/// evidence (the probe reads the current URL, it never navigates), so it
/// re-classifies as Environment (rc 2), says plainly that the session stopped
/// answering, and names the recovery. This verb is a diagnostic and stops
/// nothing; the paths that ACT on the browser — the action verbs, the watchdog
/// and the ended-run release — recover such a session themselves
/// ([`recover_unresponsive_session`]), so its text also says the next action
/// does it automatically. An honest non-wedge cause passes through unnamed.
async fn session_status(name: &str) -> OutEnvelope {
    let target = resolve_session_target(name);
    let path = match require_cli("session", json!({ "session": target })) {
        Ok(p) => p,
        Err(e) => return e,
    };
    // Both steps are the product's own probes — the probe exception: bounded by
    // the product, with chrome-use's relay self-heal suppressed
    // ([`StepClocks::probe`]).
    let enumerate = StepClocks::probe(SESSION_LIST_TIMEOUT);
    let list = match spawn_step(&path, &["session", "list"], None, enumerate, None).await {
        Ok(resp) => resp,
        Err(f) => {
            return f.envelope(
                "session",
                json!({ "session": target, "stage": "enumerate" }),
                enumerate,
            );
        }
    };
    if !session_listed(&list, &target) {
        return out_env(
            "session",
            true,
            OutKind::Empty,
            json!({ "session": target, "detail": "session is not running — nothing to probe" }),
        );
    }
    let probe = StepClocks::probe(SESSION_PROBE_TIMEOUT);
    match spawn_step(&path, &["get", "url"], Some(&target), probe, None).await {
        Ok(resp) => {
            let url = resp
                .data
                .as_ref()
                .and_then(|d| d.get("url"))
                .and_then(Value::as_str)
                .unwrap_or("?");
            out_env(
                "session",
                true,
                OutKind::Ok,
                json!({ "session": target, "status": "usable", "url": url }),
            )
        }
        Err(mut f) => {
            // Any timeout on the probe is wedge evidence (it reads the
            // current URL, it never navigates) — re-classify as Environment
            // with chrome-use's own signature phrasing, exactly like its
            // session-unresponsive classification. The probe deliberately does
            // NOT auto-recover (it is the diagnostic an owner runs to see the
            // state), so its text names the manual flow instead — the one place
            // the automatic recovery never ran. Other failures pass through with
            // their honest cause (not every failure is a wedge).
            if f.kind == OutKind::Timeout {
                f = StepFailure {
                    kind: OutKind::Environment,
                    message: format!(
                        "session unresponsive: the session stopped answering — no answer to the \
                         liveness probe (get url) within {}s. This diagnostic stops nothing; \
                         recover it with {SESSION_RECOVERY_FLOW}, or let the next action verb \
                         recover it automatically.",
                        SESSION_PROBE_TIMEOUT.as_secs()
                    ),
                };
            }
            // A protected-namespace wedge needs `--force` to stop — make the
            // recovery flow directly actionable for that edge. Only wedges
            // get the note (an honest non-wedge cause must not carry stop
            // guidance), and only protected ones (an ordinary session's stop
            // needs no --force).
            append_protected_stop_note(&mut f.message, &target);
            f.envelope("session", json!({ "session": target }), probe)
        }
    }
}

/// Append the protected-namespace `--force` note to a probe-failure message
/// that reads as a session wedge on a protected (`agent-tab-*` /
/// `link-enricher-*`) target: the recovery flow names
/// `session stop <name>`, which is only actionable with `--force` here. Pure
/// so tests pin both gates (protected membership, wedge-shaped message).
fn append_protected_stop_note(message: &mut String, target: &str) {
    if PROTECTED_SESSION_PREFIXES
        .iter()
        .any(|p| target.starts_with(p))
        && is_session_unresponsive_error(message)
    {
        write!(
            message,
            " (protected namespace: stopping '{target}' requires `session stop {target} --force`)"
        )
        .expect("writing to a String cannot fail");
    }
}

/// Tolerance-first membership check over a `session list` envelope: only an
/// explicit failure verdict rules the target out as a hard miss; a payload
/// with no verdict key still gets its names extracted. Handles both chrome-use
/// shapes — latest `{"ok":true,"sessions":[{"name":..},..]}` (the top-level
/// `sessions` key lands in `extra`) and legacy `{"data":{"sessions":["name",..]}}`.
fn session_listed(resp: &ChromeResponse, target: &str) -> bool {
    if resp.verdict() == Some(false) {
        return false;
    }
    let names = resp
        .extra
        .get("sessions")
        .or_else(|| resp.data.as_ref().and_then(|d| d.get("sessions")));
    let Some(Value::Array(arr)) = names else {
        return false;
    };
    arr.iter().any(|entry| match entry {
        Value::String(s) => s == target,
        Value::Object(_) => entry.get("name").and_then(Value::as_str) == Some(target),
        _ => false,
    })
}

/// Best-effort close of an ephemeral session AFTER the action envelope is
/// emitted — never changes the action's exit code; its diagnostics, including
/// the report of a helper it leaves behind, go to stderr.
async fn close_ephemeral(name: &str) {
    let Some(path) = cli_path() else {
        eprintln!(
            "mahbot chrome: could not close ephemeral session '{name}' (chrome-use CLI not found)"
        );
        return;
    };
    // A lifecycle close, not agent work (the probe exception in
    // [`StepClocks::probe`]): the product bounds it itself, declares chrome-use's
    // clock honestly, and suppresses the relay self-heal — derived through the
    // one probe shape so it cannot drift from the CLI's other probes.
    let clocks = StepClocks::probe(SESSION_STOP_TIMEOUT);
    match spawn_cli(CliSpawn {
        path: &path,
        args: &["session", "stop", name],
        session: None,
        json: true,
        capture_stderr: false,
        cancel_kills: true,
        timeout: CliTimeout::Bounded(clocks.call.kill),
        input: None,
        chrome_side: clocks.call.chrome_side,
        recovery: clocks.recovery,
    })
    .await
    {
        CliRun::Output(out) => {
            // A helper the close itself leaves holding the call's output channel
            // is recorded here for the caller ([`run_cli`]) to report — the
            // action envelope went out before this ran, so it cannot carry it.
            CHROME_USE_LEFTOVER_PIPES.fetch_or(out.leftover_pipes, Ordering::Relaxed);
            if !out.status.success() {
                eprintln!("mahbot chrome: failed to close ephemeral session '{name}'");
            }
        }
        CliRun::SpawnFailure => {
            eprintln!(
                "mahbot chrome: could not spawn chrome-use to close ephemeral session '{name}'"
            );
        }
        CliRun::TimedOut => {
            eprintln!("mahbot chrome: timed out closing ephemeral session '{name}'");
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    // The clock arithmetic has one home; tests here derive their expectations
    // from it instead of repeating its literals.
    use crate::chrome::kill_bound;

    #[test]
    fn eval_count_unwraps_chrome_use_result_envelope() {
        let resp = |data: Value| ChromeResponse {
            success: Some(true),
            ok: None,
            data: Some(data),
            error: None,
            code: None,
            retryable: None,
            extra: serde_json::Map::default(),
        };
        // chrome-use 1.5.101 wraps eval output as {origin, result}.
        assert_eq!(
            eval_count(&resp(json!({"origin": "https://x", "result": 7}))),
            Some(7)
        );
        // Tolerance: bare value, text form, string-wrapped number.
        assert_eq!(eval_count(&resp(json!(3))), Some(3));
        assert_eq!(
            eval_count(&resp(json!({"origin": "https://x", "result": "12"}))),
            Some(12)
        );
        assert_eq!(eval_count(&resp(json!("0"))), Some(0));
        // A non-numeric result is not a count.
        assert_eq!(
            eval_count(&resp(json!({"origin": "https://x", "result": "abc"}))),
            None
        );
        assert_eq!(eval_count(&resp(Value::Null)), None);
        // eval_result unwraps the wrapped JS value for the eval action too,
        // including the chrome-error probe's boolean.
        let wrapped = resp(json!({"origin": "https://x", "result": {"a": 1}}));
        assert_eq!(eval_result(&wrapped), Some(&json!({"a": 1})));
        let boolean = resp(json!({"origin": "https://x", "result": true}));
        assert_eq!(eval_result(&boolean).and_then(Value::as_bool), Some(true));
        // An object that merely carries a `result` key is NOT the wrapper.
        let bare = json!({"result": "page payload", "other": 1});
        assert_eq!(eval_result(&resp(bare.clone())), Some(&bare));
    }

    #[test]
    fn resolve_session_named_prefixing_is_idempotent() {
        let (name, ephemeral) = resolve_session(Some("foo"));
        assert_eq!(name, "mahbot-chrome-foo");
        assert!(!ephemeral);

        let (name, ephemeral) = resolve_session(Some("mahbot-chrome-foo"));
        assert_eq!(name, "mahbot-chrome-foo");
        assert!(!ephemeral);
    }

    #[test]
    fn resolve_session_ephemeral_uses_cli_prefix() {
        let (name, ephemeral) = resolve_session(None);
        assert!(name.starts_with(CLI_EPHEMERAL_PREFIX));
        assert!(ephemeral);
    }

    /// The alphabet is chrome-use's own for `session stop`
    /// (`validation::is_valid_session_name`: alphanumerics, '-' and '_'). A name
    /// with a '.' parses and opens fine, but chrome-use then refuses to stop it,
    /// so the session's daemon and tabs stay behind and neither the hand recipe
    /// nor the automatic recovery can clear it — the name has to be refused here,
    /// where the caller can still choose another one.
    #[test]
    fn session_names_stay_within_the_alphabet_chrome_use_can_stop() {
        for name in ["qa-2933", "qa_2933", "Qa2933"] {
            assert!(
                validate_session_name(name).is_ok(),
                "{name} must be accepted"
            );
        }
        for name in ["qa.2933", "qa 2933", "", "../qa", "qa/2933", "qa;stop"] {
            assert!(
                validate_session_name(name).is_err(),
                "{name:?} must be refused — chrome-use will not stop such a session"
            );
        }
        assert!(
            parse_invocation(&[
                "session".into(),
                "stop".into(),
                "qa.2933".into(),
                "--force".into()
            ])
            .is_err(),
            "the refusal must also be wired into the stop verb, not only --session"
        );
    }

    #[test]
    fn step_clocks_ride_the_kill_above_the_clock_they_declare() {
        // The arithmetic has one home ([`crate::chrome::clocks`] /
        // [`crate::chrome::kill_bound`], pinned there); these assertions are about
        // which shape the CLI takes, so they derive their expectations from it
        // rather than repeating its literals.
        let chrome_own = clocks(None, CliRecovery::Allowed);

        // The operation's own step (`open`'s navigation): chrome-use runs it to
        // its own client tolerance, and the operation's budget does NOT bound
        // the kill — a navigation chrome-use is still working on must be free to
        // report its own reason.
        let navigation = StepClocks::operation();
        assert_eq!(navigation.call.chrome_side, CHROME_USE_DECLARED_BUDGET);
        assert_eq!(
            navigation.call.kill,
            kill_bound(CHROME_USE_DECLARED_BUDGET, CliRecovery::Allowed)
        );

        // A verb chrome-use accepts no `--timeout` for: with no user bound the kill
        // is the one the declared clock derives, and a user bound IS the bound the
        // call really runs to — the parse refuses anything below the declared clock
        // (pinned at that layer), so the kill is never shorter than the declaration.
        let no_bound = StepClocks::tool_clock(None);
        assert_eq!(no_bound.call.chrome_side, CHROME_USE_DECLARED_BUDGET);
        assert_eq!(no_bound.call.kill, chrome_own.kill);
        let at_the_clock = StepClocks::tool_clock(Some(CHROME_USE_DECLARED_BUDGET));
        assert_eq!(at_the_clock.call.kill, chrome_own.kill);

        // A LONGER one (`eval --timeout 300`) is a bound the product must really
        // let the call run to, so it widens the kill — and the kill alone: the
        // clock DECLARED to chrome-use stays one kill margin under its own client
        // tolerance, which nothing can raise without making chrome-use fail as a
        // wedged session. The product's own kill is what the envelope reports as
        // `timeout_ms`.
        let wide = StepClocks::tool_clock(Some(Duration::from_secs(300)));
        assert_eq!(wide.call.chrome_side, CHROME_USE_DECLARED_BUDGET);
        assert_eq!(
            wide.call.kill,
            kill_bound(Duration::from_secs(300), CliRecovery::Allowed)
        );
        let env = StepFailure {
            kind: OutKind::Timeout,
            message: String::new(),
        }
        .envelope("eval", json!({ "js": "1" }), wide);
        assert_eq!(env.payload["timeout_ms"], json!(wide.call.kill.as_millis()));
        let declared_ms = format!("{}ms", CHROME_USE_DECLARED_BUDGET.as_millis());
        assert!(
            env.payload["error"]
                .as_str()
                .is_some_and(|e| e.contains(&declared_ms)),
            "the kill must ride above the clock the call really ran to: {}",
            env.payload["error"]
        );

        // A bounded step of the operation: a probe in everything but name — it
        // suppresses the relay self-heal and its kill rests [`KILL_SLACK`] above
        // the product's own bound, never chrome-use's clock.
        let probe = StepClocks::bounded(CHROME_USE_DECLARED_BUDGET, Duration::from_secs(2));
        assert_eq!(probe.call.kill, Duration::from_secs(2) + KILL_SLACK);
        assert!(matches!(probe.recovery, CliRecovery::Suppressed));
        let settle = StepClocks::bounded(Duration::from_secs(6), Duration::from_secs(6));
        assert_eq!(settle.call.chrome_side, Duration::from_secs(6));
        assert_eq!(settle.call.kill, Duration::from_secs(6) + KILL_SLACK);
        // A bound above chrome-use's own clock is still the PRODUCT's bound: the
        // step is cut off at it, not at chrome-use's clock.
        let oversized = StepClocks::bounded(CHROME_USE_DECLARED_BUDGET, Duration::from_secs(600));
        assert_eq!(oversized.call.kill, Duration::from_secs(600) + KILL_SLACK);
        assert!(oversized.call.kill > chrome_own.kill);
    }

    #[test]
    fn settle_budget_caps_and_reserves_capture() {
        // Plenty of room: capped at SETTLE_CAP.
        assert_eq!(settle_budget(Duration::from_secs(22)), Some(SETTLE_CAP));
        // Tight page: the capture reserve is honored first.
        assert_eq!(
            settle_budget(Duration::from_secs(4)),
            Some(Duration::from_millis(1500))
        );
        // Exactly at the skip threshold still runs; a hair under is skipped.
        assert_eq!(
            settle_budget(Duration::from_millis(3500)),
            Some(MIN_SETTLE_BUDGET)
        );
        assert_eq!(settle_budget(Duration::from_millis(3499)), None);
        assert_eq!(settle_budget(Duration::ZERO), None);
    }

    #[test]
    fn session_stop_gating_resolves_and_protects() {
        // CLI-namespace names pass through; bare names are prefixed.
        assert_eq!(
            resolve_stop_target("mahbot-chrome-foo", false).unwrap(),
            "mahbot-chrome-foo"
        );
        assert_eq!(
            resolve_stop_target("foo", false).unwrap(),
            "mahbot-chrome-foo"
        );
        // Protected namespaces are refused without --force, stopped as-is with it.
        assert!(resolve_stop_target("agent-tab-1", false).is_err());
        assert_eq!(
            resolve_stop_target("agent-tab-1", true).unwrap(),
            "agent-tab-1"
        );
        assert!(resolve_stop_target("link-enricher-7", false).is_err());
    }

    #[test]
    fn resolve_session_target_resolves_namespaces() {
        // A bare name is prefixed like `--session`; both namespace names pass
        // through as-is (status may probe protected sessions read-only).
        assert_eq!(resolve_session_target("docs"), "mahbot-chrome-docs");
        assert_eq!(
            resolve_session_target("mahbot-chrome-foo"),
            "mahbot-chrome-foo"
        );
        assert_eq!(resolve_session_target("agent-tab-1"), "agent-tab-1");
        assert_eq!(resolve_session_target("link-enricher-7"), "link-enricher-7");
    }

    #[test]
    fn session_status_parses_and_rejects() {
        // status parses into SessionStatus.
        let inv = parse_invocation(&["session".into(), "status".into(), "docs".into()])
            .expect("session status parses");
        match inv.action {
            Action::SessionStatus { name } => assert_eq!(name, "docs"),
            _ => panic!("expected SessionStatus"),
        }

        // The global --session flag is rejected for session subcommands.
        let err = parse_invocation(&[
            "session".into(),
            "status".into(),
            "docs".into(),
            "--session".into(),
            "x".into(),
        ])
        .err()
        .expect("--session must be rejected for session subcommands");
        assert_eq!(err, "--session is not valid for session stop/status");

        // --force is rejected for status.
        let err = parse_session(
            None,
            &["status".into(), "docs".into(), "--force".into()],
            (&[], &["force"]),
        )
        .err()
        .expect("--force must be rejected for session status");
        assert_eq!(err, "--force is not valid for session status");

        // Extra positionals are rejected for status.
        assert!(
            parse_session(
                None,
                &["status".into(), "a".into(), "b".into()],
                (&[], &["force"]),
            )
            .is_err()
        );

        // An unknown sub names both verbs.
        let err = parse_invocation(&["session".into(), "destroy".into(), "docs".into()])
            .err()
            .expect("unknown sub must be rejected");
        assert!(
            err.contains("unknown session subcommand 'destroy' (expected 'stop' or 'status')"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn session_listed_handles_both_shapes() {
        // Latest shape: the top-level `sessions` array of objects (lands in
        // `extra`), listed by name.
        let latest: ChromeResponse = serde_json::from_value(json!({
            "ok": true, "sessions": [{ "name": "mahbot-chrome-docs" }]
        }))
        .expect("deserialize latest shape");
        assert!(session_listed(&latest, "mahbot-chrome-docs"));
        assert!(!session_listed(&latest, "mahbot-chrome-other"));

        // Legacy shape: `data.sessions` array of strings.
        let legacy: ChromeResponse = serde_json::from_value(json!({
            "success": true, "data": { "sessions": ["mahbot-chrome-docs"] }
        }))
        .expect("deserialize legacy shape");
        assert!(session_listed(&legacy, "mahbot-chrome-docs"));

        // An explicit failure verdict rules the target out.
        let failed: ChromeResponse =
            serde_json::from_value(json!({ "success": false })).expect("deserialize failure");
        assert!(!session_listed(&failed, "mahbot-chrome-docs"));

        // No sessions key at all → not listed.
        let no_sessions: ChromeResponse =
            serde_json::from_value(json!({ "success": true, "data": {} }))
                .expect("deserialize empty payload");
        assert!(!session_listed(&no_sessions, "mahbot-chrome-docs"));
    }

    #[test]
    fn protected_stop_note_targets_only_protected_wedges() {
        // A wedge-shaped failure on a protected target gets the note — with
        // the full actionable command, so no stitching with the shared hint.
        let mut msg = "session unresponsive: no response within 45s".to_string();
        append_protected_stop_note(&mut msg, "agent-tab-1");
        assert!(
            msg.ends_with("(protected namespace: stopping 'agent-tab-1' requires `session stop agent-tab-1 --force`)"),
            "note missing: {msg}"
        );

        // An ordinary session's stop needs no --force — no note.
        let mut msg = "session unresponsive: no response within 45s".to_string();
        append_protected_stop_note(&mut msg, "mahbot-chrome-docs");
        assert_eq!(msg, "session unresponsive: no response within 45s");

        // A non-wedge failure on a protected target keeps its honest cause.
        let mut msg = "element not found".to_string();
        append_protected_stop_note(&mut msg, "agent-tab-1");
        assert_eq!(msg, "element not found");
    }

    #[test]
    fn envelope_layers_accurate_remediation() {
        // The envelope itself does not carry the session-wedge text — that is
        // applied after dispatch, where the session name is known (see
        // `recover_session_wedge`). A wedge message passes through untouched.
        let env = StepFailure {
            kind: OutKind::Environment,
            message: "session unresponsive: no response within 45s".to_string(),
        }
        .envelope(
            "session",
            json!({ "session": "mahbot-chrome-docs" }),
            StepClocks::probe(SESSION_STOP_TIMEOUT),
        );
        assert_eq!(
            env.payload.get("error").and_then(Value::as_str),
            Some("session unresponsive: no response within 45s")
        );

        // A non-wedge message is returned untouched too.
        let env = StepFailure {
            kind: OutKind::Environment,
            message: "relay isn't connected".to_string(),
        }
        .envelope(
            "session",
            json!({ "session": "mahbot-chrome-docs" }),
            StepClocks::probe(SESSION_STOP_TIMEOUT),
        );
        assert_eq!(
            env.payload.get("error").and_then(Value::as_str),
            Some("relay isn't connected")
        );

        // An orphaned-tab error still picks up its hand-close guidance here: it
        // is the one remediation the envelope owns, because it is the one state
        // nothing recovers.
        let env = StepFailure {
            kind: OutKind::Environment,
            message: "the tab this session was driving can no longer be resolved".to_string(),
        }
        .envelope(
            "open",
            json!({ "url": "https://x" }),
            StepClocks::operation(),
        );
        let err = env
            .payload
            .get("error")
            .and_then(Value::as_str)
            .expect("error present");
        assert!(
            err.contains("close the leftover tab in Chrome"),
            "got: {err}"
        );

        // A Timeout deadline kill of a product probe reports the probe's own
        // bound, and says it is a product-side bound rather than chrome-use's
        // verdict.
        let clocks = StepClocks::probe(SESSION_PROBE_TIMEOUT);
        let env = StepFailure {
            kind: OutKind::Timeout,
            message: String::new(),
        }
        .envelope(
            "session",
            json!({ "session": "mahbot-chrome-docs" }),
            clocks,
        );
        let expected = deadline_error(
            clocks.call.chrome_side,
            clocks.call.kill,
            "the step did not complete",
        );
        assert_eq!(
            env.payload.get("error").and_then(Value::as_str),
            Some(expected.as_str())
        );
        assert_eq!(env.payload["timeout_ms"], 20_000);
        assert!(expected.contains("the product's own bound for the step"));
        assert!(!expected.contains("rode above"));
    }

    #[test]
    fn wedge_keying_uses_who_ended_the_step_not_the_kind_alone() {
        let env = |kind: OutKind, error: &str| {
            out_env(
                "open",
                false,
                kind,
                json!({ "url": "https://x", "error": error }),
            )
        };
        // chrome-use's own session-unresponsive classification (Environment) is
        // the one wedge signal that needs no confirmation: chrome-use diagnosed
        // it rather than being cut off.
        let diagnosed = env(
            OutKind::Environment,
            "session unresponsive: no response within 45s",
        );
        assert_eq!(wedge_action(&diagnosed, true, false), WedgeAction::Recover);
        let cdp = env(
            OutKind::Environment,
            "CDP session is unresponsive after attaching (Connection reset).",
        );
        assert_eq!(wedge_action(&cdp, true, false), WedgeAction::Recover);

        // A Timeout chrome-use itself reported — what a failed `expect` /
        // `open --expect` / `wait` produces normally — is NOT a wedge: stopping
        // that session would only lose its tabs.
        let verdict = env(OutKind::Timeout, "Wait timed out after 15000ms");
        assert_eq!(wedge_action(&verdict, true, false), WedgeAction::Leave);

        // A Timeout the PRODUCT's own clock ended is only a candidate: chrome-use
        // was cut off before it could classify anything, so the session's
        // liveness is unknown and the bounded probe has to confirm it.
        let killed = env(OutKind::Timeout, "deadline reached after 92000ms");
        assert_eq!(wedge_action(&killed, true, true), WedgeAction::Probe);

        // `open --expect --structural` labels a deadline expiration Redesign
        // instead of Timeout, so a product-clock ending keys the same way.
        let redesign = env(
            OutKind::Redesign,
            "possible DOM redesign or structural change",
        );
        assert_eq!(wedge_action(&redesign, true, true), WedgeAction::Probe);
        assert_eq!(wedge_action(&redesign, true, false), WedgeAction::Leave);

        // Other kinds and other Environment messages are no evidence either, and
        // an ephemeral (unnamed for recovery) session is never touched.
        let relay = env(OutKind::Environment, "relay isn't connected");
        assert_eq!(wedge_action(&relay, true, true), WedgeAction::Leave);
        let missing = env(OutKind::Error, "element not found");
        assert_eq!(wedge_action(&missing, true, false), WedgeAction::Leave);
        let net = env(OutKind::Network, "net::ERR_CONNECTION_REFUSED");
        assert_eq!(wedge_action(&net, true, true), WedgeAction::Leave);
        assert_eq!(wedge_action(&killed, false, true), WedgeAction::Leave);
    }

    /// The end of the keying: an envelope the action left for
    /// [`WedgeAction::Leave`] reaches the caller untouched — no session stop, no
    /// annotation.
    #[tokio::test]
    async fn wedge_recovery_leaves_a_normal_timeout_alone() {
        let failure =
            |error: &str| out_env("expect", false, OutKind::Timeout, json!({ "error": error }));
        let mut env = failure("condition not met");
        recover_session_wedge(&mut env, "mahbot-chrome-docs", true, false).await;
        assert_eq!(env.payload["error"], "condition not met");

        let mut env = failure("condition not met");
        recover_session_wedge(&mut env, "mahbot-chrome-ephemeral-x", false, true).await;
        assert_eq!(env.payload["error"], "condition not met");
    }

    #[test]
    fn wedge_recovery_note_reports_what_was_done_or_the_manual_flow() {
        // The automatic recovery stopped the session: its own summary, and no
        // manual flow.
        let stopped = wedge_recovery_note(SessionRecovery::Stopped);
        assert_eq!(stopped, SessionRecovery::Stopped.summary());
        assert!(!stopped.contains("recover with"), "got: {stopped}");
        // The stop was issued but never confirmed, or could not be started: the
        // summary itself carries that, and the manual flow is named — the only
        // place it is.
        for recovery in [SessionRecovery::Unanswered, SessionRecovery::NotStarted] {
            let note = wedge_recovery_note(recovery);
            assert!(note.starts_with(recovery.summary()), "got: {note}");
            assert!(note.contains(SESSION_RECOVERY_FLOW), "got: {note}");
            assert!(!note.contains("could not run"), "got: {note}");
        }
    }

    #[test]
    fn append_error_note_extends_the_error_text_only() {
        let timeout_error = "deadline reached after 92000ms — the step did not complete";
        let mut env = out_env(
            "open",
            false,
            OutKind::Timeout,
            json!({ "url": "https://x", "error": timeout_error }),
        );
        append_error_note(&mut env, "what was done");
        assert_eq!(
            env.payload.get("error").and_then(Value::as_str),
            Some(format!("{timeout_error} — what was done").as_str())
        );
        // Other payload keys and an error-less payload are untouched.
        let mut env = out_env("open", false, OutKind::Ok, json!({ "url": "https://x" }));
        append_error_note(&mut env, "what was done");
        assert!(env.payload.get("error").is_none());
    }

    /// Direct pin of the spawn-level classification, including the crux of the
    /// expect contract: a failed assertion arrives as `success:true` with
    /// exit 1 and the verdict in `data`.
    #[test]
    fn classify_step_output_pins_envelope_over_exit_code() {
        let envelope = |body: Value| serde_json::to_vec(&body).expect("serialize envelope");
        let outcome = |r: &StepOutcome| match r {
            Ok(resp) => Ok(resp.data.clone()),
            Err(f) => Err((f.kind, f.message.clone())),
        };

        // expect pass=false: success:true + exit 1 → the verdict is returned.
        let resp = classify_step_output(
            true,
            false,
            Some(1),
            false,
            &envelope(
                json!({"success": true, "data": {"pass": false, "actual": 3, "timedOut": true}}),
            ),
            "",
        )
        .expect("expect verdict must survive the non-zero exit");
        assert_eq!(
            resp.data
                .as_ref()
                .and_then(expect_outcome)
                .map(|o| (o.pass, o.timed_out, o.actual)),
            Some((false, true, Some(json!(3))))
        );

        // …but only for expect: the same payload on another action is a
        // generic failure (fallback message, no envelope error).
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            &envelope(json!({"success": true, "data": {"pass": false}})),
            "",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Error, "chrome-use exited with code 1".into()))
        );

        // expect un-evaluable (no browser): success:false + exit 2 → environment.
        let r = classify_step_output(
            true,
            false,
            Some(2),
            false,
            &envelope(
                json!({"success": false, "error": "Browser not launched", "code": "browser_not_launched"}),
            ),
            "",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Environment, "Browser not launched".into()))
        );

        // Honest timeout classification on a failed wait.
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            &envelope(json!({"success": false, "error": "Wait timed out after 15000ms"})),
            "",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Timeout, "Wait timed out after 15000ms".into()))
        );

        // Happy path: exit 0 + success envelope.
        assert!(
            outcome(&classify_step_output(
                false,
                false,
                Some(0),
                true,
                &envelope(json!({"success": true, "data": {}})),
                ""
            ))
            .is_ok()
        );

        // Unparseable stdout: zero exit → non-JSON error; non-zero → stderr fallback.
        let r = classify_step_output(false, false, Some(0), true, b"garbage", "");
        assert_eq!(
            outcome(&r),
            Err((OutKind::Error, "chrome-use returned non-JSON output".into()))
        );
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            b"garbage",
            "relay is not connected",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Environment, "relay is not connected".into()))
        );
    }

    /// The canned stale-relay hint is stripped from Timeout-classified
    /// failures only — envelope `error` text and the stderr fallback alike —
    /// while non-Timeout messages keep it verbatim.
    #[test]
    fn classify_step_output_strips_canned_relay_hint() {
        let envelope = |body: Value| serde_json::to_vec(&body).expect("serialize envelope");
        let outcome = |r: &StepOutcome| match r {
            Ok(resp) => Ok(resp.data.clone()),
            Err(f) => Err((f.kind, f.message.clone())),
        };
        let hint = "Hint: the session's browser connection is unresponsive (likely a stale \
                    relay/service-worker mid-session). Reconnect with `connect`, or close the \
                    session and reopen it.";

        // Timeout envelope: hint stripped, honest prefix kept.
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            &envelope(
                json!({"success": false, "error": format!("Wait timed out after 15000ms. {hint}")}),
            ),
            "",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Timeout, "Wait timed out after 15000ms".into()))
        );

        // A non-timeout envelope carrying the hint keeps it verbatim (kind Error,
        // not stripped — only Timeout-classified messages are rewritten).
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            &envelope(json!({"success": false, "error": format!("some other failure. {hint}")})),
            "",
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Error, format!("some other failure. {hint}")))
        );

        // The stderr fallback is classified and sanitized too: a timeout
        // phrasing with the canned hint in stderr → hint stripped.
        let r = classify_step_output(
            false,
            false,
            Some(1),
            false,
            b"garbage",
            &format!("Wait timed out after 15000ms. {hint}"),
        );
        assert_eq!(
            outcome(&r),
            Err((OutKind::Timeout, "Wait timed out after 15000ms".into()))
        );
    }

    /// A successful envelope carrying chrome-use's browser-replacement note is a
    /// failure, never a quiet success — and it is deliberately NOT the
    /// daemon-down path (the daemon is fine; the connection to the real browser
    /// was lost). The benign degraded-success warnings keep today's behaviour.
    #[test]
    fn classify_step_output_fails_a_self_launched_browser() {
        let envelope = |body: Value| serde_json::to_vec(&body).expect("serialize envelope");
        let note = "This session's previous browser is gone (its browser connection was dead) \
                    and a fresh one was launched for this command";
        let r = classify_step_output(
            false,
            false,
            Some(0),
            true,
            &envelope(json!({"success": true, "warning": note})),
            "",
        );
        let Err(f) = r else {
            panic!("a browser chrome-use started itself must not classify as success")
        };
        assert_eq!(f.kind, OutKind::Environment);
        assert_eq!(f.message, self_launched_browser_error(note));

        // The expect-style success path (envelope trusted over a non-zero exit)
        // applies the same rule.
        let r = classify_step_output(
            true,
            false,
            Some(1),
            false,
            &envelope(json!({"success": true, "warning": note, "data": {"pass": true}})),
            "",
        );
        let Err(f) = r else {
            panic!("a browser chrome-use started itself must not classify as success")
        };
        assert_eq!(f.kind, OutKind::Environment);

        // A degraded-success warning is NOT browser replacement: the step still
        // succeeds and the action-specific envelope judges it.
        let r = classify_step_output(
            false,
            false,
            Some(0),
            true,
            &envelope(
                json!({"success": true, "warning": "no key listeners on the focused element"}),
            ),
            "",
        );
        assert!(r.is_ok());
    }

    /// An unparseable answer the product itself cut off names the truncation,
    /// while a genuinely malformed one keeps the generic text — a product
    /// truncation must never read as chrome-use's malformed output, on a
    /// non-zero exit too, where the stderr fallback would otherwise claim
    /// chrome-use's own failure.
    #[test]
    fn classify_step_output_names_a_truncated_answer() {
        let outcome = |r: &StepOutcome| match r {
            Ok(resp) => Ok(resp.data.clone()),
            Err(f) => Err((f.kind, f.message.clone())),
        };
        let r = classify_step_output(false, true, Some(0), true, b"garbage", "");
        assert_eq!(outcome(&r), Err((OutKind::Error, truncated_output_error())));
        let r = classify_step_output(false, false, Some(0), true, b"garbage", "");
        assert_eq!(
            outcome(&r),
            Err((OutKind::Error, "chrome-use returned non-JSON output".into()))
        );
        // A non-zero exit with an unparsed, truncated answer names the
        // truncation too, instead of the stderr fallback.
        let r = classify_step_output(false, true, Some(1), false, b"garbage", "some stderr");
        assert_eq!(outcome(&r), Err((OutKind::Error, truncated_output_error())));
        let r = classify_step_output(false, false, Some(1), false, b"garbage", "some stderr");
        assert_eq!(outcome(&r), Err((OutKind::Error, "some stderr".into())));
    }

    #[test]
    fn network_failure_error_names_code_or_falls_back() {
        assert_eq!(
            network_failure_error(Some("ERR_NAME_NOT_RESOLVED")),
            "Chrome rendered an error page — ERR_NAME_NOT_RESOLVED (DNS resolution failure)"
        );
        assert_eq!(
            network_failure_error(None),
            "Chrome rendered an error page (site unreachable or refused)"
        );
    }

    #[test]
    fn step_failure_envelope_reports_timeout_vs_error() {
        // A declared (forwarded) deadline kill: the envelope reports the
        // product's own bound for the step (its kill) as `timeout_ms`, and the
        // error names that bound and the clock chrome-use was working to.
        let clocks = StepClocks::forwarded(Duration::from_secs(10));
        let env = StepFailure {
            kind: OutKind::Timeout,
            message: String::new(),
        }
        .envelope("wait", json!({ "selector": ".x" }), clocks);
        assert_eq!(
            env.payload["timeout_ms"],
            json!(clocks.call.kill.as_millis())
        );
        let kill = kill_bound(Duration::from_secs(10), CliRecovery::Allowed).as_millis();
        assert_eq!(
            env.payload["error"],
            format!(
                "deadline reached after {kill}ms — the step did not complete: that is the \
                 product's own bound, which rode above the 10000ms deadline chrome-use itself \
                 was working to, so chrome-use never reported its own reason"
            )
        );

        // Any other failure reports the chrome-use error text, no timeout_ms.
        let env = StepFailure {
            kind: OutKind::Network,
            message: "net::ERR_NAME_NOT_RESOLVED".into(),
        }
        .envelope(
            "open",
            json!({ "url": "https://x" }),
            StepClocks::operation(),
        );
        assert_eq!(env.payload["error"], "net::ERR_NAME_NOT_RESOLVED");
        assert_eq!(env.payload["error_code"], "ERR_NAME_NOT_RESOLVED");
        assert!(env.payload.get("timeout_ms").is_none());

        // A Network failure with no recognizable net error token gets no
        // error_code.
        let env = StepFailure {
            kind: OutKind::Network,
            message: "connection refused by peer".into(),
        }
        .envelope(
            "open",
            json!({ "url": "https://x" }),
            StepClocks::operation(),
        );
        assert!(env.payload.get("error_code").is_none());
    }

    #[test]
    fn expect_envelope_maps_pass_timeout_and_false() {
        // pass → rc 0 / ok.
        let env = expect_envelope(
            "'#main' is visible",
            ExpectOutcome {
                pass: true,
                actual: Some(json!({"tag": "h1"})),
                timed_out: false,
            },
        );
        assert!(env.ok);
        assert_eq!(env.kind, OutKind::Ok);
        assert_eq!(env.payload["pass"], json!(true));
        assert_eq!(env.payload["actual"], json!({"tag": "h1"}));

        // deadline expiration → timeout.
        let env = expect_envelope(
            "count('.card') == 3",
            ExpectOutcome {
                pass: false,
                actual: Some(json!(0)),
                timed_out: true,
            },
        );
        assert!(!env.ok);
        assert_eq!(env.kind, OutKind::Timeout);
        assert_eq!(env.payload["pass"], json!(false));
        assert_eq!(env.payload["timed_out"], json!(true));
        assert_eq!(
            env.payload["error"],
            json!(
                "condition was not met within the deadline — consider verifying the condition or allowing more time"
            )
        );

        // plain false → error (no timed_out key).
        let env = expect_envelope(
            "url contains \"dashboard\"",
            ExpectOutcome {
                pass: false,
                actual: Some(json!("about:blank")),
                timed_out: false,
            },
        );
        assert!(!env.ok);
        assert_eq!(env.kind, OutKind::Error);
        assert_eq!(env.payload["pass"], json!(false));
        assert!(env.payload.get("timed_out").is_none());

        // `actual: None` omits the actual key.
        let env = expect_envelope(
            "'#main' is present",
            ExpectOutcome {
                pass: true,
                actual: None,
                timed_out: false,
            },
        );
        assert!(env.ok);
        assert_eq!(env.kind, OutKind::Ok);
        assert_eq!(env.payload["pass"], json!(true));
        assert!(env.payload.get("actual").is_none());
    }

    /// The `--timeout` of a verb that DECLARES its value to chrome-use
    /// (`wait`/`expect`, and `open` through its `--expect` wait) is capped below
    /// chrome-use's own client tolerance ([`CHROME_USE_OWN_BUDGET`]). A deadline at
    /// or above the tolerance makes chrome-use run OUT of tolerance instead of
    /// answering, so its session-unresponsive classification — which stops the
    /// session and loses its open tabs — replaces the honest "condition was not
    /// met". The rule is enforced at parse time: a too-long deadline is refused with
    /// a usage error instead of accepted and then silently mis-reported.
    ///
    /// A verb that forwards nothing is refused the OTHER way: chrome-use takes no
    /// per-call deadline for it, so nothing can honour a bound below the clock
    /// mahbot declares to chrome-use — such a request is refused rather than
    /// accepted and discarded, while a bound at or above it (`eval --timeout 300`)
    /// is one the call really runs to.
    #[test]
    fn timeout_flag_is_capped_below_chrome_use_client_tolerance() {
        let err = parse_invocation(&["wait".into(), "#x".into(), "--timeout".into(), "45".into()])
            .err()
            .expect("a deadline at the tolerance must be refused");
        assert!(
            err.contains("45") && err.contains("client tolerance"),
            "err must be the tolerance refusal, naming the value: {err}"
        );

        assert!(
            parse_invocation(&["wait".into(), "#x".into(), "--timeout".into(), "44".into(),])
                .is_ok(),
            "one second below the tolerance must parse"
        );

        for args in [
            &["expect".into(), "#x".into(), "visible".into()][..],
            &["open".into(), "https://example.com".into()][..],
        ] {
            let mut argv = args.to_vec();
            argv.extend(["--timeout".into(), "45".into()]);
            let err = parse_invocation(&argv)
                .err()
                .unwrap_or_else(|| panic!("{argv:?} must refuse a deadline at the tolerance"));
            // The tolerance refusal itself, not another rejection of the line: it
            // names both the value asked for and the tolerance it crossed.
            assert!(
                err.contains("45") && err.contains("client tolerance"),
                "{argv:?} must be refused on the value: {err}"
            );
        }

        // A verb chrome-use takes no per-call deadline for: a bound below the clock
        // mahbot declares to chrome-use cannot be honoured, so it is refused — with
        // the value the caller asked for and the shortest one that IS honoured.
        let declared = CHROME_USE_DECLARED_BUDGET.as_secs().to_string();
        for action in [
            &["count".into(), ".x".into()][..],
            &["eval".into(), "1+1".into()][..],
            &["extract".into(), "--schema-file".into(), "s.json".into()][..],
            &["click".into(), "#a".into()][..],
            &["fill".into(), "#a".into(), "t".into()][..],
            &["type".into(), "#a".into(), "t".into()][..],
            &["press".into(), "Enter".into()][..],
        ] {
            let mut argv = action.to_vec();
            argv.extend(["--timeout".into(), "5".into()]);
            let err = parse_invocation(&argv)
                .err()
                .unwrap_or_else(|| panic!("{argv:?} must refuse an unhonourable bound"));
            assert!(
                err.contains("--timeout 5s") && err.contains(&declared),
                "{argv:?} must name the refused value and the shortest honoured one: {err}"
            );
        }
        // At the declared clock it parses, and it is the bound the call runs to.
        let at_the_clock = parse_invocation(&[
            "eval".into(),
            "1+1".into(),
            "--timeout".into(),
            declared.clone(),
        ])
        .expect("a bound at the declared clock is honoured");
        assert!(matches!(
            at_the_clock.action,
            Action::Eval { bound: Some(b), .. } if b == CHROME_USE_DECLARED_BUDGET
        ));

        let wide = parse_invocation(&[
            "eval".into(),
            "1+1".into(),
            "--timeout".into(),
            "300".into(),
        ])
        .expect("a verb that forwards no deadline takes any bound");
        match wide.action {
            Action::Eval { bound, .. } => {
                assert_eq!(bound, Some(Duration::from_secs(300)));
            }
            _ => panic!("expected Eval with the user's bound"),
        }
    }

    #[expect(clippy::too_many_lines)]
    #[test]
    fn parse_happy_paths_support_both_flag_forms() {
        // status takes no args.
        assert!(matches!(
            parse_invocation(&["status".into()])
                .expect("status parses")
                .action,
            Action::Status
        ));

        // open with `--flag value`.
        let inv = parse_invocation(&[
            "open".into(),
            "https://example.com".into(),
            "--expect".into(),
            ".btn".into(),
            "--structural".into(),
            "--timeout".into(),
            "5".into(),
        ])
        .expect("open parses");
        match inv.action {
            Action::Open {
                url,
                expect,
                structural,
                timeout,
            } => {
                assert_eq!(url, "https://example.com");
                assert_eq!(expect.as_deref(), Some(".btn"));
                assert!(structural);
                assert_eq!(timeout, Duration::from_secs(5));
            }
            _ => panic!("expected Open"),
        }

        // open with `--flag=value`.
        let inv = parse_invocation(&[
            "open".into(),
            "https://example.com".into(),
            "--expect=.btn".into(),
            "--timeout=3".into(),
        ])
        .expect("open parses");
        match inv.action {
            Action::Open {
                expect, timeout, ..
            } => {
                assert_eq!(expect.as_deref(), Some(".btn"));
                assert_eq!(timeout, Duration::from_secs(3));
            }
            _ => panic!("expected Open"),
        }

        // open without --timeout defaults to the whole-operation budget (20 s);
        // the navigation itself runs to the clock mahbot declares to chrome-use.
        let inv =
            parse_invocation(&["open".into(), "https://example.com".into()]).expect("open parses");
        match inv.action {
            Action::Open { timeout, .. } => assert_eq!(timeout, DEFAULT_OPEN_TIMEOUT),
            _ => panic!("expected Open"),
        }

        // count / eval: a user `--timeout` is their own bound on the step (no
        // default), and only a value at or above the clock mahbot declares to
        // chrome-use parses — anything shorter is refused (see
        // `timeout_flag_is_capped_below_chrome_use_client_tolerance`), while
        // wait/expect forward their value as chrome-use's clock.
        for (action, sel) in [("count", ".x"), ("eval", "1+1")] {
            let inv = parse_invocation(&[action.into(), sel.into(), "--timeout=60".into()])
                .expect("action parses");
            match inv.action {
                Action::Count { bound, .. } | Action::Eval { bound, .. } => {
                    assert_eq!(bound, Some(Duration::from_secs(60)));
                }
                _ => panic!("expected a bounded action"),
            }
        }
        // Without --timeout a forwarding-less verb carries no bound at all:
        // the clock mahbot declares to chrome-use is its clock.
        let inv = parse_invocation(&["count".into(), ".x".into()]).expect("count parses");
        assert!(matches!(inv.action, Action::Count { bound: None, .. }));
        let inv = parse_invocation(&["eval".into(), "1+1".into()]).expect("eval parses");
        assert!(matches!(inv.action, Action::Eval { bound: None, .. }));

        // wait / expect default to the condition deadline they declare AND
        // forward as their `--timeout`, well under chrome-use's own client
        // tolerance so the tool's own verdict always fires first.
        let inv = parse_invocation(&["wait".into(), ".x".into()]).expect("wait parses");
        match inv.action {
            Action::Wait { timeout, .. } => assert_eq!(timeout, DEFAULT_STEP_TIMEOUT),
            _ => panic!("expected Wait"),
        }

        // wait with --url / --text targets.
        let inv = parse_invocation(&[
            "wait".into(),
            "--url".into(),
            "dashboard".into(),
            "--timeout=9".into(),
        ])
        .expect("wait url parses");
        match inv.action {
            Action::Wait { target, timeout } => {
                assert!(matches!(target, WaitTarget::Url(u) if u == "dashboard"));
                assert_eq!(timeout, Duration::from_secs(9));
            }
            _ => panic!("expected Wait"),
        }
        let inv = parse_invocation(&["wait".into(), "--text".into(), "Loaded".into()])
            .expect("wait text parses");
        match inv.action {
            Action::Wait { target, timeout } => {
                assert!(matches!(target, WaitTarget::Text(t) if t == "Loaded"));
                assert_eq!(timeout, DEFAULT_STEP_TIMEOUT);
            }
            _ => panic!("expected Wait"),
        }

        // expect conditions via the forms grammar.
        let inv = parse_invocation(&["expect".into(), "#main".into(), "visible".into()])
            .expect("expect state parses");
        match inv.action {
            Action::Expect { cond, timeout } => {
                assert!(matches!(
                    cond,
                    ExpectCond::State { selector, state } if selector == "#main" && state == "visible"
                ));
                assert_eq!(timeout, DEFAULT_STEP_TIMEOUT);
            }
            _ => panic!("expected Expect"),
        }
        let inv = parse_invocation(&[
            "expect".into(),
            "count".into(),
            ".card".into(),
            ">=".into(),
            "3".into(),
            "--timeout=5".into(),
        ])
        .expect("expect count parses");
        match inv.action {
            Action::Expect { cond, timeout } => {
                assert!(matches!(
                    cond,
                    ExpectCond::Count { selector, op, n } if selector == ".card" && op == ">=" && n == 3
                ));
                assert_eq!(timeout, Duration::from_secs(5));
            }
            _ => panic!("expected Expect"),
        }
        let inv = parse_invocation(&[
            "expect".into(),
            "text".into(),
            "h1".into(),
            "contains".into(),
            "Hello".into(),
            "World".into(),
        ])
        .expect("expect text parses");
        match inv.action {
            Action::Expect { cond, timeout } => {
                assert!(matches!(
                    cond,
                    ExpectCond::Text { selector, predicate, value }
                        if selector == "h1" && predicate == "contains" && value == "Hello World"
                ));
                assert_eq!(timeout, DEFAULT_STEP_TIMEOUT);
            }
            _ => panic!("expected Expect"),
        }
        let inv = parse_invocation(&[
            "expect".into(),
            "url".into(),
            "contains".into(),
            "dashboard".into(),
        ])
        .expect("expect url parses");
        match inv.action {
            Action::Expect { cond, timeout } => {
                assert!(matches!(
                    cond,
                    ExpectCond::Url { predicate, pattern }
                        if predicate == "contains" && pattern == "dashboard"
                ));
                assert_eq!(timeout, DEFAULT_STEP_TIMEOUT);
            }
            _ => panic!("expected Expect"),
        }

        // extract requires --schema-file.
        let inv = parse_invocation(&[
            "extract".into(),
            "--schema-file=rows.json".into(),
            "--limit=3".into(),
        ])
        .expect("extract parses");
        match inv.action {
            Action::Extract {
                schema_file,
                limit,
                bound,
            } => {
                assert_eq!(schema_file, "rows.json");
                assert_eq!(limit, Some(3));
                assert_eq!(bound, None);
            }
            _ => panic!("expected Extract"),
        }

        // click with --if-present.
        let inv = parse_invocation(&["click".into(), ".btn".into(), "--if-present".into()])
            .expect("click parses");
        match inv.action {
            Action::Click {
                selector,
                if_present,
                ..
            } => {
                assert_eq!(selector, ".btn");
                assert!(if_present);
            }
            _ => panic!("expected Click"),
        }

        // global --session anywhere, including before the action.
        let inv = parse_invocation(&[
            "--session".into(),
            "run-1".into(),
            "count".into(),
            ".x".into(),
        ])
        .expect("session parses");
        assert_eq!(inv.session.as_deref(), Some("run-1"));
        assert!(matches!(inv.action, Action::Count { .. }));

        // session stop with --force.
        let inv = parse_invocation(&[
            "session".into(),
            "stop".into(),
            "foo".into(),
            "--force".into(),
        ])
        .expect("session stop parses");
        match inv.action {
            Action::SessionStop { name, force } => {
                assert_eq!(name, "foo");
                assert!(force);
            }
            _ => panic!("expected SessionStop"),
        }
    }

    #[test]
    fn parse_text_input_happy_paths() {
        // fill inline multi-word text.
        let inv = parse_invocation(&["fill".into(), "#q".into(), "hello".into(), "world".into()])
            .expect("fill inline parses");
        match inv.action {
            Action::Fill {
                selector, source, ..
            } => {
                assert_eq!(selector, "#q");
                assert!(matches!(source, TextInput::Inline(t) if t == "hello world"));
            }
            _ => panic!("expected Fill"),
        }

        // fill with `--file=<path>`.
        let inv = parse_invocation(&["fill".into(), "#q".into(), "--file=post.md".into()])
            .expect("fill --file parses");
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::File(p) if p == "post.md"));
            }
            _ => panic!("expected Fill"),
        }

        // fill with a separate `--file <path>` value.
        let inv = parse_invocation(&[
            "fill".into(),
            "#q".into(),
            "--file".into(),
            "post.md".into(),
        ])
        .expect("fill --file value parses");
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::File(p) if p == "post.md"));
            }
            _ => panic!("expected Fill"),
        }

        // fill --stdin.
        let inv = parse_invocation(&["fill".into(), "#q".into(), "--stdin".into()])
            .expect("fill --stdin parses");
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::Stdin));
            }
            _ => panic!("expected Fill"),
        }

        // type with and without --key-events.
        let inv =
            parse_invocation(&["type".into(), "#q".into(), "hi".into()]).expect("type parses");
        match inv.action {
            Action::Type {
                selector,
                text,
                key_events,
                ..
            } => {
                assert_eq!(selector, "#q");
                assert_eq!(text, "hi");
                assert!(!key_events);
            }
            _ => panic!("expected Type"),
        }
        let inv = parse_invocation(&[
            "type".into(),
            "#q".into(),
            "hi".into(),
            "--key-events".into(),
        ])
        .expect("type --key-events parses");
        match inv.action {
            Action::Type { key_events, .. } => assert!(key_events),
            _ => panic!("expected Type"),
        }

        // press with selector, hold and timeout.
        let inv = parse_invocation(&[
            "press".into(),
            "Enter".into(),
            "--selector".into(),
            "#t".into(),
            "--hold".into(),
            "50".into(),
            "--timeout".into(),
            "60".into(),
        ])
        .expect("press parses");
        match inv.action {
            Action::Press {
                key,
                selector,
                hold,
                bound,
            } => {
                assert_eq!(key, "Enter");
                assert_eq!(selector.as_deref(), Some("#t"));
                assert_eq!(hold, Some(50));
                assert_eq!(bound, Some(Duration::from_secs(60)));
            }
            _ => panic!("expected Press"),
        }
    }

    /// `--` stays a true end-of-options terminator: every token after it is
    /// verbatim text, even one that names a flag. (Single-dash text no longer
    /// needs the shield — see [`fill_type_accept_single_dash_text`] — but the
    /// terminator behavior itself is unchanged.)
    #[test]
    fn parse_dash_separator_takes_text_verbatim() {
        let inv = parse_invocation(&["fill".into(), "#q".into(), "--".into(), "--foo".into()])
            .expect("fill -- separator parses");
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::Inline(t) if t == "--foo"));
            }
            _ => panic!("expected Fill"),
        }

        let inv = parse_invocation(&[
            "type".into(),
            "#q".into(),
            "--".into(),
            "--session".into(),
            "sneaky".into(),
            "rest".into(),
        ])
        .expect("type with --session-looking text parses");
        match inv.action {
            Action::Type { selector, text, .. } => {
                assert_eq!(selector, "#q");
                assert_eq!(text, "--session sneaky rest");
            }
            _ => panic!("expected Type"),
        }
    }

    /// Single-dash text is accepted naturally in fill/type (they have no
    /// single-dash flags, so any `-token` is literal text), while other
    /// actions keep rejecting it as an unknown flag.
    #[test]
    fn fill_type_accept_single_dash_text() {
        let inv =
            parse_invocation(&["fill".into(), "#q".into(), "-tail".into()]).expect("fill parses");
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::Inline(t) if t == "-tail"));
            }
            _ => panic!("expected Fill"),
        }

        // A `--session` after the text still binds globally.
        let inv = parse_invocation(&[
            "fill".into(),
            "#q".into(),
            "-tail".into(),
            "--session".into(),
            "abc".into(),
        ])
        .expect("fill with trailing --session parses");
        assert_eq!(inv.session.as_deref(), Some("abc"));
        match inv.action {
            Action::Fill { source, .. } => {
                assert!(matches!(source, TextInput::Inline(t) if t == "-tail"));
            }
            _ => panic!("expected Fill"),
        }

        let inv = parse_invocation(&["type".into(), "#q".into(), "-dashprobe".into()])
            .expect("type parses");
        match inv.action {
            Action::Type { text, .. } => assert_eq!(text, "-dashprobe"),
            _ => panic!("expected Type"),
        }

        // Other actions still reject single-dash tokens as unknown flags, and
        // fill/type still reject unknown double-dash flags.
        assert!(parse_invocation(&["count".into(), ".x".into(), "-bogus".into()]).is_err());
        assert!(parse_invocation(&["open".into(), "https://x".into(), "-bogus".into()]).is_err());
        assert!(parse_invocation(&["fill".into(), "#q".into(), "--bogus".into()]).is_err());
    }

    /// The resolved session lands as a top-level stdout field.
    #[test]
    fn surface_session_adds_top_level_field() {
        let mut env = out_env("fill", true, OutKind::Ok, json!({ "selector": "#q" }));
        surface_session(&mut env, "mahbot-chrome-abc");
        assert_eq!(env.payload["session"], json!("mahbot-chrome-abc"));
        let wire: Value = serde_json::from_str(&env.to_json()).expect("wire json");
        assert_eq!(wire["session"], json!("mahbot-chrome-abc"));
        assert_eq!(wire["kind"], json!("ok"));
    }

    /// The inline-text argv shield: a leading-dash value rides after `--` so
    /// chrome-use's arg preprocessor forwards it verbatim.
    #[test]
    fn text_value_argv_shields_leading_dash_text() {
        assert_eq!(text_value_argv("hello"), vec!["hello"]);
        assert_eq!(text_value_argv("--foo"), vec!["--", "--foo"]);
        assert_eq!(text_value_argv("-5"), vec!["--", "-5"]);
        assert_eq!(text_value_argv(""), vec![""]);
    }

    #[test]
    fn parse_rejects_usage_errors() {
        assert!(parse_invocation(&["bogus".into()]).is_err());
        assert!(parse_invocation(&["open".into()]).is_err()); // missing url
        assert!(parse_invocation(&["open".into(), "https://x".into(), "--bogus".into()]).is_err());
        assert!(parse_invocation(&["count".into()]).is_err()); // missing selector
        assert!(parse_invocation(&["extract".into()]).is_err()); // missing --schema-file
        assert!(parse_invocation(&["status".into(), "--session".into(), "x".into()]).is_err());
        assert!(parse_invocation(&["session".into(), "stop".into()]).is_err()); // missing name
        assert!(
            parse_invocation(&[
                "open".into(),
                "https://x".into(),
                "--timeout".into(),
                "abc".into()
            ])
            .is_err()
        );
        assert!(
            parse_invocation(&["open".into(), "https://x".into(), "--timeout=0".into()]).is_err()
        );
        assert!(parse_invocation(&["session".into(), "destroy".into(), "x".into()]).is_err());
        // wait must reject the numeric silent-sleep form and mixed targets.
        assert!(parse_invocation(&["wait".into(), "5000".into()]).is_err());
        assert!(
            parse_invocation(&[
                "wait".into(),
                "--url".into(),
                "a".into(),
                "--text".into(),
                "b".into()
            ])
            .is_err()
        );
        assert!(
            parse_invocation(&["wait".into(), "--url".into(), "a".into(), ".x".into()]).is_err()
        );
        // wait rejects extra positionals (previously silently ignored).
        assert!(parse_invocation(&["wait".into(), "#main".into(), "extra".into()]).is_err());
        // expect rejects missing / off-allowlist conditions.
        assert!(parse_invocation(&["expect".into()]).is_err());
        assert!(parse_invocation(&["expect".into(), "#a".into(), "gone".into()]).is_err());
        assert!(
            parse_invocation(&[
                "expect".into(),
                "count".into(),
                ".c".into(),
                "~=".into(),
                "2".into()
            ])
            .is_err()
        );
        assert!(
            parse_invocation(&[
                "expect".into(),
                "count".into(),
                ".c".into(),
                "==".into(),
                "x".into()
            ])
            .is_err()
        );
        assert!(
            parse_invocation(&[
                "expect".into(),
                "text".into(),
                "h1".into(),
                "starts".into(),
                "x".into()
            ])
            .is_err()
        );
        assert!(
            parse_invocation(&["expect".into(), "count".into(), ".c".into(), "==".into()]).is_err()
        );
    }

    /// Usage rejections for the text-input actions.
    #[test]
    fn parse_rejects_text_input_usage_errors() {
        // fill: exactly one text source.
        assert!(parse_invocation(&["fill".into(), "#q".into()]).is_err()); // none
        assert!(
            parse_invocation(&[
                "fill".into(),
                "#q".into(),
                "a".into(),
                "--file".into(),
                "f".into()
            ])
            .is_err()
        ); // inline + --file
        assert!(
            parse_invocation(&["fill".into(), "#q".into(), "a".into(), "--stdin".into()]).is_err()
        ); // inline + --stdin
        assert!(
            parse_invocation(&[
                "fill".into(),
                "#q".into(),
                "--stdin".into(),
                "--file".into(),
                "f".into()
            ])
            .is_err()
        ); // --stdin + --file
        assert!(parse_invocation(&["type".into(), "#q".into()]).is_err()); // missing text
        assert!(parse_invocation(&["press".into(), "Enter".into(), "extra".into()]).is_err()); // extra positional
        assert!(
            parse_invocation(&[
                "press".into(),
                "Enter".into(),
                "--hold".into(),
                "abc".into()
            ])
            .is_err()
        ); // non-numeric hold
        assert!(parse_invocation(&["press".into(), "Enter".into(), "--selector".into()]).is_err()); // missing value
    }

    #[test]
    fn text_input_ok_envelope_clean_success() {
        let base = json!({ "selector": "#q" });
        let resp = ChromeResponse {
            success: Some(true),
            data: Some(json!({ "value": "filled" })),
            ..ChromeResponse::default()
        };
        let env = text_input_ok_envelope("fill", &base, resp);
        assert!(env.ok);
        assert_eq!(env.kind, OutKind::Ok);
        assert_eq!(env.payload["data"]["value"], json!("filled"));
        assert!(env.payload.get("warning").is_none());
    }

    /// chrome-use emits `readBack` on EVERY readable `type` success and
    /// `keyListeners` on every listener-dependent `press` — they are not the
    /// degraded signal. Without chrome-use's `warning` field these are clean
    /// successes (rc 0).
    #[test]
    fn text_input_ok_envelope_read_back_and_key_listeners_without_warning_are_ok() {
        let typed = ChromeResponse {
            success: Some(true),
            data: Some(json!({ "typed": "hi", "readBack": "hi" })),
            ..ChromeResponse::default()
        };
        let env = text_input_ok_envelope("type", &json!({ "selector": "#q", "text": "hi" }), typed);
        assert!(env.ok);
        assert_eq!(env.kind, OutKind::Ok);

        let press = ChromeResponse {
            success: Some(true),
            data: Some(json!({ "key": "ArrowDown", "keyListeners": 3 })),
            ..ChromeResponse::default()
        };
        let env = text_input_ok_envelope("press", &json!({ "key": "ArrowDown" }), press);
        assert!(env.ok);
        assert_eq!(env.kind, OutKind::Ok);
    }

    #[test]
    fn text_input_ok_envelope_warning_in_data_is_error() {
        let base = json!({ "selector": "#q", "text": "hi" });
        let resp = ChromeResponse {
            success: Some(true),
            data: Some(json!({
                "typed": "hi", "readBack": "h",
                "warning": "the field does not contain what was typed",
            })),
            ..ChromeResponse::default()
        };
        let env = text_input_ok_envelope("type", &base, resp);
        assert!(!env.ok);
        assert_eq!(env.kind, OutKind::Error);
        assert_eq!(
            env.payload["warning"],
            json!("the field does not contain what was typed")
        );
    }

    #[test]
    fn text_input_ok_envelope_warning_at_top_level_is_error() {
        let base = json!({ "key": "Enter" });
        let resp = ChromeResponse {
            success: Some(true),
            data: Some(json!({})),
            extra: serde_json::Map::from_iter([("warning".to_string(), json!("no key listeners"))]),
            ..ChromeResponse::default()
        };
        let env = text_input_ok_envelope("press", &base, resp);
        assert!(!env.ok);
        assert_eq!(env.kind, OutKind::Error);
        assert_eq!(env.payload["warning"], json!("no key listeners"));
    }

    #[test]
    fn recognized_action_falls_back_to_usage() {
        assert_eq!(
            recognized_action(&["open".into(), "https://x".into()]),
            "open"
        );
        assert_eq!(recognized_action(&["bogus".into()]), "usage");
        assert_eq!(
            recognized_action(&["--session".into(), "s".into(), "count".into(), ".x".into()]),
            "count"
        );
        assert_eq!(
            recognized_action(&["--session=s".into(), "count".into(), ".x".into()]),
            "count"
        );
    }

    #[test]
    fn top_help_lists_every_cli_action_with_purpose() {
        let help = top_help();
        for d in actions::ACTIONS.iter().filter(|a| a.cli.is_some()) {
            assert!(help.contains(d.name), "top help missing action {}", d.name);
            assert!(
                help.contains(d.purpose),
                "top help missing purpose for {}",
                d.name
            );
        }
    }

    #[test]
    fn action_help_covers_flags_kinds_and_examples() {
        for d in actions::ACTIONS.iter().filter(|a| a.cli.is_some()) {
            let cli = d.cli.as_ref().expect("filtered to cli actions");
            let help = action_help(d.name);
            assert!(help.contains("Usage:"), "{}: missing Usage", d.name);
            assert!(help.contains("Kinds"), "{}: missing Kinds", d.name);
            assert!(help.contains("Examples:"), "{}: missing Examples", d.name);
            for k in cli.kinds {
                assert!(
                    help.contains(&format!("{} ({})", k.as_str(), k.exit_code())),
                    "{}: missing kind {}",
                    d.name,
                    k.as_str()
                );
            }
        }
        assert!(action_help("open").contains("--expect"));
        assert!(action_help("open").contains("redesign"));
        assert!(!action_help("status").contains("Flags:"));
    }

    #[test]
    fn action_flags_match_the_cli_help_flag_sets() {
        // ACTION_FLAGS must cover exactly the CLI action words.
        let mut table: Vec<&str> = ACTION_FLAGS.iter().map(|(name, ..)| *name).collect();
        table.sort_unstable();
        let mut cli: Vec<&str> = actions::ACTIONS
            .iter()
            .filter(|a| a.cli.is_some())
            .map(|a| a.name)
            .collect();
        cli.sort_unstable();
        assert_eq!(table, cli, "ACTION_FLAGS must cover the CLI action words");

        for &(word, value_flags, bool_flags) in ACTION_FLAGS {
            let help = actions::desc(word)
                .and_then(|d| d.cli.as_ref())
                .expect("ACTION_FLAGS word should have a CliHelp");
            // Machine name of each declared flag: `--expect <sel>` → `expect`,
            // `--structural` → `structural`.
            let mut expected: Vec<&str> = help
                .flags
                .iter()
                .map(|(flag, _)| {
                    flag.strip_prefix("--")
                        .unwrap_or(flag)
                        .split_whitespace()
                        .next()
                        .unwrap_or(flag)
                })
                .collect();
            let mut actual: Vec<&str> = value_flags
                .iter()
                .chain(bool_flags.iter())
                .copied()
                .collect();
            if help.session {
                // The global `--session` flag is rendered for and accepted by
                // every session-enabled action, so it is on both sides.
                expected.push("session");
                actual.push("session");
            }
            expected.sort_unstable();
            actual.sort_unstable();
            assert_eq!(
                actual, expected,
                "flag set mismatch for action {word}: ACTION_FLAGS vs CliHelp"
            );
        }
    }

    #[test]
    fn action_help_request_intercepts_per_action_help() {
        let arg = |s: &[&str]| s.iter().copied().map(String::from).collect::<Vec<_>>();
        assert_eq!(action_help_request(&arg(&["open", "-h"])), Some("open"));
        assert_eq!(
            action_help_request(&arg(&["open", "https://x", "--help"])),
            Some("open")
        );
        assert_eq!(
            action_help_request(&arg(&["--session", "docs", "open", "-h"])),
            Some("open")
        );
        assert_eq!(action_help_request(&arg(&["open"])), None);
        assert_eq!(action_help_request(&arg(&["snapshot", "-h"])), None);
        assert_eq!(action_help_request(&arg(&["frobnicate", "-h"])), None);
        assert_eq!(action_help_request(&arg(&["--timeout", "5"])), None);
    }

    #[test]
    fn out_env_kind_contract_is_enforced() {
        // Every declared kind satisfies the contract.
        for d in actions::ACTIONS.iter().filter(|a| a.cli.is_some()) {
            let cli = d.cli.as_ref().expect("filtered above");
            for k in cli.kinds {
                assert!(
                    kind_contract_holds(d.name, *k),
                    "{}: declared kind {} fails the contract",
                    d.name,
                    k.as_str()
                );
            }
        }
        // An undeclared kind for a registered action violates it.
        assert!(!kind_contract_holds("status", OutKind::Network));
        // Unregistered pseudo-actions ("usage") are exempt.
        assert!(kind_contract_holds("usage", OutKind::Usage));
    }
}