mind-cli 0.43.0

A manager for agent tooling (skills, agents, rules, tools) that melds arbitrary git repos and links items into your agent directories.
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
//! `mind evolve` -- update the `mind` binary itself in place.
//!
//! The download, verification, extraction, and in-place swap are the
//! `self_update` crate's, driven through its github backend so the artifact
//! selection matches what `resources/install.sh` and the Homebrew formula
//! resolve (`mind-<version>-<target>.tar.gz`). What lives here is everything
//! around that: the platform triple, the up-to-date/downgrade decision, the
//! managed-policy check, the confirmation prompt, the `--json` result, and the
//! build-provenance gate wired in as the crate's pre-extraction archive hook.
//!
//! The decision logic (target triple, version comparison, report text) is pure,
//! so it is unit-testable with no network access. Only `run`'s tail, and the
//! updater it builds, touch the network.

use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

use self_update::backends::github;

use crate::error::{MindError, Result};
use crate::mindfile::version_at_least;

const REPO_OWNER: &str = "jaemk";
const REPO_NAME: &str = "mind";
const REPO: &str = "jaemk/mind";

/// The release asset carrying the published digests (STO-47).
const SHA256SUMS_ASSET: &str = "SHA256SUMS";

/// Whether the running binary needs replacing.
#[derive(Debug, PartialEq, Eq)]
pub enum Decision {
    /// The running version already satisfies the target; nothing to do.
    UpToDate,
    /// The target is newer than the running version; replace the binary.
    Update,
    /// An explicit `--version` was pinned strictly BELOW the running version.
    /// We refuse to downgrade but report why rather than silently saying "up to date".
    // spec: CLI-147
    PinnedBelowCurrent,
}

/// Map an OS/arch pair to its release target triple, rejecting platforms with no
/// published artifact. Mirrors install.sh, which maps the same pairs and rejects
/// any other OS/arch combination.
pub fn target_triple(os: &str, arch: &str) -> Result<&'static str> {
    match (os, arch) {
        // Linux resolves to the statically linked musl artifact, matching
        // resources/install.sh: the gnu build carries the glibc floor of the
        // runner that produced it, so it fails to start on older
        // distributions after the download has already reported success.
        ("linux", "x86_64") => Ok("x86_64-unknown-linux-musl"),
        ("linux", "aarch64") => Ok("aarch64-unknown-linux-musl"),
        ("macos", "aarch64") => Ok("aarch64-apple-darwin"),
        // spec: STO-83
        ("macos", "x86_64") => Ok("x86_64-apple-darwin"),
        _ => Err(MindError::UnsupportedPlatform {
            os: os.to_string(),
            arch: arch.to_string(),
        }),
    }
}

/// Decide whether the running binary needs replacing.
///
/// - `explicit` is true when the caller supplied an explicit `--version` flag
///   (rather than resolving the latest release from the network).
///
/// When `explicit` is true and the pinned `target` is STRICTLY below `current`,
/// returns `PinnedBelowCurrent` instead of `UpToDate` so the caller can emit a
/// clear "not downgrading" message (CLI-147) rather than a misleading "up to date".
/// When the target equals the running version, `UpToDate` is always returned,
/// regardless of `explicit`. When the target is above `current`, `Update` is
/// returned regardless of `explicit`. When `current` and `target` share the
/// same NUMERIC base version (a "tie") but differ in their prerelease suffix
/// (or exactly one of them carries one), the tie is broken by prerelease
/// precedence -- see [`tie_break`] -- rather than assumed `UpToDate` (STO-77).
// spec: CLI-140 STO-77
pub fn decision(current: &str, target: &str, explicit: bool) -> Decision {
    if version_at_least(current, target) {
        if version_at_least(target, current) {
            // Both directions hold: current and target share the same
            // numeric base version. A byte-identical match aside, this is
            // NOT necessarily "up to date" -- a prerelease predates its base
            // release, and two prereleases of the same base order against
            // each other by semver precedence, so hand off to `tie_break`
            // rather than assuming `UpToDate` outright.
            return tie_break(current, target, explicit);
        }
        // current is STRICTLY above target numerically.
        if explicit {
            // target < current: explicit downgrade request we refuse.
            Decision::PinnedBelowCurrent
        } else {
            Decision::UpToDate
        }
    } else {
        Decision::Update
    }
}

/// Resolve a NUMERIC TIE between `current` and `target` (the same dotted base
/// version, as already established by the caller via `version_at_least` in
/// both directions) into a `Decision`, breaking the tie by prerelease
/// precedence instead of assuming `UpToDate` outright (STO-77).
///
/// Before this existed, `decision` only ever moved off a numeric tie in ONE
/// direction: a prerelease `current` onto its own (non-prerelease) base
/// `target`. Two other same-base pairings silently fell through to
/// `UpToDate`/no-op even under an explicit `--to`: two prereleases of the
/// same base ordered against each other (e.g. running `0.24.0-rc1`, pinning
/// `--to 0.24.0-rc2` -- the main reason to pin an rc at all), and a release
/// `current` explicitly pinned onto a same-base prerelease `target` (e.g.
/// running the released `0.24.0`, `--to 0.24.0-rc1`). Both are handled here.
///
/// - Byte-identical `current`/`target` strings are always `UpToDate`.
/// - A prerelease `current` vs. a plain-release `target` of the same base:
///   the prerelease predates the release, so `Update` (unconditional on
///   `explicit`, matching the pre-existing behavior this generalizes).
/// - A plain-release `current` vs. a same-base prerelease `target`: the
///   prerelease predates `current`, so an EXPLICIT pin here is a downgrade
///   request (`PinnedBelowCurrent`); a non-explicit target never reaches this
///   arm in practice (`releases/latest` never returns a prerelease), so it
///   degrades to `UpToDate` rather than a spurious refusal if it ever did.
/// - Two prereleases of the same base: compared via [`prerelease_cmp`]
///   (semver precedence). `target` ordering ABOVE `current` is `Update`;
///   BELOW is an explicit downgrade (`PinnedBelowCurrent`) or a non-explicit
///   `UpToDate`; equal precedence (but non-identical strings, e.g. differing
///   only in build metadata) is `UpToDate`.
fn tie_break(current: &str, target: &str, explicit: bool) -> Decision {
    if current == target {
        return Decision::UpToDate;
    }
    match (is_prerelease(current), is_prerelease(target)) {
        (false, false) => Decision::UpToDate,
        (true, false) => Decision::Update,
        (false, true) => {
            if explicit {
                Decision::PinnedBelowCurrent
            } else {
                Decision::UpToDate
            }
        }
        (true, true) => {
            let cp = prerelease_suffix(current).unwrap_or_default();
            let tp = prerelease_suffix(target).unwrap_or_default();
            match prerelease_cmp(cp, tp) {
                std::cmp::Ordering::Less => Decision::Update,
                std::cmp::Ordering::Equal => Decision::UpToDate,
                std::cmp::Ordering::Greater => {
                    if explicit {
                        Decision::PinnedBelowCurrent
                    } else {
                        Decision::UpToDate
                    }
                }
            }
        }
    }
}

/// Whether `v` carries a prerelease suffix (a `-` segment, e.g. `-dev` or
/// `-rc.2`, such as a fork or packager might append). Build metadata (`+...`)
/// is not a prerelease. Used by [`tie_break`] to classify a numeric-tie pair
/// before ordering it.
fn is_prerelease(v: &str) -> bool {
    v.split_once('+').map_or(v, |(base, _)| base).contains('-')
}

/// The prerelease suffix of `v` (the text after its first `-`, before any
/// `+build` segment), if any. `None` when `v` carries no prerelease.
///
/// Whole-string build-metadata stripping happens first (mirroring
/// `version_at_least`'s rationale): a `+` inside a dotted build-metadata
/// segment must never be mistaken for part of the prerelease suffix.
fn prerelease_suffix(v: &str) -> Option<&str> {
    let base_and_pre = v.split_once('+').map_or(v, |(base, _)| base);
    base_and_pre.split_once('-').map(|(_, pre)| pre)
}

/// Compare two semver-shaped dot-identifiers by semver precedence (informally
/// mirroring semver.org's precedence rule #11): a purely-numeric identifier
/// (all ASCII digits) compares numerically; anything else compares as ASCII
/// bytes; a numeric identifier ALWAYS has lower precedence than a
/// non-numeric one when the two are compared against each other (so `"9"` <
/// `"rc"`, not just `"9"` < `"9a"`).
fn semver_identifier_cmp(a: &str, b: &str) -> std::cmp::Ordering {
    let a_numeric = !a.is_empty() && a.bytes().all(|c| c.is_ascii_digit());
    let b_numeric = !b.is_empty() && b.bytes().all(|c| c.is_ascii_digit());
    match (a_numeric, b_numeric) {
        (true, true) => {
            // Values reaching here are already validated by
            // `is_plausible_release_tag`, but parse defensively (u128, with a
            // MAX fallback) rather than panicking on an implausibly long
            // numeric identifier.
            let an: u128 = a.parse().unwrap_or(u128::MAX);
            let bn: u128 = b.parse().unwrap_or(u128::MAX);
            an.cmp(&bn)
        }
        (true, false) => std::cmp::Ordering::Less,
        (false, true) => std::cmp::Ordering::Greater,
        (false, false) => a.cmp(b),
    }
}

/// Compare two prerelease suffixes (the text after `-`, before any `+build`)
/// by semver precedence: dot-separated identifiers compared pairwise via
/// [`semver_identifier_cmp`]; when one is a strict prefix of the other in
/// dot-identifiers (fewer fields), the SHORTER one has lower precedence
/// (`rc.1` < `rc.1.2`), matching semver.org's precedence rule #11.
fn prerelease_cmp(a: &str, b: &str) -> std::cmp::Ordering {
    let mut ai = a.split('.');
    let mut bi = b.split('.');
    loop {
        match (ai.next(), bi.next()) {
            (None, None) => return std::cmp::Ordering::Equal,
            (None, Some(_)) => return std::cmp::Ordering::Less,
            (Some(_), None) => return std::cmp::Ordering::Greater,
            (Some(x), Some(y)) => {
                let c = semver_identifier_cmp(x, y);
                if c != std::cmp::Ordering::Equal {
                    return c;
                }
            }
        }
    }
}

/// The one-line status `--check` (and the run path) reports: the running version,
/// the target, and whether an update is pending. Pure so it is unit-testable
/// without touching the network.
///
/// `triple` is the resolved release target triple (`target_triple`) -- the exact
/// artifact `evolve` would fetch. It is appended as a trailing `-- target
/// <triple>` clause so the wording of each existing branch stays byte-for-byte
/// intact as a prefix (STO-65): the release artifacts recently changed from gnu
/// to musl on Linux, so surfacing which artifact is about to be downloaded,
/// before anything is fetched, lets a stale-glibc concern be caught at `--check`
/// time instead of after the swap.
// spec: CLI-141 STO-65
fn check_report(current: &str, target: &str, decision: &Decision, triple: &str) -> String {
    match decision {
        Decision::UpToDate => {
            format!("mind {current} is up to date (latest is {target}) -- target {triple}")
        }
        Decision::Update => {
            format!(
                "mind {current} -> {target} available; run `mind evolve` to update -- target {triple}"
            )
        }
        // spec: CLI-147
        Decision::PinnedBelowCurrent => {
            format!(
                "pinned {target} is below the running {current}; not downgrading -- target {triple}"
            )
        }
    }
}

/// Consult the managed policy for the self-update control (POL-51..POL-54).
///
/// Returns:
/// - `Ok(None)` when the policy allows `evolve` to any version (no pin).
/// - `Ok(Some(pin))` when the policy pins to a specific version (use as `--to`).
/// - `Err(SelfUpdatePolicy)` when `evolve` is disabled (POL-52) or when
///   `user_version` conflicts with the pin (POL-53).
///
/// Pure: no network call. `user_version` is the raw `--to` argument (`--version`
/// is a hidden deprecated alias for the same value; may have a leading `v`,
/// which is stripped before comparison).
pub(crate) fn check_policy_for_evolve(
    policy: Option<&crate::policy::Policy>,
    user_version: Option<&str>,
) -> Result<Option<String>> {
    use crate::policy::SelfUpdateControl;
    let Some(pol) = policy else {
        return Ok(None);
    };
    match pol.self_update_control() {
        SelfUpdateControl::Allowed => Ok(None),
        SelfUpdateControl::Disabled => Err(MindError::SelfUpdatePolicy {
            detail: "self-update is disabled by the managed policy".to_string(),
        }),
        SelfUpdateControl::Pinned(pin) => {
            if let Some(uv) = user_version {
                let uv_clean = uv.strip_prefix('v').unwrap_or(uv);
                if uv_clean != pin {
                    return Err(MindError::SelfUpdatePolicy {
                        detail: format!(
                            "managed policy pins self-update to {pin}; \
                             --to {uv_clean} conflicts with the pin"
                        ),
                    });
                }
            }
            Ok(Some(pin.clone()))
        }
    }
}

/// Whether `exe`, the canonicalized path of the running binary, lives in a
/// Homebrew Cellar (CLI-243). Homebrew installs every formula under
/// `<prefix>/Cellar/<formula>/<version>/` and links it into `<prefix>/bin`, so a
/// `Cellar` component in the resolved path means brew owns the binary.
pub(crate) fn is_homebrew_install(exe: &Path) -> bool {
    exe.components().any(|c| c.as_os_str() == "Cellar")
}

/// The running binary's path when Homebrew manages it, else `None`. The path is
/// canonicalized first so the `<prefix>/bin/mind` symlink resolves to its Cellar
/// target.
fn homebrew_exe() -> Option<std::path::PathBuf> {
    let exe = std::env::current_exe().ok()?;
    let exe = std::fs::canonicalize(&exe).unwrap_or(exe);
    is_homebrew_install(&exe).then_some(exe)
}

/// `mind evolve [--check] [--yes] [--version <v>]` — update the running binary.
///
/// `--version` resolves the target WITHOUT any network call, so
/// `evolve --check --version <v>` is fully offline. With no `--version`, the
/// latest release is fetched from the GitHub API. `--check` reports the decision
/// and returns without downloading. Otherwise, unless `--yes`, it prompts before
/// replacing the binary.
pub fn run(check: bool, yes: bool, mut version: Option<String>) -> Result<()> {
    let current = env!("CARGO_PKG_VERSION");
    let os = std::env::consts::OS;
    let arch = std::env::consts::ARCH;
    // Resolve (and validate) the platform target up front, so an unsupported
    // platform fails before any network call.
    let target = target_triple(os, arch)?;

    // Load the managed policy and check the self-update control before any network
    // call (POL-51..POL-54). A machine with no policy file behaves exactly as today.
    let policy = crate::policy::Policy::load()?;
    let policy_pin_active =
        if let Some(pin) = check_policy_for_evolve(policy.as_ref(), version.as_deref())? {
            // Policy pins to a specific version; behave as if --version <pin> was passed.
            version = Some(pin);
            true
        } else {
            false
        };

    // spec: CLI-243 -- a Homebrew-managed binary is brew's to replace. Refuse the
    // swap before any network call; `--check` still reports, with a pointer at
    // `brew upgrade`.
    let brew_exe = homebrew_exe();
    if let (Some(exe), false) = (&brew_exe, check) {
        return Err(MindError::HomebrewManaged {
            path: exe.display().to_string(),
        });
    }

    // Resolve the target version: an explicit --to (or a policy pin) bypasses the
    // network entirely; otherwise the latest published release is read through an
    // updater built for the lookup alone. Either way the resolved value is
    // validated before anything uses it (STO-76, STO-77).
    let explicit = version.is_some();
    let target_version = match version.as_deref() {
        Some(v) => validated_target(v)?,
        None => {
            // spec: PRG-4
            let _step = crate::progress::step(LOOKUP_STEP);
            validated_target(&latest_version(&updater(target, None, lookup_timeout())?)?)?
        }
    };

    let d = decision(current, &target_version, explicit);
    let out = crate::render::ctx();

    if check {
        // CLI-141: report and change nothing, without downloading.
        if out.json {
            let outcome = match d {
                Decision::UpToDate => "up-to-date",
                Decision::Update => "available",
                Decision::PinnedBelowCurrent => "not-downgrading",
            };
            return print_evolve_json(&target_version, outcome, target);
        }
        let marker = match d {
            Decision::UpToDate | Decision::PinnedBelowCurrent => out.ok(),
            Decision::Update => out.warn(),
        };
        println!(
            "{marker} {}",
            check_report(current, &target_version, &d, target)
        );
        if brew_exe.is_some() && matches!(d, Decision::Update) {
            println!("note: this mind is managed by Homebrew; update it with `brew upgrade mind`");
        }
        // spec: POL-66 -- when running is above the policy pin, warn that the pin is
        // an upper bound and does not downgrade. Human mode only; --json already
        // returned above.
        if matches!(d, Decision::PinnedBelowCurrent) && policy_pin_active {
            println!(
                "warning: running {current} differs from the managed policy pin \
                 {target_version}; the policy pin is an upper bound and does not downgrade"
            );
        }
        return Ok(());
    }

    match d {
        Decision::UpToDate => {
            if out.json {
                return print_evolve_json(&target_version, "up-to-date", target);
            }
            println!("{} mind {current} is already up to date", out.ok());
            return Ok(());
        }
        // spec: CLI-147 -- explicit pin below running version: report and exit 0,
        // do NOT download or replace the binary.
        Decision::PinnedBelowCurrent => {
            if out.json {
                return print_evolve_json(&target_version, "not-downgrading", target);
            }
            println!(
                "{} {}",
                out.ok(),
                check_report(current, &target_version, &d, target)
            );
            // spec: POL-66 -- when running is above the policy pin, warn that the pin
            // is an upper bound and does not downgrade. Human mode only; --json already
            // returned above.
            if policy_pin_active {
                println!(
                    "warning: running {current} differs from the managed policy pin \
                     {target_version}; the policy pin is an upper bound and does not downgrade"
                );
            }
            return Ok(());
        }
        Decision::Update => {}
    }

    if !yes {
        // spec: LIFE-45 -- B1: `--json` is non-interactive, mirroring DEP-60: a
        // non-TTY run or a `--json` run without `--yes` refuses rather than
        // swapping the binary unprompted.
        if !crate::hook::is_tty() || out.json {
            return Err(MindError::ConfirmationRequired {
                action: format!("updating mind to {target_version}"),
            });
        }
        if !crate::commands::confirm(&format!("update mind to {target_version}?"))? {
            println!("aborted; nothing changed");
            return Ok(());
        }
    }

    // Pin the updater to the version that was decided on and confirmed, so the
    // release that gets installed is that one even if a newer one is published in
    // between.
    let upd = updater(target, Some(&target_version), download_timeout())?;
    download_and_swap(upd, current, &target_version, target)
}

/// Emit the structured `evolve` result (CLI-153) under `--json`.
///
/// `target_triple` adds the `target_triple` key (STO-65) alongside the existing
/// `action`/`target`/`outcome` keys, which are never renamed: `--json` consumers
/// already depend on those names.
fn print_evolve_json(version: &str, outcome: &str, target_triple: &str) -> Result<()> {
    crate::render::print_json(&serde_json::json!({
        "action": "evolve",
        "target": version,
        "target_triple": target_triple,
        "outcome": outcome,
    }))
}

/// The result of a soft build-provenance verification attempt (STO-66).
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum AttestationOutcome {
    /// `gh attestation verify` succeeded: the archive matches a valid, signed
    /// build-provenance attestation published by the release repo.
    Verified,
    /// `gh` could not even attempt the verification -- an old `gh` with no
    /// `attestation` subcommand, or a network-level failure reaching GitHub --
    /// which is not a statement about the artifact itself. Carries the
    /// (sanitized) reason for the human-readable note.
    ToolingError(String),
    /// `gh` ran the check and reported the artifact does NOT verify: no
    /// matching attestation, a signer/repo mismatch, or an explicit signature
    /// failure. Deliberately fail-closed (see `is_gh_tooling_error`):
    /// "no attestations found" is `gh`'s output both for "this release
    /// predates build-provenance attestations" and for "this artifact was
    /// substituted", and the two are not distinguishable from its output, so
    /// both must abort rather than silently trusting an unverified artifact.
    GenuineFailure(String),
}

/// The `gh` argv that verifies a downloaded archive's build-provenance
/// attestation against the release repo (STO-66), matching
/// `resources/install.sh`'s `gh attestation verify "$tmp/$asset" --repo "$REPO"`.
/// Pure (no I/O) so it is unit-testable without spawning a process.
pub(crate) fn gh_attestation_verify_args(archive_path: &str, repo: &str) -> Vec<String> {
    vec![
        "attestation".into(),
        "verify".into(),
        archive_path.into(),
        "--repo".into(),
        repo.into(),
    ]
}

/// Classify `gh attestation verify`'s (sanitized) stderr as a TOOLING problem --
/// `gh` itself could not run the check -- rather than a verification result
/// (STO-66).
///
/// Deliberately narrow: anything not matched here is treated as a genuine
/// verification failure and aborts the swap. The interesting failure mode (a
/// substituted artifact has no valid attestation) surfaces through the exact
/// same "no attestations found" / HTTP-404 wording that `gh` also uses for a
/// merely-absent attestation (confirmed empirically: querying a real, unmodified
/// artifact's digest under the wrong repo, and querying a tampered artifact's
/// digest under the correct repo, both produce the same class of "not found"
/// message). There is no reliable way to tell those two cases apart from `gh`'s
/// output, so the ambiguity fails CLOSED here rather than silently passing an
/// unverifiable artifact through.
pub(crate) fn is_gh_tooling_error(stderr: &str) -> bool {
    let lower = stderr.to_ascii_lowercase();
    const TOOLING_MARKERS: &[&str] = &[
        // Old `gh` with no `attestation verify` subcommand/flag.
        "unknown command",
        "unknown flag",
        // Network-level failures reaching the GitHub API -- `gh` never got an
        // answer to verify against, so this says nothing about the artifact.
        "dial tcp",
        "no such host",
        "connection refused",
        "i/o timeout",
        "context deadline exceeded",
        "tls handshake",
        "certificate signed by unknown authority",
        // Auth required (e.g. a private repo, or an org policy) -- `gh` could
        // not even ask the question.
        "gh auth login",
    ];
    TOOLING_MARKERS.iter().any(|m| lower.contains(m))
}

/// Soft-verify a downloaded archive's build-provenance attestation (STO-66),
/// mirroring `resources/install.sh`'s `gh attestation verify` step.
///
/// `gh_cmd` is the command name to invoke: `"gh"` in production. Tests point it
/// at a name that resolves to nothing (simulating "`gh` absent" deterministically,
/// without needing to hide the real `gh` from PATH) or at a fake script placed
/// first on PATH (simulating a specific `gh` outcome), mirroring the fake-curl
/// pattern used elsewhere in this file.
///
/// Returns `None` when `gh_cmd` is not on PATH: evolve proceeds silently,
/// exactly like install.sh's `if command -v gh` gate (no note printed for a
/// plain absence, only for a `gh` that ran but could not complete the check).
fn attestation_step(gh_cmd: &str, archive: &Path) -> Option<AttestationOutcome> {
    if !have(gh_cmd) {
        return None;
    }
    let archive_str = archive.to_string_lossy();
    let args = gh_attestation_verify_args(&archive_str, REPO);
    let output = match Command::new(gh_cmd).args(&args).output() {
        Ok(o) => o,
        // A spawn failure after `have()` reported present (e.g. a TOCTOU race,
        // or a `gh` that is on PATH but not executable) is itself a tooling
        // problem, not a statement about the artifact.
        Err(e) => return Some(AttestationOutcome::ToolingError(e.to_string())),
    };
    if output.status.success() {
        return Some(AttestationOutcome::Verified);
    }
    let stderr = crate::sanitize::strip_ansi(String::from_utf8_lossy(&output.stderr).trim());
    if is_gh_tooling_error(&stderr) {
        Some(AttestationOutcome::ToolingError(stderr))
    } else {
        Some(AttestationOutcome::GenuineFailure(stderr))
    }
}

/// The github updater for the `mind` release repo.
///
/// Everything the crate would drive itself is turned off: `evolve` prints its own
/// progress lines and runs its own `[y/N]` prompt (CLI-141) after its own
/// decision (CLI-140, STO-77), so the crate's release-status block and
/// confirmation are suppressed. What it *is* used for is the transport, the
/// asset selection, the verification gates, and the swap.
///
/// `tag` pins a release (`--to`, or a managed-policy pin); `None` leaves the
/// updater on the latest release, which is what `latest_version` reads.
///
/// - `target` is the resolved triple (STO-65), so the musl artifact is selected
///   on linux exactly as `resources/install.sh` does.
/// - `checksum_from_asset("SHA256SUMS")` is STO-47: the digests published beside
///   the release are fetched and the entry for the selected artifact is verified
///   before anything is extracted.
/// - `verify_archive` is STO-66: `gh attestation verify` over the downloaded
///   archive, which is the file the release workflow attests.
/// - `check_install_path_writable(true)` moves the "cannot replace this binary"
///   failure (STO-45) ahead of the download instead of after it.
/// - `timeout` bounds each request the updater makes. It is a WHOLE-request
///   budget, body included, not a connect timeout, so the two callers pass
///   different values: see `lookup_timeout` and `download_timeout` (STO-52).
fn updater(
    target: &str,
    tag: Option<&str>,
    timeout: std::time::Duration,
) -> Result<github::Update> {
    let mut builder = github::Update::configure();
    builder
        .repo_owner(REPO_OWNER)
        .repo_name(REPO_NAME)
        .bin_name("mind")
        .target(target)
        .current_version(env!("CARGO_PKG_VERSION"))
        // GH_TOKEN / GITHUB_TOKEN when set, which lifts GitHub's 60/hour
        // per-IP anonymous limit; absent, the request goes out unauthenticated.
        .auth_token_from_env()
        // spec: STO-52
        .timeout(timeout)
        .no_confirm(true)
        .show_output(false)
        // spec: PRG-4 -- the download is a progress step like every other
        // network step, so the crate's own download bar stays off.
        .show_download_progress(false)
        .check_install_path_writable(true)
        // spec: STO-47
        .checksum_from_asset(SHA256SUMS_ASSET)
        // spec: STO-66
        .verify_archive(|archive: &Path| attestation_gate(archive));
    if let Some(tag) = tag {
        builder.release_tag(format!("v{tag}"));
    }
    builder.build().map_err(map_update_error)
}

/// Validate a resolved target version and normalize it (one leading `v` off).
///
/// The value reaches here either from an explicit `--to` (or a managed-policy
/// pin) or from the release API's `tag_name`, and is validated the same way in
/// both cases, immediately on resolution and before anything downstream uses it:
/// the release-tag lookup it drives, the confirmation prompt it is echoed into,
/// and the `--json` result.
///
/// spec: STO-76 -- a value carrying path segments (a repo/release takeover, a
/// TLS-intercepting proxy, or a `--to` copied from a malicious "run these steps"
/// doc) would otherwise be interpolated into a request path and shown to the
/// user as the version they are approving.
///
/// This uses `is_plausible_release_tag`, NOT `is_plausible_version`: the two
/// answer different questions (see `is_plausible_release_tag`'s doc). A release
/// tag legitimately carries a semver prerelease/build suffix (`evolve --to
/// 1.2.3-rc1` is the only way to reach a prerelease, since `releases/latest`
/// never surfaces one), so the validator accepts that shape while still
/// rejecting anything that could escape or split a URL path segment.
///
/// spec: STO-77 -- a rejected value is `SelfUpdateInvalidTarget`, NOT
/// `SelfUpdatePolicy`: the latter's JSON `kind` (`self-update-policy`) reads as
/// a managed-policy refusal, which this is not.
///
/// Only ONE leading `v` is stripped, so `--to v1.2.3` behaves identically to
/// `--to 1.2.3` while `--to vv1.2.3` still fails validation.
// spec: STO-76 STO-77
fn validated_target(raw: &str) -> Result<String> {
    let value = raw.strip_prefix('v').unwrap_or(raw).to_string();
    if !crate::mindfile::is_plausible_release_tag(&value) {
        return Err(MindError::SelfUpdateInvalidTarget { value });
    }
    Ok(value)
}

/// The version of the latest published release (the `releases/latest` endpoint,
/// which never surfaces a prerelease), with the leading `v` already stripped.
fn latest_version(upd: &github::Update) -> Result<String> {
    let releases = upd.get_latest_release().map_err(map_update_error)?;
    let release = releases.latest().ok_or_else(|| MindError::DownloadFailed {
        url: format!("https://api.github.com/repos/{REPO}/releases/latest"),
        reason: "the release listing is empty".to_string(),
    })?;
    Ok(release.version().to_string())
}

/// STO-66's build-provenance gate, as the crate's pre-extraction archive hook.
///
/// The hook runs on the downloaded archive, which is what
/// `actions/attest-build-provenance` signs (the release workflow's
/// `subject-path` is `mind-*.tar.gz`), so this is the file `gh` has to be
/// pointed at. Absent `gh`, or a `gh` that could not complete the check, this
/// proceeds with at most a note; a genuine verification failure returns `Err`,
/// which aborts with nothing extracted and nothing replaced.
fn attestation_gate(archive: &Path) -> std::result::Result<(), self_update::Error> {
    let out = crate::render::ctx();
    // spec: PRG-4
    let outcome = {
        let _step = crate::progress::step(VERIFY_STEP);
        attestation_step("gh", archive)
    };
    match outcome {
        None => Ok(()),
        Some(AttestationOutcome::Verified) => {
            if !out.json {
                // spec: PRG-5 -- the download step is still live.
                crate::progress::suspend(|| println!("{} build provenance verified", out.ok()));
            }
            Ok(())
        }
        Some(AttestationOutcome::ToolingError(reason)) => {
            if !out.json {
                crate::progress::suspend(|| {
                    println!(
                        "{} build provenance could not be verified ({reason}); continuing",
                        out.warn()
                    )
                });
            }
            Ok(())
        }
        Some(AttestationOutcome::GenuineFailure(reason)) => {
            Err(self_update::Error::archive_verification_rejected(reason))
        }
    }
}

/// Download the release archive, verify it, and replace the running executable.
///
/// Holds the global exclusive lock (STO-46) for the entire download-and-swap
/// step so two concurrent `mind evolve` invocations cannot race. The lock is
/// taken here, after the network-free decision and the prompt, so `evolve
/// --check` and a declined prompt never contend for it (STO-48).
fn download_and_swap(
    upd: github::Update,
    current: &str,
    target_version: &str,
    target_triple: &str,
) -> Result<()> {
    // spec: STO-46 -- hold the exclusive lock for the entire download-and-swap.
    let paths = crate::paths::Paths::resolve()?;
    let mut lock = crate::lock::open(&paths)?;
    let _guard = lock.write()?;

    let out = crate::render::ctx();
    if let Some(line) = download_banner(out.json, target_version, target_triple) {
        println!("{line}");
    }

    {
        // spec: PRG-4 -- the checksum fetch, download, provenance check, and
        // swap; the crate prints nothing itself (`show_output(false)`).
        let _step = crate::progress::step(download_step(target_version));
        upd.update().map_err(map_update_error)?;
    }

    if out.json {
        return print_evolve_json(target_version, "updated", target_triple);
    }
    println!("{} updated mind {current} -> {target_version}", out.ok());
    Ok(())
}

/// The progress step over the latest-release lookup (PRG-4).
const LOOKUP_STEP: &str = "checking for the latest mind release";

/// The progress step over the archive's provenance check (PRG-4).
const VERIFY_STEP: &str = "verifying build provenance";

/// The progress step over the download and swap (PRG-4).
fn download_step(target_version: &str) -> String {
    format!("downloading mind {target_version}")
}

/// The "downloading mind <version>" line, or `None` under `--json`.
///
/// `evolve` writes its own document straight to stdout rather than through
/// `commands.rs`, so it gets none of CLI-217's structural protection: this guard
/// is the only thing keeping a progress line out of `--json` stdout, which has to
/// stay a single machine-readable document. Returning an `Option` rather than
/// printing inline keeps that decision unit-testable without a download.
// spec: CLI-217
fn download_banner(json: bool, target_version: &str, target_triple: &str) -> Option<String> {
    if json {
        return None;
    }
    let out = crate::render::ctx();
    Some(format!(
        "{} downloading mind {target_version} ({})",
        out.bullet(),
        out.dim(&format!("{target_triple} from {REPO}"))
    ))
}

/// Map an updater failure onto mind's structured errors (no stringly-typed
/// errors leak out of this module).
///
/// The mappings that matter are the ones a caller acts on: a rejected
/// attestation (STO-66) and a digest mismatch (STO-47) keep their own variants
/// and exit codes rather than collapsing into a generic download failure, and
/// an unwritable target keeps the actionable "reinstall with privileges"
/// wording (STO-45). Anything else is a download failure carrying the crate's
/// message, sanitized: a hostile endpoint controls parts of it (STO-54).
fn map_update_error(e: self_update::Error) -> MindError {
    let release_url = format!("https://github.com/{REPO}/releases");
    match e {
        // spec: STO-66
        self_update::Error::ArchiveVerificationRejected { reason, .. } => {
            MindError::AttestationVerificationFailed {
                reason: crate::sanitize::strip_ansi(&reason.unwrap_or_default()),
            }
        }
        // spec: STO-47
        self_update::Error::ChecksumMismatch {
            expected, computed, ..
        } => MindError::DigestMismatch {
            url: release_url,
            expected,
            actual: computed,
        },
        // spec: STO-47 -- a release with no usable SHA256SUMS entry is a
        // refusal, not a silently unverified install.
        self_update::Error::ChecksumSourceInvalid { asset, reason, .. } => {
            MindError::DigestMismatch {
                url: release_url,
                expected: format!(
                    "(from {SHA256SUMS_ASSET}: {})",
                    crate::sanitize::strip_ansi(&reason)
                ),
                actual: crate::sanitize::strip_ansi(&asset),
            }
        }
        // spec: STO-45
        self_update::Error::InstallPathNotWritable { path, .. } => MindError::TargetNotWritable {
            path: path.display().to_string(),
        },
        // No asset for this platform's triple in the release.
        self_update::Error::NoReleaseFound { .. } => MindError::ReleaseAssetEmpty,
        // spec: STO-80 -- a spent quota and a rejected credential are the two
        // failures whose fix is a token, so they get the token hint instead of
        // `maybe_proxy_hint`'s transport advice.
        ref rate_limited @ self_update::Error::RateLimited { .. } => {
            auth_refusal(rate_limited, true)
        }
        ref unauthorized @ self_update::Error::Unauthorized { .. } => {
            auth_refusal(unauthorized, false)
        }
        other => MindError::DownloadFailed {
            url: release_url,
            reason: maybe_proxy_hint(&other.to_string()),
        },
    }
}

/// Phrases that identify a TLS trust failure rather than a transport failure
/// (STO-79). Matched case-insensitively against the sanitized reason.
///
/// The first is rustls's own wording, which is what mind's client produces --
/// verified against a local server presenting a cert from an untrusted CA. The
/// rest cover the other spellings a reader is likely to paste into a bug
/// report (OpenSSL's, and the self-signed variant of the same root cause).
const CERT_MARKERS: &[&str] = &[
    "invalid peer certificate",
    "certificate verify",
    "unknownissuer",
    "self-signed certificate",
];

/// Append a setup hint when the failure has a known, actionable cause.
///
/// Two causes, with different fixes, so they get different hints and never both
/// fire: a proxy error means the request could not REACH the endpoint, a
/// certificate error means it reached it and would not TRUST it. Proxy is
/// checked first because a 407 is answered by the proxy before any certificate
/// is presented.
///
/// The text comes from the HTTP client and, through it, from the endpoint,
/// which is untrusted (a MITM'd or hostile endpoint controls those bytes). It
/// is sanitized via `strip_ansi` before being embedded in the returned string
/// (STO-54).
// spec: STO-54, STO-79
fn maybe_proxy_hint(reason: &str) -> String {
    let reason = crate::sanitize::strip_ansi(reason);
    let lower = reason.to_ascii_lowercase();
    if reason.contains("407") || lower.contains("proxy") {
        format!(
            "{reason}\nhint: if you are behind a proxy, set HTTPS_PROXY or HTTP_PROXY \
             (e.g. export HTTPS_PROXY=http://proxy.example.com:8080)"
        )
    } else if CERT_MARKERS.iter().any(|m| lower.contains(m)) {
        // mind verifies against the machine's trust store (STO-78), so the fix
        // is to put the CA there -- not to relax verification, which `evolve`
        // offers no way to do.
        format!(
            "{reason}\nhint: mind verifies TLS against your machine's certificate store. \
             If your network intercepts HTTPS with a company CA, install that CA in the \
             system store, or point mind at a bundle containing it \
             (e.g. export SSL_CERT_FILE=/path/to/corporate-ca.pem)"
        )
    } else {
        reason
    }
}

/// The environment variables the updater's token lookup reads, in its precedence
/// order (STO-57: `gh`'s order, first set and non-empty wins).
///
/// The crate's own list is `pub(crate)`, so it is mirrored here rather than
/// imported. It is only ever used to decide WHICH hint to print -- the lookup
/// that actually authorizes the request is the crate's `auth_token_from_env`.
const AUTH_TOKEN_ENV_VARS: &[&str] = &["GH_TOKEN", "GITHUB_TOKEN"];

/// Whether the environment supplied a token the updater would have sent.
///
/// Matches the crate's emptiness rule (a variable set to whitespace is no token),
/// so the hint does not claim the request was authenticated when it was not.
fn auth_token_in_env() -> bool {
    AUTH_TOKEN_ENV_VARS
        .iter()
        .any(|var| std::env::var(var).is_ok_and(|v| !v.trim().is_empty()))
}

/// The remedy line for a refused request (STO-80), chosen by whether the
/// environment actually supplied a token.
///
/// GitHub's anonymous budget is 60 requests/hour counted per SOURCE IP, so on a
/// NAT'd or proxied corporate egress it is shared with everyone else on that IP
/// and is routinely exhausted by other people entirely. The remedy is a token,
/// and it is named ONLY when none is set: telling someone to set a variable they
/// already set sends them after the wrong thing.
///
/// `rate_limited` splits the two token-set cases, which have opposite fixes: a
/// spent quota on a valid token is a wait, a rejected credential is not.
// spec: STO-80
fn token_hint(rate_limited: bool) -> &'static str {
    match (auth_token_in_env(), rate_limited) {
        (false, _) => {
            "hint: the request went out unauthenticated, and GitHub's anonymous budget of 60 \
             requests/hour is counted per source IP, so a NAT'd or proxied network shares one \
             budget. Set GH_TOKEN or GITHUB_TOKEN for the authenticated 5000/hour budget, e.g. \
             GH_TOKEN=$(gh auth token) mind evolve"
        }
        (true, true) => {
            "hint: GH_TOKEN/GITHUB_TOKEN is set, so this is that token's own budget; retry once \
             it resets"
        }
        (true, false) => {
            "hint: GH_TOKEN/GITHUB_TOKEN is set and was rejected; check that it is valid and not \
             expired (gh auth status)"
        }
    }
}

/// Render a wait in the units a reader thinks in (STO-80).
///
/// A GitHub primary rate limit resets up to an hour out, so the raw value is
/// routinely four digits of seconds -- `2411s` is a number the reader has to do
/// arithmetic on before it means anything.
///
/// At most two units, largest first, so the value stays short and the precision
/// stays proportionate: seconds are dropped once there is an hour to report,
/// where they are noise. A zero component is omitted rather than padded (`1h`,
/// not `1h0m`), and a wait under a minute keeps its seconds.
// spec: STO-80
fn humanize_secs(secs: u64) -> String {
    let (hours, minutes, seconds) = (secs / 3600, (secs % 3600) / 60, secs % 60);
    match (hours, minutes, seconds) {
        (0, 0, s) => format!("{s}s"),
        (0, m, 0) => format!("{m}m"),
        (0, m, s) => format!("{m}m{s}s"),
        (h, 0, _) => format!("{h}h"),
        (h, m, _) => format!("{h}h{m}m"),
    }
}

/// Compose the message for a request the endpoint refused (STO-80).
///
/// The message is built from the error's accessors rather than its `Display`,
/// which appends its own generic "set an auth token to raise the limit" advice:
/// that duplicates `token_hint`'s unauthenticated branch and flatly contradicts
/// its token-set branch, where a token IS set and the fix is to wait.
///
/// `rate_limited` is the caller's variant match, not a re-derivation: the crate
/// already decided (a 429, or a 403 whose headers report a spent quota or a
/// `Retry-After`), and that decision is the whole reason the two variants exist.
///
/// The URL is endpoint-derived, so it is sanitized before it is embedded
/// (STO-54), exactly as in `maybe_proxy_hint`.
// spec: STO-80 STO-54
fn auth_refusal(err: &self_update::Error, rate_limited: bool) -> MindError {
    let what = if rate_limited {
        "rate limited"
    } else {
        "rejected"
    };
    // Both accessors are `Some` for these two variants; the fallbacks keep the
    // message well-formed rather than asserting on the crate's internals.
    let status = err
        .http_status()
        .map(|s| format!("HTTP {s}"))
        .unwrap_or_else(|| "a refusal".to_string());
    let url = crate::sanitize::strip_ansi(err.url().unwrap_or("the GitHub API"));
    // `rate_limit_delay` folds `Retry-After` and the quota reset into one wait,
    // preferring the server's explicit `Retry-After`; it is `None` when neither
    // is known (a bare 429) and on every `Unauthorized`.
    let wait = err
        .rate_limit_delay()
        .map(|d| format!(" (retry in {})", humanize_secs(d.as_secs())))
        .unwrap_or_default();
    MindError::SelfUpdateRefused {
        reason: format!(
            "GitHub {what} this request: {status} from {url}{wait}\n{}",
            token_hint(rate_limited)
        ),
    }
}

/// Per-process counter that makes successive `mktemp_dir` calls within the same
/// process yield distinct paths even when the wall-clock resolution is coarser
/// than the interval between calls.
static MKTEMP_SEQ: AtomicU64 = AtomicU64::new(0);

/// Create an unpredictably-named, exclusively-owned temp directory under a
/// caller-chosen name prefix. The name combines the prefix, the PID, a subsecond
/// wall-clock timestamp, and a per-process sequence number so that:
///
/// - two successive calls within the same process always yield distinct paths
///   (the sequence number), and
/// - the path is hard to predict from outside (the nanos component varies with
///   the exact call time).
///
/// `create_dir` (not `create_dir_all`) gives exclusive-creation semantics: if the
/// directory already exists the call fails rather than silently reusing it, which
/// prevents a local attacker from pre-creating the path.
///
/// The only caller left is `tui::action`'s stdout-capture file (TUI-61); the
/// `evolve` download staging that used to share this is the updater's own
/// `tempfile::TempDir` now.
// spec: TUI-61
pub(crate) fn mktemp_dir_prefixed(prefix: &str) -> Result<std::path::PathBuf> {
    let pid = std::process::id();
    let seq = MKTEMP_SEQ.fetch_add(1, Ordering::Relaxed);
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .subsec_nanos();
    let base = std::env::temp_dir().join(format!("{prefix}-{pid}-{nanos}-{seq}"));
    // Exclusive creation: fails if the path already exists.
    std::fs::create_dir(&base).map_err(|e| MindError::io(&base, e))?;
    // 0700: only the owning process can enter or read the directory.
    #[cfg(unix)]
    std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o700))
        .map_err(|e| MindError::io(&base, e))?;
    Ok(base)
}

/// Clamp the raw parsed value of `MIND_HTTP_TIMEOUT_SECS` to the usable range.
///
/// A value of 0 is treated the same as a missing value and falls back to 15:
/// both `--connect-timeout 0` (curl) and `--timeout=0` (wget) mean "no limit",
/// which silently defeats the intent of the knob (STO-52).
// spec: STO-52
pub(crate) fn clamp_http_timeout(raw: Option<u64>) -> u64 {
    match raw {
        None | Some(0) => 15,
        Some(n) => n,
    }
}

/// Read the connect-timeout from `MIND_HTTP_TIMEOUT_SECS` (STO-52).
/// Falls back to 15 on a missing, non-numeric, or zero value.
fn http_timeout_secs() -> u64 {
    clamp_http_timeout(
        std::env::var("MIND_HTTP_TIMEOUT_SECS")
            .ok()
            .and_then(|v| v.parse::<u64>().ok()),
    )
}

/// The ceiling on the artifact download (STO-52), matching the `--max-time 600`
/// `resources/install.sh` passes to curl.
const DOWNLOAD_TIMEOUT_CEILING_SECS: u64 = 600;

/// The budget for a metadata request (the release lookup, and the `SHA256SUMS`
/// fetch that rides along with the download updater): `MIND_HTTP_TIMEOUT_SECS`,
/// default 15 s.
fn lookup_timeout() -> std::time::Duration {
    std::time::Duration::from_secs(http_timeout_secs())
}

/// The budget for the download (STO-52).
///
/// The updater's timeout is a WHOLE-request budget, body included, not curl's
/// connect timeout, so the 15 s that bounds a metadata request would abort a
/// perfectly healthy multi-megabyte download on a slow link. The download gets
/// the 600 s ceiling instead, and a `MIND_HTTP_TIMEOUT_SECS` set ABOVE that wins
/// (someone raising the knob wants more room, not less).
fn download_timeout() -> std::time::Duration {
    std::time::Duration::from_secs(http_timeout_secs().max(DOWNLOAD_TIMEOUT_CEILING_SECS))
}

/// Whether a command exists on PATH. `command -v` is a shell builtin, not an
/// executable, so it must run inside a shell (`Command::new("command")` would
/// just fail to spawn and report everything as missing).
fn have(cmd: &str) -> bool {
    Command::new("sh")
        .arg("-c")
        .arg(format!("command -v {cmd}"))
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

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

    /// Serializes tests that mutate process-wide env vars (`PATH`,
    /// `GITHUB_TOKEN`/`GH_TOKEN`) so they don't race each other. This is the
    /// crate-wide lock defined in `src/paths.rs` (C18): `set_var`/`remove_var`
    /// soundness is process-wide, not module-wide, and `src/paths.rs` and
    /// `src/commands.rs` mutate their own overlapping set of env vars
    /// (`MIND_AGENT_HOMES`, `MIND_POLICY_FILE`, `MIND_HOME`, `CLAUDE_HOME`, ...)
    /// in the same multithreaded test binary, some via a spawned `git`, which
    /// snapshots `environ`. Using one shared lock here instead of a
    /// module-local one closes that gap.
    use crate::paths::ENV_LOCK;

    #[test]
    // spec: STO-76 STO-77
    fn run_refuses_an_explicit_to_target_with_path_segments_with_no_network_call() {
        // An explicit `--to` value bypasses the network entirely for target
        // resolution (no `latest` lookup), so a malicious value must still be
        // refused purely from local validation -- no PATH stub needed; if
        // `run` somehow tried to shell out despite there being no curl/wget
        // stub on PATH, it would fail with a `DownloadFailed`/`Io` error
        // instead of `SelfUpdateInvalidTarget`, and the assertion below would
        // catch that misbehavior.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let orig_policy_file = std::env::var("MIND_POLICY_FILE").ok();
        // SAFETY: ENV_LOCK is held for the duration of the mutation and the
        // `run()` call below.
        unsafe {
            std::env::remove_var("MIND_POLICY_FILE");
        }

        let result = run(
            true,
            false,
            Some("1/../../../../attacker/mind/releases/download/v1".to_string()),
        );

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            match orig_policy_file {
                Some(v) => std::env::set_var("MIND_POLICY_FILE", v),
                None => std::env::remove_var("MIND_POLICY_FILE"),
            }
        }
        drop(guard);

        match result {
            // spec: STO-77
            Err(MindError::SelfUpdateInvalidTarget { value }) => {
                assert!(
                    value.contains("attacker"),
                    "must name the rejected --to value: {value}"
                );
            }
            other => panic!("expected a SelfUpdateInvalidTarget refusal, got {other:?}"),
        }
    }

    #[test]
    // spec: STO-76 CLI-140
    fn run_accepts_an_explicit_prerelease_to_target_and_reaches_decision_end_to_end() {
        // Before the STO-76 fix reused `is_plausible_version` (digits and dots
        // only) at this site, `evolve --to 1.2.3-rc1` was refused outright --
        // testing a prerelease before promoting it is legitimate, and since
        // GitHub's `releases/latest` never surfaces a prerelease, an explicit
        // `--to` is the only way to reach one. This drives the real `run()`
        // entry path (not `decision()` directly, which would mask a
        // regression at the validation site upstream of it): a prerelease
        // `--to` value must pass the STO-76 validation, reach `decision()`,
        // and (since the target is built numerically ABOVE the running
        // version) come back `Decision::Update`. No network call is possible
        // here at all (no curl/wget stub is put on PATH), so reaching the
        // `!yes` confirmation gate -- rather than a `DownloadFailed`/`Io`
        // error from an attempted shell-out, or a `SelfUpdateInvalidTarget`
        // "not a plausible" refusal -- proves both that validation accepted
        // the value AND that `decision()` classified it as an update,
        // end-to-end through production code.
        //
        // L8: the target is DERIVED from `CARGO_PKG_VERSION` (bumping the
        // major component) rather than hardcoded as `"1.2.3-rc1"`. A
        // hardcoded literal only worked because it happened to sit
        // numerically above this crate's version at the time it was written
        // (0.23.0); once the crate's own version reaches 1.2.3, the same
        // literal ties instead of exceeding it, `decision()` flips outcome,
        // and this test would fail with an unrelated-looking message far
        // from its real cause.
        let current = env!("CARGO_PKG_VERSION");
        let major: u64 = current
            .split(['.', '-', '+'])
            .next()
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        let target = format!("{}.0.0-rc1", major + 1);

        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let orig_policy_file = std::env::var("MIND_POLICY_FILE").ok();
        let orig_tty = std::env::var("MIND_TTY").ok();
        // SAFETY: ENV_LOCK is held for the duration of the mutation and the
        // `run()` call below.
        unsafe {
            std::env::remove_var("MIND_POLICY_FILE");
            std::env::set_var("MIND_TTY", "0");
        }

        let result = run(false, false, Some(target.clone()));

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            match orig_policy_file {
                Some(v) => std::env::set_var("MIND_POLICY_FILE", v),
                None => std::env::remove_var("MIND_POLICY_FILE"),
            }
            match orig_tty {
                Some(v) => std::env::set_var("MIND_TTY", v),
                None => std::env::remove_var("MIND_TTY"),
            }
        }
        drop(guard);

        match result {
            Err(MindError::ConfirmationRequired { action }) => {
                assert!(
                    action.contains(&target),
                    "must be prompting to update to the resolved prerelease target: {action}"
                );
            }
            other => panic!(
                "expected ConfirmationRequired (proving the prerelease target passed \
                 validation and reached an Update decision), got {other:?}"
            ),
        }
    }

    #[test]
    // spec: STO-76 STO-77
    fn run_refuses_traversal_smuggled_inside_an_explicit_prerelease_suffix() {
        // A `-`-prefixed suffix of dots and slashes must not become an escape
        // hatch around the STO-76 URL-path-segment check just because a
        // legitimate prerelease suffix is now accepted. Exercised through the
        // real `run()` entry path with no curl/wget stub on PATH, so any
        // attempt to shell out before validation would surface as a
        // DownloadFailed/Io error instead, which the match below would catch.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let orig_policy_file = std::env::var("MIND_POLICY_FILE").ok();
        // SAFETY: ENV_LOCK is held for the duration of the mutation and the
        // `run()` call below.
        unsafe {
            std::env::remove_var("MIND_POLICY_FILE");
        }

        let result = run(true, false, Some("1.0.0-../..".to_string()));

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            match orig_policy_file {
                Some(v) => std::env::set_var("MIND_POLICY_FILE", v),
                None => std::env::remove_var("MIND_POLICY_FILE"),
            }
        }
        drop(guard);

        match result {
            // spec: STO-77
            Err(MindError::SelfUpdateInvalidTarget { value }) => {
                assert!(
                    value.contains("1.0.0-../.."),
                    "must name the rejected --to value: {value}"
                );
            }
            other => panic!("expected a SelfUpdateInvalidTarget refusal, got {other:?}"),
        }
    }

    #[test]
    // spec: STO-76
    fn run_refuses_a_pure_dot_run_smuggled_inside_an_explicit_prerelease_suffix() {
        // L9: `run_refuses_traversal_smuggled_inside_an_explicit_prerelease_suffix`
        // (above) uses a `/`-carrying payload, which `is_plausible_version`
        // (the OLD, pre-STO-76 validator) also rejects outright -- ANY dash
        // fails that validator, since it is digits-and-dots only. So that
        // test would stay green even if the `run()` call site regressed back
        // to calling `is_plausible_version` instead of
        // `is_plausible_release_tag`, which would ALSO wrongly break every
        // legitimate `--to X-rc1` pin (covered separately by
        // `run_accepts_an_explicit_prerelease_to_target_and_reaches_decision_end_to_end`).
        //
        // This test instead pins the SPLIT ITSELF: a prerelease suffix built
        // from nothing but dots (no slash at all) is exactly the shape a
        // naive "every character is in the allowed charset" reading of
        // `is_plausible_release_tag`'s grammar would wrongly accept (`.` is
        // itself a member of the allowed suffix charset), but the real
        // implementation splits the suffix on `.` and rejects any EMPTY
        // identifier between two dots -- so it is still refused. A validator
        // that dropped that non-empty-identifier check (while still using
        // the right function at the call site) would pass this test's
        // predecessor but fail this one.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let orig_policy_file = std::env::var("MIND_POLICY_FILE").ok();
        // SAFETY: ENV_LOCK is held for the duration of the mutation and the
        // `run()` call below.
        unsafe {
            std::env::remove_var("MIND_POLICY_FILE");
        }

        let result = run(true, false, Some("1.0.0-..".to_string()));

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            match orig_policy_file {
                Some(v) => std::env::set_var("MIND_POLICY_FILE", v),
                None => std::env::remove_var("MIND_POLICY_FILE"),
            }
        }
        drop(guard);

        match result {
            Err(MindError::SelfUpdateInvalidTarget { value }) => {
                assert!(
                    value.contains("1.0.0-.."),
                    "must name the rejected --to value: {value}"
                );
            }
            other => panic!("expected a SelfUpdateInvalidTarget refusal, got {other:?}"),
        }
    }

    #[test]
    // spec: STO-52
    fn http_timeout_zero_clamped_to_default() {
        // MIND_HTTP_TIMEOUT_SECS=0 means "no limit" in both curl (--connect-timeout 0)
        // and wget (--timeout=0), silently defeating the knob. clamp_http_timeout
        // must treat 0 the same as a missing value and return the 15-second default.
        assert_eq!(
            clamp_http_timeout(Some(0)),
            15,
            "a zero value must clamp to the 15-second default"
        );
        assert_eq!(
            clamp_http_timeout(None),
            15,
            "a missing value must default to 15"
        );
        assert_eq!(
            clamp_http_timeout(Some(30)),
            30,
            "a non-zero value must pass through unchanged"
        );
        assert_eq!(
            clamp_http_timeout(Some(1)),
            1,
            "the minimum non-zero value (1) must not be altered"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_strips_ansi_and_bidi_from_reason() {
        // The reason text comes from curl/wget stderr, which a MITM'd or hostile
        // endpoint controls. maybe_proxy_hint must strip ANSI escapes and bidi
        // override characters before embedding the reason in DownloadFailed.reason.
        let ansi_reason = "download error \x1b[31mred\x1b[0m text";
        let result = maybe_proxy_hint(ansi_reason);
        assert!(
            !result.contains('\x1b'),
            "ANSI escape sequences must be stripped from the reason: {result:?}"
        );
        assert!(
            result.contains("download error"),
            "visible text must be preserved after stripping: {result:?}"
        );

        // Bidi override characters (U+202E and siblings) must also be stripped.
        let bidi_reason = "pay \u{202E}oot";
        let result = maybe_proxy_hint(bidi_reason);
        assert!(
            !result.contains('\u{202E}'),
            "bidi override (U+202E) must be stripped: {result:?}"
        );

        // The proxy-hint branch must also produce a sanitized output.
        let hostile_407 = "\x1b[1m407 Proxy Auth Required\x1b[0m \u{202E}spoofed";
        let result = maybe_proxy_hint(hostile_407);
        assert!(
            !result.contains('\x1b'),
            "ANSI must be stripped even in the proxy-hint branch: {result:?}"
        );
        assert!(
            !result.contains('\u{202E}'),
            "bidi must be stripped even in the proxy-hint branch: {result:?}"
        );
        // The hint must still be appended (407 was present after sanitization).
        assert!(
            result.contains("HTTPS_PROXY"),
            "proxy hint must still be appended when 407 is present: {result:?}"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_names_only_settings_that_apply() {
        // The hint must name the environment variables the updater's HTTP client
        // actually reads, and must not point at settings that do nothing for it:
        // git's `http.proxy` never applied, and `~/.curlrc` stopped applying when
        // the downloader stopped being curl.
        let hint = maybe_proxy_hint("Received HTTP code 407 from proxy");
        assert!(
            !hint.contains("http.proxy"),
            "hint must not mention git's http.proxy: {hint:?}"
        );
        assert!(
            !hint.contains("curlrc"),
            "hint must not mention ~/.curlrc, which the updater does not read: {hint:?}"
        );
        assert!(
            hint.contains("HTTPS_PROXY") && hint.contains("HTTP_PROXY"),
            "hint must name HTTPS_PROXY and HTTP_PROXY: {hint:?}"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_407_in_escape_sequence_does_not_trigger_hint() {
        // Adversarial: a hostile endpoint emits an SGR color escape whose numeric
        // parameter is `407` (`ESC [ 4 0 7 m`). strip_ansi removes the whole escape
        // BEFORE the `contains("407")` test, so the digits inside the escape must
        // NOT be left behind to spuriously trigger the proxy hint. The `407` was
        // never real HTTP-407 text; it was an ANSI parameter.
        let colored = "\x1b[407mdownload timed out\x1b[0m";
        let result = maybe_proxy_hint(colored);
        assert!(
            !result.contains('\x1b'),
            "escape must be stripped: {result:?}"
        );
        assert!(
            !result.contains("407"),
            "the `407` ANSI parameter must be stripped, not surface as text: {result:?}"
        );
        assert!(
            !result.contains("HTTPS_PROXY"),
            "an ANSI-parameter 407 must NOT append the proxy hint: {result:?}"
        );
        assert_eq!(
            result, "download timed out",
            "only the visible message survives: {result:?}"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_real_407_split_across_escapes_is_detected() {
        // Adversarial converse: a genuine HTTP-407 message with a color escape
        // spliced into the middle of the digits (`4 ESC[0m 07`). Because strip_ansi
        // runs FIRST, the escape is removed and the digits rejoin into `407`, so the
        // proxy hint IS correctly appended. This pins the ordering: sanitize, then
        // match -- not match, then sanitize (which would miss this).
        let split = "HTTP 4\x1b[0m07 from proxy";
        let result = maybe_proxy_hint(split);
        assert!(
            !result.contains('\x1b'),
            "escape must be stripped: {result:?}"
        );
        assert!(
            result.contains("407"),
            "digits must rejoin into 407 after stripping the interior escape: {result:?}"
        );
        assert!(
            result.contains("HTTPS_PROXY"),
            "a real 407 (revealed after sanitization) must append the proxy hint: {result:?}"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_could_not_resolve_proxy_branch() {
        // The second recognized proxy-failure phrase must also append the hint, and
        // the output must still be sanitized (ANSI stripped) in that branch.
        let reason = "wget: \x1b[31mCould not resolve proxy\x1b[0m: proxy.local";
        let result = maybe_proxy_hint(reason);
        assert!(
            !result.contains('\x1b'),
            "ANSI must be stripped in the resolve-proxy branch: {result:?}"
        );
        assert!(
            result.contains("Could not resolve proxy"),
            "the recognized phrase must survive sanitization: {result:?}"
        );
        assert!(
            result.contains("HTTPS_PROXY"),
            "the resolve-proxy phrase must trigger the proxy hint: {result:?}"
        );
    }

    #[test]
    // spec: STO-54
    fn maybe_proxy_hint_non_proxy_reason_is_returned_verbatim_after_sanitizing() {
        // A non-proxy failure must be returned sanitized but WITHOUT the hint
        // appended, so ordinary download errors are not decorated with proxy advice.
        let reason = "server returned HTTP 500";
        let result = maybe_proxy_hint(reason);
        assert_eq!(
            result, "server returned HTTP 500",
            "a non-proxy reason must pass through unchanged (already ASCII): {result:?}"
        );
        assert!(
            !result.contains("HTTPS_PROXY"),
            "a non-proxy reason must NOT append the proxy hint: {result:?}"
        );
    }

    /// Run `body` with `GH_TOKEN`/`GITHUB_TOKEN` forced to a known state, then
    /// restore whatever the process had. The lock is the crate-wide `ENV_LOCK`
    /// because token state is process-wide (see the `use` above).
    fn with_token_env<T>(token: Option<&str>, body: impl FnOnce() -> T) -> T {
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let saved: Vec<(&str, Option<String>)> = AUTH_TOKEN_ENV_VARS
            .iter()
            .map(|var| (*var, std::env::var(var).ok()))
            .collect();
        // SAFETY: ENV_LOCK is held across every mutation and the body.
        unsafe {
            for (var, _) in &saved {
                std::env::remove_var(var);
            }
            if let Some(value) = token {
                std::env::set_var("GH_TOKEN", value);
            }
        }
        let result = body();
        // SAFETY: ENV_LOCK is still held.
        unsafe {
            for (var, value) in &saved {
                match value {
                    Some(v) => std::env::set_var(var, v),
                    None => std::env::remove_var(var),
                }
            }
        }
        drop(guard);
        result
    }

    /// The API endpoint `evolve`'s release lookup hits, as the crate reports it
    /// back on a refusal.
    const API_URL: &str = "https://api.github.com/repos/jaemk/mind/releases/latest";

    /// A spent-quota 403: what GitHub answers an unauthenticated caller whose
    /// per-IP budget is gone. The reset is `reset_in` seconds out, so the
    /// rendered wait is that value less the sub-second rounding.
    fn spent_quota_403(reset_in: u64) -> self_update::Error {
        let reset = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock after the epoch")
            .as_secs()
            + reset_in;
        let mut headers = self_update::http_client::HeaderMap::new();
        headers.insert("x-ratelimit-remaining", "0".parse().unwrap());
        headers.insert("x-ratelimit-reset", reset.to_string().parse().unwrap());
        self_update::Error::http_status_error_with_headers(403, API_URL, &headers)
    }

    /// GitHub's *secondary* rate limit: a 403 carrying an explicit `Retry-After`
    /// while the primary quota is untouched. Preferred over `spent_quota_403`
    /// wherever a test asserts the rendered wait, because the delay is stated by
    /// the response rather than derived from the clock, so it is exact.
    fn retry_after_403(secs: u64) -> self_update::Error {
        let mut headers = self_update::http_client::HeaderMap::new();
        headers.insert("retry-after", secs.to_string().parse().unwrap());
        self_update::Error::http_status_error_with_headers(403, API_URL, &headers)
    }

    #[test]
    // spec: STO-80
    fn a_refused_request_names_the_token_when_the_environment_supplies_none() {
        // The corporate-network case: GitHub's anonymous budget is per source IP,
        // so a NAT'd egress spends it on other people's requests and `evolve`
        // fails for a user who never ran it before. The remedy is a token, which
        // nothing else in the message names, and it comes with a runnable form --
        // "set GH_TOKEN" alone leaves the user hunting for a value.
        let msg = with_token_env(None, || map_update_error(spent_quota_403(2400))).to_string();
        assert!(
            msg.contains("GH_TOKEN") && msg.contains("GITHUB_TOKEN"),
            "the hint must name both variables the lookup reads: {msg:?}"
        );
        assert!(
            msg.contains("gh auth token"),
            "the hint must give a runnable way to supply a token: {msg:?}"
        );
        assert!(
            msg.contains("per source IP"),
            "the hint must explain why a shared egress exhausts the budget: {msg:?}"
        );
        assert!(
            msg.contains("HTTP 403") && msg.contains(API_URL),
            "the message must name the status and the endpoint that refused: {msg:?}"
        );
        assert!(
            msg.contains("retry in 39m") || msg.contains("retry in 40m"),
            "a known quota reset must be reported as a wait: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn a_reported_wait_is_humanized_not_a_raw_second_count() {
        // The value a reader acts on. A GitHub primary limit resets up to an hour
        // out, so the raw form is four digits of seconds and the reader has to
        // divide before knowing whether to wait or give up.
        let msg = with_token_env(None, || map_update_error(retry_after_403(2411))).to_string();
        assert!(
            msg.contains("(retry in 40m11s)"),
            "the wait must be rendered in minutes and seconds: {msg:?}"
        );
        assert!(
            !msg.contains("2411"),
            "the raw second count must not survive: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn humanize_secs_shows_at_most_two_units_largest_first() {
        // The boundaries, including the ones that decide whether a component is
        // omitted or padded: a zero component is dropped (`1h`, not `1h0m`), and
        // seconds disappear once there is an hour to report, where they are noise
        // against a wait the user is not going to time precisely.
        for (secs, expected) in [
            (0, "0s"),
            (1, "1s"),
            (59, "59s"),
            (60, "1m"),
            (61, "1m1s"),
            (2411, "40m11s"),
            (3599, "59m59s"),
            (3600, "1h"),
            (3601, "1h"),
            (3660, "1h1m"),
            (3661, "1h1m"),
            (7322, "2h2m"),
        ] {
            assert_eq!(
                humanize_secs(secs),
                expected,
                "humanize_secs({secs}) must render as {expected}"
            );
        }
    }

    #[test]
    // spec: STO-80
    fn a_refused_request_does_not_tell_you_to_set_a_token_you_already_set() {
        // With a token in the environment the request WAS authenticated, so the
        // "set GH_TOKEN" advice would send the user after a variable they already
        // have. A rate limit is then their own 5000/hour budget (wait), and an
        // authorization failure is a bad credential (check it) -- opposite fixes,
        // so neither may collapse into the other's wording.
        let limited = with_token_env(Some("t"), || map_update_error(spent_quota_403(60)));
        let limited = limited.to_string();
        assert!(
            limited.contains("retry once it resets"),
            "an authenticated rate limit must advise waiting: {limited:?}"
        );
        assert!(
            !limited.contains("gh auth token"),
            "an authenticated failure must NOT advise setting a token: {limited:?}"
        );

        let rejected = with_token_env(Some("t"), || {
            map_update_error(self_update::Error::http_status_error(401, API_URL))
        });
        let rejected = rejected.to_string();
        assert!(
            rejected.contains("was rejected") && rejected.contains("gh auth status"),
            "a rejected credential must advise checking the token: {rejected:?}"
        );
        assert!(
            !rejected.contains("retry once it resets"),
            "a rejected credential is not a wait: {rejected:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn a_refused_request_treats_a_blank_token_as_no_token() {
        // The crate's lookup trims and skips an empty value, so a variable set to
        // whitespace sends the request unauthenticated. The hint keys on the same
        // rule; keying on `var().is_ok()` instead would print "your token was
        // rejected" for a request that carried no token at all.
        let msg = with_token_env(Some("   "), || map_update_error(spent_quota_403(60))).to_string();
        assert!(
            msg.contains("unauthenticated") && msg.contains("gh auth token"),
            "a whitespace-only token must count as no token: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn a_refused_request_does_not_repeat_the_crates_generic_token_advice() {
        // The crate's own Display closes a rate limit with "set an auth token to
        // raise the limit, or check less often". Composing the message from the
        // error's accessors instead of its Display is what keeps that clause out:
        // beside the unauthenticated hint it says the same thing twice, and beside
        // the token-set hint it tells the user to set a token they just set.
        let msg = with_token_env(Some("t"), || map_update_error(spent_quota_403(60))).to_string();
        assert!(
            !msg.contains("set an auth token"),
            "the crate's generic advice must not survive into the message: {msg:?}"
        );
        assert_eq!(
            msg.matches("GH_TOKEN").count(),
            1,
            "the token must be named exactly once: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn a_bare_rate_limit_reports_no_wait_it_does_not_know() {
        // A 429 from a proxy or CDN carries no quota headers at all, so there is
        // no reset to report. The message must omit the wait rather than
        // inventing one or printing "retry in 0s".
        let msg = with_token_env(None, || {
            map_update_error(self_update::Error::http_status_error(429, API_URL))
        })
        .to_string();
        assert!(
            !msg.contains("retry in"),
            "an unknown wait must be omitted, not guessed: {msg:?}"
        );
        assert!(
            msg.contains("HTTP 429"),
            "the status must still be reported: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80 STO-54
    fn a_refused_request_sanitizes_the_endpoint_controlled_url() {
        // The URL is echoed back from the endpoint, which is untrusted on the
        // network this hint exists for (an intercepting proxy), so it is stripped
        // before it is embedded -- otherwise a crafted response could dress itself
        // up as mind's own output right where the user is being told to go find a
        // credential.
        let hostile = "https://api.github.com/\x1b[31mx\x1b[0m";
        let msg = with_token_env(None, || {
            map_update_error(self_update::Error::http_status_error(403, hostile))
        })
        .to_string();
        assert!(
            !msg.contains('\x1b'),
            "ANSI must be stripped from the endpoint-supplied URL: {msg:?}"
        );
    }

    #[test]
    // spec: STO-80
    fn map_update_error_routes_refusals_away_from_the_transport_hints() {
        // The two crate variants that mean "the endpoint would not let this
        // request through" must reach the token hint rather than
        // `maybe_proxy_hint`, whose advice (HTTPS_PROXY, SSL_CERT_FILE) is about
        // reaching and trusting the endpoint and does not apply here. They must
        // also leave `DownloadFailed` behind: nothing was downloaded, and that
        // variant's wording names the release page rather than the endpoint that
        // actually refused.
        // The crate's variants are `#[non_exhaustive]`, so they are built through
        // its public status constructor: 429 classifies as `RateLimited` on the
        // status alone (RFC 6585) and a header-blind 403 as `Unauthorized`.
        let rate_limited = self_update::Error::http_status_error(429, API_URL);
        let unauthorized = self_update::Error::http_status_error(403, API_URL);
        assert!(
            matches!(rate_limited, self_update::Error::RateLimited { .. })
                && matches!(unauthorized, self_update::Error::Unauthorized { .. }),
            "the fixtures must be the two variants under test"
        );

        for err in [rate_limited, unauthorized] {
            let label = err.to_string();
            let mapped = with_token_env(None, || map_update_error(err));
            assert_eq!(
                mapped.kind(),
                "self-update-refused",
                "{label} must carry its own error kind"
            );
            let msg = mapped.to_string();
            assert!(
                msg.contains("GH_TOKEN"),
                "{label} must carry the token hint: {msg:?}"
            );
            assert!(
                !msg.contains("HTTPS_PROXY") && !msg.contains("SSL_CERT_FILE"),
                "{label} must not carry transport advice: {msg:?}"
            );
            assert!(
                !msg.contains("failed to download"),
                "{label} must not be reported as a download failure: {msg:?}"
            );
        }
    }

    #[test]
    // spec: STO-78
    fn the_self_update_dependency_trusts_the_machines_certificate_store() {
        // STO-78 is a Cargo feature, not a code path, so this is where it can be
        // pinned: drop `native-certs` and every build still compiles and every
        // other test still passes, while `evolve` silently stops working on any
        // network that re-signs HTTPS with a company CA. The failure is invisible
        // to a hermetic suite and only shows up on a user's machine, which is
        // exactly the kind of regression worth a guard.
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
        let manifest = std::fs::read_to_string(&path).expect("Cargo.toml must be readable");
        let dep = manifest
            .split_once("self_update = {")
            .expect("Cargo.toml must declare the self_update dependency")
            .1
            .split_once('}')
            .expect("the self_update dependency table must be closed")
            .0;
        assert!(
            dep.contains("\"native-certs\""),
            "self_update must enable `native-certs` so evolve verifies against the \
             machine's certificate store (STO-78); got: {dep}"
        );
    }

    #[test]
    // spec: STO-79
    fn maybe_proxy_hint_certificate_failure_points_at_the_trust_store() {
        // The exact string rustls produces when the peer presents a cert from a CA
        // that is not in the trust store, captured from a live handshake against a
        // server holding a cert signed by an untrusted CA. This is what a user
        // behind an intercepting corporate proxy sees, so it must carry the hint
        // that names the fix (the trust store / SSL_CERT_FILE) rather than the
        // proxy hint, which would send them after the wrong setting.
        let reason = "io: invalid peer certificate: UnknownIssuer";
        let result = maybe_proxy_hint(reason);
        assert!(
            result.starts_with(reason),
            "the original reason must be preserved ahead of the hint: {result:?}"
        );
        assert!(
            result.contains("SSL_CERT_FILE"),
            "a certificate failure must name the bundle override: {result:?}"
        );
        assert!(
            result.contains("certificate store"),
            "a certificate failure must name the machine's trust store: {result:?}"
        );
        assert!(
            !result.contains("HTTPS_PROXY"),
            "a certificate failure must NOT get the proxy hint: {result:?}"
        );
    }

    #[test]
    // spec: STO-79
    fn maybe_proxy_hint_certificate_markers_are_matched_case_insensitively() {
        // The reason text is whatever the transport produced, so the marker match
        // must not depend on its casing. Each recognized spelling gets the cert
        // hint, in the casing least likely to match a naive `contains`.
        for reason in [
            "Invalid Peer Certificate: UnknownIssuer",
            "SSL certificate problem: unable to get local issuer certificate verify failed",
            "tls handshake failed: UNKNOWNISSUER",
            "Self-Signed Certificate in certificate chain",
        ] {
            let result = maybe_proxy_hint(reason);
            assert!(
                result.contains("SSL_CERT_FILE"),
                "{reason:?} must be recognized as a certificate failure: {result:?}"
            );
        }
    }

    #[test]
    // spec: STO-54, STO-79
    fn maybe_proxy_hint_prefers_the_proxy_hint_when_both_could_match() {
        // A 407 is answered by the proxy before any certificate is presented, so a
        // reason carrying both signals is a proxy problem. The two hints are
        // mutually exclusive; this pins which one wins.
        let reason = "407 Proxy Authentication Required (invalid peer certificate: UnknownIssuer)";
        let result = maybe_proxy_hint(reason);
        assert!(
            result.contains("HTTPS_PROXY"),
            "a 407 must take the proxy hint even alongside certificate wording: {result:?}"
        );
        assert!(
            !result.contains("SSL_CERT_FILE"),
            "the two hints must be mutually exclusive: {result:?}"
        );
    }

    #[test]
    // spec: STO-79
    fn maybe_proxy_hint_strips_ansi_and_bidi_from_a_certificate_reason() {
        // The certificate branch is reached with endpoint-controlled text just like
        // the proxy branch (STO-54), so it must sanitize before it embeds. A hostile
        // subject/issuer name is exactly where spoofed control sequences would ride in.
        let hostile = "\x1b[1minvalid peer certificate\x1b[0m: \u{202E}UnknownIssuer";
        let result = maybe_proxy_hint(hostile);
        assert!(
            !result.contains('\x1b'),
            "ANSI must be stripped in the certificate branch: {result:?}"
        );
        assert!(
            !result.contains('\u{202E}'),
            "bidi overrides must be stripped in the certificate branch: {result:?}"
        );
        assert!(
            result.contains("SSL_CERT_FILE"),
            "sanitization must not suppress the certificate hint: {result:?}"
        );
    }

    // ---- STO-53: install.sh wget invocations pass --tries=1 ------------------

    /// Read the shipped `resources/install.sh` from the crate root (mirrors the
    /// CHANGELOG.md resource-reading pattern in tests/changelog.rs).
    fn install_sh() -> String {
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/install.sh");
        std::fs::read_to_string(&path).expect("resources/install.sh must exist and be readable")
    }

    #[test]
    // spec: STO-53
    fn install_sh_every_wget_invocation_passes_tries_1() {
        // STO-53 requires that *every* wget invocation in resources/install.sh (not
        // just the Rust downloader) pass --tries=1, so the shell installer cannot
        // hang ~20x the timeout on a blackholed endpoint. The Rust arg builders are
        // covered by wget_args_include_tries_1; this closes the shell-script half.
        let script = install_sh();
        let wget_lines: Vec<&str> = script
            .lines()
            .map(|l| l.trim())
            .filter(|l| l.starts_with("wget "))
            .collect();
        assert_eq!(
            wget_lines.len(),
            2,
            "install.sh must invoke wget exactly twice (fetch + fetch_to); found: {wget_lines:?}"
        );
        for line in &wget_lines {
            assert!(
                line.contains("--tries=1"),
                "every wget invocation in install.sh must pass --tries=1: {line:?}"
            );
        }
    }

    #[test]
    // spec: STO-52
    fn install_sh_wget_invocations_carry_a_timeout() {
        // install.sh's wget calls must also carry an explicit --timeout so a stalled
        // connect cannot hang the installer (STO-52; the fixed 15 s CONNECT_TIMEOUT).
        let script = install_sh();
        for line in script
            .lines()
            .map(|l| l.trim())
            .filter(|l| l.starts_with("wget "))
        {
            assert!(
                line.contains("--timeout="),
                "every wget invocation in install.sh must pass --timeout=: {line:?}"
            );
        }
    }

    // ---- POL-51..54: policy control over self-update -------------------------

    /// Load a `Policy` from a TOML string via a temp file (mirrors the
    /// MIND_POLICY_FILE fixture pattern used in tests/cli.rs).
    fn policy_from_toml(toml: &str) -> crate::policy::Policy {
        let dir = std::env::temp_dir();
        let path = dir.join(format!(
            "mind-selfupdate-pol-{}-{}.toml",
            std::process::id(),
            MKTEMP_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        std::fs::write(&path, toml).unwrap();
        let p = crate::policy::load_file(&path).unwrap();
        std::fs::remove_file(&path).ok();
        p
    }

    #[test]
    // spec: POL-51
    fn policy_absent_allows_evolve_with_no_pin() {
        // No policy -> check_policy_for_evolve returns Ok(None): unrestricted.
        let result = check_policy_for_evolve(None, None);
        assert_eq!(
            result.unwrap(),
            None,
            "absent policy must return Ok(None): unrestricted evolve"
        );
    }

    #[test]
    // spec: POL-54
    fn policy_self_update_true_allows_evolve() {
        // [binary].self-update = true is explicitly allowed (same as absent).
        let pol = policy_from_toml("[binary]\nself-update = true\n");
        let result = check_policy_for_evolve(Some(&pol), None);
        assert_eq!(
            result.unwrap(),
            None,
            "self-update = true must return Ok(None): unrestricted evolve"
        );
    }

    #[test]
    // spec: POL-52
    fn policy_disabled_denies_evolve_check_and_run() {
        // [binary].self-update = false -> Err(SelfUpdatePolicy) in all invocation modes.
        let pol = policy_from_toml("[binary]\nself-update = false\n");

        // No --version: disabled.
        let err = check_policy_for_evolve(Some(&pol), None).unwrap_err();
        match err {
            MindError::SelfUpdatePolicy { detail } => {
                assert!(
                    detail.contains("disabled by the managed policy"),
                    "disabled detail must say the policy disabled it: {detail}"
                );
            }
            other => panic!("expected SelfUpdatePolicy, got {other:?}"),
        }

        // With --version: still disabled (no version makes it OK).
        let err = check_policy_for_evolve(Some(&pol), Some("9.9.9")).unwrap_err();
        assert!(
            matches!(err, MindError::SelfUpdatePolicy { .. }),
            "disabled policy must error even with a --version arg: {err:?}"
        );
    }

    #[test]
    // spec: POL-53
    fn policy_pinned_no_version_arg_returns_pin() {
        // Policy pins to "0.14.0"; no --version -> return the pin, no network call.
        let pol = policy_from_toml("[binary]\nself-update = \"0.14.0\"\n");
        let pin = check_policy_for_evolve(Some(&pol), None).unwrap();
        assert_eq!(
            pin,
            Some("0.14.0".to_string()),
            "pinned policy with no --version must return the pin"
        );
    }

    #[test]
    // spec: POL-53
    fn policy_pinned_matching_version_arg_returns_pin() {
        // Policy pins to "0.14.0"; --version 0.14.0 matches -> returns the pin.
        let pol = policy_from_toml("[binary]\nself-update = \"0.14.0\"\n");
        let pin = check_policy_for_evolve(Some(&pol), Some("0.14.0")).unwrap();
        assert_eq!(
            pin,
            Some("0.14.0".to_string()),
            "matching --version must succeed with a pinned policy"
        );
    }

    #[test]
    // spec: POL-53
    fn policy_pinned_mismatched_version_arg_errors() {
        // Policy pins to "0.14.0"; --version 0.15.0 conflicts -> Err.
        let pol = policy_from_toml("[binary]\nself-update = \"0.14.0\"\n");
        let result = check_policy_for_evolve(Some(&pol), Some("0.15.0"));
        match result.unwrap_err() {
            MindError::SelfUpdatePolicy { detail } => {
                assert!(detail.contains("0.14.0"), "must name the pin: {detail}");
                assert!(
                    detail.contains("0.15.0"),
                    "must name the conflicting version: {detail}"
                );
                assert!(
                    detail.contains("conflicts"),
                    "must say 'conflicts': {detail}"
                );
                // `--to` is the canonical evolve flag (`--version` is a hidden
                // deprecated alias, commit 40c495f); the pin-conflict message
                // must name the flag `evolve --help` actually documents.
                assert!(
                    detail.contains("--to 0.15.0"),
                    "must name the canonical --to flag, not the hidden --version alias: {detail}"
                );
            }
            other => panic!("expected SelfUpdatePolicy, got {other:?}"),
        }
    }

    #[test]
    fn have_detects_present_and_absent_commands() {
        // `sh` is on PATH on every supported platform; a builtin like `command`
        // is not an executable, so the old `Command::new("command")` probe wrongly
        // reported everything missing. This guards that regression.
        assert!(have("sh"), "`sh` must be detected on PATH");
        assert!(
            !have("mind-no-such-binary-xyzzy"),
            "a nonexistent command must not be detected"
        );
    }

    // spec: CLI-243
    #[test]
    fn is_homebrew_install_matches_only_a_cellar_component() {
        for p in [
            "/opt/homebrew/Cellar/mind/0.30.1/bin/mind",
            "/usr/local/Cellar/mind/0.30.1/bin/mind",
            "/home/linuxbrew/.linuxbrew/Cellar/mind/0.30.1/bin/mind",
        ] {
            assert!(is_homebrew_install(Path::new(p)), "{p}");
        }
        for p in [
            "/home/u/.local/bin/mind",
            "/home/u/.cargo/bin/mind",
            "/home/u/Cellars/mind",
            "/home/u/my-Cellar/bin/mind",
        ] {
            assert!(!is_homebrew_install(Path::new(p)), "{p}");
        }
    }

    #[test]
    // spec: STO-83
    fn target_triple_maps_supported_platforms() {
        assert_eq!(
            target_triple("linux", "x86_64").unwrap(),
            "x86_64-unknown-linux-musl"
        );
        assert_eq!(
            target_triple("linux", "aarch64").unwrap(),
            "aarch64-unknown-linux-musl"
        );
        assert_eq!(
            target_triple("macos", "aarch64").unwrap(),
            "aarch64-apple-darwin"
        );
        assert_eq!(
            target_triple("macos", "x86_64").unwrap(),
            "x86_64-apple-darwin"
        );
    }

    #[test]
    // spec: CLI-142 -- a platform with no published artifact is an error and
    // nothing is changed.
    fn target_triple_rejects_unknown_platforms() {
        match target_triple("macos", "riscv64") {
            Err(MindError::UnsupportedPlatform { os, arch }) => {
                assert_eq!(os, "macos");
                assert_eq!(arch, "riscv64");
            }
            other => panic!("expected UnsupportedPlatform, got {other:?}"),
        }
        // An unknown architecture is also rejected on Linux.
        assert!(matches!(
            target_triple("linux", "riscv64"),
            Err(MindError::UnsupportedPlatform { .. })
        ));
        // An unknown OS is rejected.
        assert!(matches!(
            target_triple("windows", "x86_64"),
            Err(MindError::UnsupportedPlatform { .. })
        ));
    }

    #[test]
    // spec: CLI-140
    fn decision_compares_versions() {
        // current == target => up to date (explicit or not).
        assert_eq!(decision("0.3.0", "0.3.0", false), Decision::UpToDate);
        assert_eq!(decision("0.3.0", "0.3.0", true), Decision::UpToDate);
        // target newer => update.
        assert_eq!(decision("0.2.0", "0.3.0", false), Decision::Update);
        // current newer, no explicit pin => up to date.
        assert_eq!(decision("0.4.0", "0.3.0", false), Decision::UpToDate);
    }

    #[test]
    // spec: CLI-147
    fn decision_explicit_pinned_below_current_yields_pinned_below() {
        // An explicit --version strictly below the running version must NOT return
        // UpToDate; the caller needs PinnedBelowCurrent to emit a "not downgrading"
        // message rather than silently claiming up to date.
        assert_eq!(
            decision("0.3.0", "0.1.0", true),
            Decision::PinnedBelowCurrent
        );
        assert_eq!(
            decision("1.0.0", "0.9.9", true),
            Decision::PinnedBelowCurrent
        );
        // With explicit=false (latest from network) a running version >= latest is
        // still UpToDate, never PinnedBelowCurrent.
        assert_eq!(decision("0.4.0", "0.3.0", false), Decision::UpToDate);
    }

    #[test]
    // spec: CLI-140
    fn decision_explicit_equal_to_current_is_up_to_date() {
        // When the pinned version equals the running version "up to date" is correct
        // even with explicit=true; no downgrade is attempted.
        assert_eq!(decision("0.3.0", "0.3.0", true), Decision::UpToDate);
    }

    #[test]
    // spec: CLI-140
    fn decision_explicit_above_current_is_update() {
        // An explicit --version newer than the running version requests an upgrade.
        assert_eq!(decision("0.2.0", "0.3.0", true), Decision::Update);
    }

    #[test]
    // spec: CLI-140
    fn decision_prerelease_current_updates_onto_its_base_release() {
        // A source-built `-dev` binary shares the numeric view of its base
        // release, so the comparator reads them as equal; but the dev build
        // predates the release, so `decision` must offer the update rather than
        // claim up-to-date -- both when resolving latest and for an explicit
        // `--to` onto the same base version.
        assert_eq!(decision("0.23.1-dev", "0.23.1", false), Decision::Update);
        assert_eq!(decision("0.23.1-dev", "0.23.1", true), Decision::Update);
        // A genuinely newer dev build is NOT downgraded onto an older release.
        assert_eq!(decision("0.24.0-dev", "0.23.1", false), Decision::UpToDate);
        assert_eq!(
            decision("0.24.0-dev", "0.23.1", true),
            Decision::PinnedBelowCurrent
        );
        // Two prereleases of the same base stay up-to-date (no release to move to).
        assert_eq!(
            decision("0.23.1-dev", "0.23.1-dev", false),
            Decision::UpToDate
        );
        // A release current is never treated as a prerelease.
        assert_eq!(decision("0.23.1", "0.23.1", false), Decision::UpToDate);
    }

    #[test]
    // spec: CLI-140
    fn decision_dotted_prerelease_current_updates_onto_its_base_release() {
        // A DOTTED prerelease suffix (semver's `-rc.2` form, as opposed to a
        // bare `-dev`) must be handled identically to `decision_prerelease_
        // current_updates_onto_its_base_release` above. Before the
        // `version_at_least` fix (mindfile.rs), per-dotted-component suffix
        // stripping mis-parsed "1.0.0-rc.2" as [1, 0, 0, 2] -- numerically
        // ABOVE the plain "1.0.0" it should tie with -- so `is_prerelease`
        // correctly flagged the value as a prerelease, but the numeric-tie
        // guard `version_at_least(target, current)` never held, and `decision`
        // fell through to UpToDate / PinnedBelowCurrent instead of offering
        // the update that supersedes the prerelease.
        assert_eq!(decision("1.0.0-rc.2", "1.0.0", false), Decision::Update);
        assert_eq!(decision("1.0.0-rc.2", "1.0.0", true), Decision::Update);
        // A genuinely newer dotted prerelease is NOT downgraded onto an older
        // release.
        assert_eq!(decision("1.1.0-rc.2", "1.0.0", false), Decision::UpToDate);
        assert_eq!(
            decision("1.1.0-rc.2", "1.0.0", true),
            Decision::PinnedBelowCurrent
        );
        // Two dotted prereleases of the same base stay up-to-date.
        assert_eq!(
            decision("1.0.0-rc.2", "1.0.0-rc.2", false),
            Decision::UpToDate
        );
    }

    // ---- STO-77 / M8: numeric-tie prerelease ordering -------------------

    #[test]
    // spec: STO-77 CLI-140
    fn decision_explicit_prerelease_to_prerelease_tie_break_orders_by_precedence() {
        // M8: before this fix, `evolve --to 0.24.0-rc2` while running
        // `0.24.0-rc1` tied numerically, and since `target` ALSO carried a
        // prerelease suffix, the old `is_prerelease(current) &&
        // !is_prerelease(target)` guard never fired -- the whole point of
        // pinning an rc release. `decision` silently returned `UpToDate`
        // ("already up to date", exit 0, nothing changed) instead of
        // offering the update. This is the headline case the fix restores:
        // moving forward between two prereleases of the same base must
        // offer `Update`, and moving backward must be refused as an explicit
        // downgrade, not silently ignored.
        assert_eq!(
            decision("0.24.0-rc1", "0.24.0-rc2", true),
            Decision::Update,
            "pinning a HIGHER rc of the same base must offer the update"
        );
        assert_eq!(
            decision("0.24.0-rc2", "0.24.0-rc1", true),
            Decision::PinnedBelowCurrent,
            "pinning a LOWER rc of the same base must be a refused downgrade, not UpToDate"
        );
        // The non-explicit path (a fetched `latest` tag, which never carries
        // a prerelease in practice) still degrades to `UpToDate` rather than
        // ever reporting a spurious downgrade refusal.
        assert_eq!(
            decision("0.24.0-rc2", "0.24.0-rc1", false),
            Decision::UpToDate
        );
        // Byte-identical prerelease strings are always up to date.
        assert_eq!(
            decision("0.24.0-rc1", "0.24.0-rc1", true),
            Decision::UpToDate
        );

        // Numeric-identifier precedence is compared NUMERICALLY, not
        // lexicographically: "9" must order below "10" (a naive string
        // compare would read "10" < "9").
        assert_eq!(decision("0.24.0-9", "0.24.0-10", true), Decision::Update);
        assert_eq!(
            decision("0.24.0-10", "0.24.0-9", true),
            Decision::PinnedBelowCurrent
        );

        // A shorter dot-identifier list has LOWER precedence than a longer
        // one that extends it (semver.org precedence rule #11: `rc.1` <
        // `rc.1.2`).
        assert_eq!(
            decision("0.24.0-rc.1", "0.24.0-rc.1.2", true),
            Decision::Update
        );
        assert_eq!(
            decision("0.24.0-rc.1.2", "0.24.0-rc.1", true),
            Decision::PinnedBelowCurrent
        );
    }

    #[test]
    // spec: STO-77 CLI-140
    fn decision_explicit_release_to_same_base_prerelease_is_pinned_below_current() {
        // M8's second silent no-op: running the RELEASED `0.24.0` and pinning
        // `--to 0.24.0-rc1` also ties numerically. A prerelease predates its
        // base release (the same rule the pre-existing "prerelease current ->
        // release target" arm already encoded in the other direction), so an
        // EXPLICIT pin back onto a same-base prerelease is a downgrade
        // request and must be refused as such, not silently reported
        // "already up to date".
        assert_eq!(
            decision("0.24.0", "0.24.0-rc1", true),
            Decision::PinnedBelowCurrent
        );
        // Without an explicit pin (unreachable in practice -- `releases/latest`
        // never returns a prerelease -- but must degrade safely rather than
        // spuriously refuse) it stays UpToDate.
        assert_eq!(decision("0.24.0", "0.24.0-rc1", false), Decision::UpToDate);
    }

    #[test]
    fn is_prerelease_detects_only_a_dash_suffix() {
        assert!(is_prerelease("0.23.1-dev"));
        assert!(is_prerelease("1.0.0-rc.2"));
        assert!(!is_prerelease("0.23.1"));
        assert!(!is_prerelease("0.23.1+build"));
    }

    #[test]
    // spec: CLI-141
    fn check_report_reflects_the_decision_without_network() {
        // The --check branch reports pending vs up-to-date purely from the
        // decision over an explicit target version: no network is consulted.
        let pending = decision("0.2.0", "0.3.0", false);
        assert_eq!(pending, Decision::Update);
        let report = check_report("0.2.0", "0.3.0", &pending, "x86_64-unknown-linux-musl");
        assert!(report.contains("0.2.0"), "report: {report}");
        assert!(report.contains("0.3.0"), "report: {report}");
        assert!(report.contains("available"), "report: {report}");

        let current = decision("0.3.0", "0.3.0", false);
        assert_eq!(current, Decision::UpToDate);
        let report = check_report("0.3.0", "0.3.0", &current, "x86_64-unknown-linux-musl");
        assert!(report.contains("up to date"), "report: {report}");
    }

    #[test]
    // spec: CLI-147
    fn check_report_pinned_below_says_not_downgrading() {
        // The report for PinnedBelowCurrent must name both versions and say
        // "not downgrading" -- it must NOT say "up to date".
        let d = Decision::PinnedBelowCurrent;
        let report = check_report("0.3.0", "0.1.0", &d, "x86_64-unknown-linux-musl");
        assert!(report.contains("0.1.0"), "pinned version missing: {report}");
        assert!(
            report.contains("0.3.0"),
            "running version missing: {report}"
        );
        assert!(
            report.contains("not downgrading"),
            "must say 'not downgrading': {report}"
        );
        assert!(
            !report.contains("up to date"),
            "must NOT say 'up to date': {report}"
        );
    }

    #[test]
    // spec: CLI-141
    fn check_report_up_to_date_when_equal() {
        // When the running and target versions are equal, "up to date" regardless
        // of explicit; tests the UpToDate arm of check_report directly.
        let d = Decision::UpToDate;
        let report = check_report("0.3.0", "0.3.0", &d, "x86_64-unknown-linux-musl");
        assert!(report.contains("up to date"), "report: {report}");
        assert!(
            !report.contains("not downgrading"),
            "must NOT say 'not downgrading': {report}"
        );
    }

    // spec: STO-52
    #[test]
    fn the_download_budget_is_never_the_metadata_budget() {
        // The updater's timeout is a whole-request budget, body included, not a
        // connect timeout, so the value that bounds a release lookup would abort a
        // healthy multi-megabyte download on a slow link. The download gets the
        // ceiling instead, and a knob set above the ceiling still wins.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let orig = std::env::var("MIND_HTTP_TIMEOUT_SECS").ok();

        // SAFETY: ENV_LOCK is held across every mutation and read below.
        unsafe { std::env::remove_var("MIND_HTTP_TIMEOUT_SECS") };
        let (default_lookup, default_download) = (lookup_timeout(), download_timeout());

        unsafe { std::env::set_var("MIND_HTTP_TIMEOUT_SECS", "30") };
        let (raised_lookup, raised_download) = (lookup_timeout(), download_timeout());

        unsafe { std::env::set_var("MIND_HTTP_TIMEOUT_SECS", "900") };
        let above_ceiling = download_timeout();

        // Restore before any assertion can panic and leave the env corrupted.
        // SAFETY: ENV_LOCK is still held.
        unsafe {
            match orig {
                Some(v) => std::env::set_var("MIND_HTTP_TIMEOUT_SECS", v),
                None => std::env::remove_var("MIND_HTTP_TIMEOUT_SECS"),
            }
        }
        drop(guard);

        assert_eq!(default_lookup.as_secs(), 15, "the default metadata budget");
        assert_eq!(
            default_download.as_secs(),
            600,
            "the download must get the 600s ceiling, not the 15s metadata budget"
        );
        assert_eq!(
            raised_lookup.as_secs(),
            30,
            "MIND_HTTP_TIMEOUT_SECS sets the metadata budget"
        );
        assert_eq!(
            raised_download.as_secs(),
            600,
            "a knob below the ceiling must not shrink the download budget"
        );
        assert_eq!(
            above_ceiling.as_secs(),
            900,
            "a knob above the ceiling wins: raising it asks for more room, not less"
        );
    }

    // spec: STO-76
    #[test]
    fn validated_target_strips_exactly_one_leading_v() {
        assert_eq!(validated_target("1.2.3").unwrap(), "1.2.3");
        assert_eq!(
            validated_target("v1.2.3").unwrap(),
            "1.2.3",
            "`--to v1.2.3` must behave identically to `--to 1.2.3`"
        );
        assert_eq!(validated_target("v1.2.3-rc1").unwrap(), "1.2.3-rc1");
        assert!(
            validated_target("vv1.2.3").is_err(),
            "only one leading `v` is stripped, so the second one must fail validation"
        );
    }

    // The release tag the updater looks up is `v<version>`, built from the
    // ALREADY-validated (and `v`-stripped) version. Pinning `--to v1.2.3` must
    // therefore request `v1.2.3`, not `vv1.2.3`, which is a tag no release carries.
    // spec: CLI-142 STO-76
    #[test]
    fn an_explicit_target_pins_a_single_v_prefixed_release_tag() {
        use self_update::UpdateConfig as _;
        let version = validated_target("v1.2.3").expect("a plain version validates");
        let upd = updater(
            "x86_64-unknown-linux-musl",
            Some(&version),
            download_timeout(),
        )
        .expect("building the updater must not require the network");
        assert_eq!(upd.release_tag(), Some("v1.2.3"));
    }

    // With no explicit target the updater is left unpinned, so the lookup goes to
    // the `releases/latest` endpoint rather than to a tag.
    // spec: CLI-140
    #[test]
    fn no_explicit_target_leaves_the_updater_unpinned() {
        use self_update::UpdateConfig as _;
        let upd = updater("x86_64-unknown-linux-musl", None, lookup_timeout())
            .expect("building the updater must not require the network");
        assert_eq!(upd.release_tag(), None);
        assert_eq!(
            upd.target(),
            "x86_64-unknown-linux-musl",
            "the resolved triple selects the release asset (STO-65)"
        );
    }

    // spec: PRG-4
    #[test]
    fn evolve_steps_are_labeled_and_the_crate_draws_no_bar_of_its_own() {
        use self_update::UpdateConfig;
        assert_eq!(LOOKUP_STEP, "checking for the latest mind release");
        assert_eq!(VERIFY_STEP, "verifying build provenance");
        assert_eq!(download_step("1.2.3"), "downloading mind 1.2.3");
        let upd = updater(
            "x86_64-unknown-linux-musl",
            Some("1.2.3"),
            download_timeout(),
        )
        .expect("building the updater must not require the network");
        assert!(
            !upd.show_download_progress(),
            "the download is a progress step, not the crate's bar"
        );
    }

    // spec: CLI-217
    #[test]
    fn download_banner_is_suppressed_under_json() {
        // `evolve` writes its own document straight to stdout, so it is exempt from
        // CLI-217's structural fd-1 protection and this guard is the only thing
        // keeping the progress line out of a `--json` run's single document. The
        // text-mode branch is asserted too, so deleting the line outright rather
        // than routing it fails here instead of passing by coincidence.
        assert_eq!(
            download_banner(true, "1.2.3", "x86_64-unknown-linux-musl"),
            None,
            "--json must emit no progress line"
        );
        let line = download_banner(false, "1.2.3", "x86_64-unknown-linux-musl")
            .expect("text mode must still announce the download");
        assert!(
            line.contains("downloading mind 1.2.3"),
            "the banner must name the version being installed: {line}"
        );
        assert!(
            line.contains("x86_64-unknown-linux-musl"),
            "the banner must name the artifact's target triple: {line}"
        );
    }

    // spec: TUI-61
    #[test]
    fn mktemp_dir_prefixed_creates_a_fresh_directory() {
        // The directory must exist after the call returns and must be empty.
        let dir = mktemp_dir_prefixed("mind-test").expect("mktemp_dir_prefixed");
        let exists = dir.is_dir();
        let named = dir
            .file_name()
            .and_then(|n| n.to_str())
            .is_some_and(|n| n.starts_with("mind-test-"));
        let _ = std::fs::remove_dir_all(&dir);
        assert!(exists, "the directory must be created: {dir:?}");
        assert!(
            named,
            "the caller's prefix must name the directory: {dir:?}"
        );
    }

    // spec: TUI-61
    #[test]
    fn mktemp_dir_prefixed_yields_distinct_paths() {
        // Two successive calls must return different paths (the sequence number
        // component guarantees this within a process), and both must be creatable
        // -- proving the exclusive-create semantics would reject a pre-existing dir.
        let a = mktemp_dir_prefixed("mind-test").expect("first mktemp_dir_prefixed");
        let b = mktemp_dir_prefixed("mind-test").expect("second mktemp_dir_prefixed");
        let _ = std::fs::remove_dir_all(&a);
        let _ = std::fs::remove_dir_all(&b);
        assert_ne!(a, b, "successive calls must yield distinct paths");
    }

    // ---- STO-65: target triple visible before download ------------------------

    #[test]
    // spec: STO-65
    fn check_report_includes_the_target_triple_in_every_branch() {
        // The resolved artifact target triple must be visible in the --check
        // report BEFORE anything is downloaded, in all three decision branches,
        // without disturbing the existing wording (each existing assertion in
        // check_report_reflects_the_decision_without_network /
        // check_report_pinned_below_says_not_downgrading /
        // check_report_up_to_date_when_equal must still hold).
        let triple = "x86_64-unknown-linux-musl";

        let update = decision("0.2.0", "0.3.0", false);
        let report = check_report("0.2.0", "0.3.0", &update, triple);
        assert!(
            report.contains(triple),
            "Update report must include the target triple: {report}"
        );
        assert!(
            report.contains("available"),
            "Update report must keep the existing wording: {report}"
        );

        let up_to_date = decision("0.3.0", "0.3.0", false);
        let report = check_report("0.3.0", "0.3.0", &up_to_date, triple);
        assert!(
            report.contains(triple),
            "UpToDate report must include the target triple: {report}"
        );
        assert!(
            report.contains("up to date"),
            "UpToDate report must keep the existing wording: {report}"
        );

        let pinned_below = Decision::PinnedBelowCurrent;
        let report = check_report("0.3.0", "0.1.0", &pinned_below, triple);
        assert!(
            report.contains(triple),
            "PinnedBelowCurrent report must include the target triple: {report}"
        );
        assert!(
            report.contains("not downgrading"),
            "PinnedBelowCurrent report must keep the existing wording: {report}"
        );
    }

    #[test]
    // spec: STO-65
    fn check_report_target_triple_distinguishes_musl_from_gnu() {
        // The whole point: a gnu -> musl artifact swap must be visible in the
        // report text, not just silently resolved. A musl triple must not read
        // as a gnu triple and vice versa.
        let musl_report = check_report(
            "0.2.0",
            "0.3.0",
            &Decision::Update,
            "x86_64-unknown-linux-musl",
        );
        assert!(musl_report.contains("musl"), "{musl_report}");
        assert!(!musl_report.contains("gnu"), "{musl_report}");

        let gnu_report = check_report(
            "0.2.0",
            "0.3.0",
            &Decision::Update,
            "x86_64-unknown-linux-gnu",
        );
        assert!(gnu_report.contains("gnu"), "{gnu_report}");
    }

    // The --json shape (the `target_triple` key added alongside the existing
    // `action`/`target`/`outcome` keys, none renamed) is covered end-to-end at
    // the CLI level in tests/cli.rs (`evolve_check_json_includes_target_triple_key`),
    // which drives the real binary and the real `print_evolve_json`, rather than
    // here: `print_evolve_json` writes straight to stdout via
    // `crate::render::print_json`, so a src/ unit test cannot observe its output
    // without capturing process stdout.

    // ---- STO-66: soft build-provenance verification ---------------------------

    #[test]
    // spec: STO-66
    fn gh_attestation_verify_args_builds_expected_argv() {
        let args = gh_attestation_verify_args("/tmp/mind-0.3.0-x86_64.tar.gz", "jaemk/mind");
        assert_eq!(
            args,
            vec![
                "attestation".to_string(),
                "verify".to_string(),
                "/tmp/mind-0.3.0-x86_64.tar.gz".to_string(),
                "--repo".to_string(),
                "jaemk/mind".to_string(),
            ],
            "argv must match `gh attestation verify <archive> --repo <repo>`: {args:?}"
        );
    }

    #[test]
    // spec: STO-66
    fn is_gh_tooling_error_classifies_known_tooling_markers() {
        assert!(
            is_gh_tooling_error("Error: unknown command \"attestation\" for \"gh\""),
            "an unsupported subcommand (old gh) must classify as a tooling error"
        );
        assert!(
            is_gh_tooling_error(
                "Get \"https://api.github.com/...\": dial tcp: lookup api.github.com: no such host"
            ),
            "a DNS/network failure must classify as a tooling error"
        );
        assert!(
            is_gh_tooling_error("Error: connection refused"),
            "a connection-refused failure must classify as a tooling error"
        );
        assert!(
            is_gh_tooling_error(
                "To use GitHub CLI in a GitHub Actions workflow, run: gh auth login"
            ),
            "an auth-required message must classify as a tooling error"
        );
        // Case-insensitive.
        assert!(is_gh_tooling_error("DIAL TCP: CONNECTION REFUSED"));
    }

    #[test]
    // spec: STO-66
    fn is_gh_tooling_error_does_not_classify_no_attestations_found_as_tooling() {
        // The critical negative case: "no attestations found" (and the raw HTTP
        // 404 wording gh also surfaces for the identical underlying condition)
        // must NOT be treated as a tooling error, because it is exactly the
        // signal a substituted artifact would also produce (the attacker cannot
        // forge a valid signed attestation for a different digest). Treating it
        // as a pass-through tooling error would defeat the entire check.
        assert!(
            !is_gh_tooling_error("Error: no attestations found"),
            "'no attestations found' must be a genuine failure, not a tooling error"
        );
        assert!(
            !is_gh_tooling_error(
                "Error: HTTP 404: Not Found (https://api.github.com/repos/jaemk/mind/attestations/sha256:deadbeef)"
            ),
            "an HTTP 404 from the attestations endpoint must be a genuine failure, not a tooling error"
        );
    }

    /// Write an executable fake `gh` at `dir/gh` that records its argv (one arg
    /// per line, skipping the leading `attestation verify <path>` positional
    /// args' exact values are still captured) to `capture_path`, then exits with
    /// `exit_code` after printing `stderr_msg` to stderr. Mirrors
    /// `write_fake_failing_curl`.
    fn write_fake_gh(dir: &Path, capture_path: &Path, exit_code: i32, stderr_msg: &str) {
        let script_path = dir.join("gh");
        let script = format!(
            "#!/bin/sh\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done > {:?}\nprintf '%s' {:?} >&2\nexit {exit_code}\n",
            capture_path, stderr_msg
        );
        std::fs::write(&script_path, script).expect("write fake gh");
        let mut perms = std::fs::metadata(&script_path)
            .expect("stat fake gh")
            .permissions();
        perms.set_mode(0o700);
        std::fs::set_permissions(&script_path, perms).expect("chmod fake gh");
    }

    #[test]
    // spec: STO-66
    fn attestation_step_returns_none_when_gh_is_absent() {
        // A command name that resolves to nothing simulates "gh absent"
        // deterministically, without needing to hide the real `gh` (which is
        // present on this machine's PATH) from the process. No PATH mutation,
        // no ENV_LOCK needed.
        let archive = std::env::temp_dir().join("mind-evolve-attest-none-does-not-exist.tar.gz");
        assert_eq!(
            attestation_step("mind-no-such-gh-xyzzy", &archive),
            None,
            "attestation_step must return None (skip silently) when gh is absent"
        );
    }

    #[test]
    // spec: STO-66
    fn attestation_step_verified_with_fake_gh_success() {
        // A fake `gh` that exits 0 must yield Verified, and must have been
        // invoked with the expected `attestation verify <archive> --repo
        // jaemk/mind` argv (proving the wiring end-to-end, not just the pure
        // arg builder in isolation).
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());

        let n = MKTEMP_SEQ.fetch_add(1, Ordering::Relaxed);
        let scratch =
            std::env::temp_dir().join(format!("mind-evolve-fake-gh-ok-{}-{n}", std::process::id()));
        std::fs::create_dir_all(&scratch).expect("create scratch dir");
        let capture_path = scratch.join("argv-capture.txt");
        write_fake_gh(&scratch, &capture_path, 0, "");

        let orig_path = std::env::var("PATH").unwrap_or_default();
        let new_path = format!("{}:{orig_path}", scratch.display());
        // SAFETY: ENV_LOCK is held for the duration of the mutation below.
        unsafe {
            std::env::set_var("PATH", &new_path);
        }

        let archive = scratch.join("mind-0.3.0-x86_64.tar.gz");
        std::fs::write(&archive, b"fake archive").expect("seed fake archive");
        let result = attestation_step("gh", &archive);

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            std::env::set_var("PATH", &orig_path);
        }
        drop(guard);

        assert_eq!(
            result,
            Some(AttestationOutcome::Verified),
            "an exit-0 gh must yield Verified"
        );

        let argv = std::fs::read_to_string(&capture_path).expect("read captured argv");
        let lines: Vec<&str> = argv.lines().collect();
        assert_eq!(
            lines,
            vec![
                "attestation",
                "verify",
                archive.to_string_lossy().as_ref(),
                "--repo",
                "jaemk/mind",
            ],
            "gh must be invoked with the expected argv: {lines:?}"
        );

        let _ = std::fs::remove_dir_all(&scratch);
    }

    #[test]
    // spec: STO-66
    fn attestation_step_tooling_error_with_fake_gh_network_failure() {
        // A fake `gh` that exits non-zero with a recognizably network-level
        // stderr must classify as ToolingError, so evolve proceeds with a note
        // rather than aborting -- gh never got to ask the question.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());

        let n = MKTEMP_SEQ.fetch_add(1, Ordering::Relaxed);
        let scratch = std::env::temp_dir().join(format!(
            "mind-evolve-fake-gh-tooling-{}-{n}",
            std::process::id()
        ));
        std::fs::create_dir_all(&scratch).expect("create scratch dir");
        let capture_path = scratch.join("argv-capture.txt");
        write_fake_gh(
            &scratch,
            &capture_path,
            1,
            "dial tcp: lookup api.github.com: no such host",
        );

        let orig_path = std::env::var("PATH").unwrap_or_default();
        let new_path = format!("{}:{orig_path}", scratch.display());
        // SAFETY: ENV_LOCK is held for the duration of the mutation below.
        unsafe {
            std::env::set_var("PATH", &new_path);
        }

        let archive = scratch.join("mind-0.3.0-x86_64.tar.gz");
        std::fs::write(&archive, b"fake archive").expect("seed fake archive");
        let result = attestation_step("gh", &archive);

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            std::env::set_var("PATH", &orig_path);
        }
        drop(guard);

        match result {
            Some(AttestationOutcome::ToolingError(reason)) => {
                assert!(
                    reason.contains("no such host"),
                    "reason must carry gh's stderr: {reason}"
                );
            }
            other => panic!("expected Some(ToolingError(_)), got {other:?}"),
        }

        let _ = std::fs::remove_dir_all(&scratch);
    }

    #[test]
    // spec: STO-66
    fn attestation_step_genuine_failure_with_fake_gh_no_attestations_aborts() {
        // A fake `gh` that exits non-zero reporting "no attestations found"
        // must classify as GenuineFailure -- the exact case that must abort the
        // swap rather than pass through, since it is indistinguishable (from
        // gh's output) from a substituted artifact.
        let guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());

        let n = MKTEMP_SEQ.fetch_add(1, Ordering::Relaxed);
        let scratch = std::env::temp_dir().join(format!(
            "mind-evolve-fake-gh-fail-{}-{n}",
            std::process::id()
        ));
        std::fs::create_dir_all(&scratch).expect("create scratch dir");
        let capture_path = scratch.join("argv-capture.txt");
        write_fake_gh(&scratch, &capture_path, 1, "Error: no attestations found");

        let orig_path = std::env::var("PATH").unwrap_or_default();
        let new_path = format!("{}:{orig_path}", scratch.display());
        // SAFETY: ENV_LOCK is held for the duration of the mutation below.
        unsafe {
            std::env::set_var("PATH", &new_path);
        }

        let archive = scratch.join("mind-0.3.0-x86_64.tar.gz");
        std::fs::write(&archive, b"fake archive").expect("seed fake archive");
        let result = attestation_step("gh", &archive);

        // SAFETY: ENV_LOCK is still held.
        unsafe {
            std::env::set_var("PATH", &orig_path);
        }
        drop(guard);

        match result {
            Some(AttestationOutcome::GenuineFailure(reason)) => {
                assert!(
                    reason.contains("no attestations found"),
                    "reason must carry gh's stderr: {reason}"
                );
                // Mirror what download_and_swap does with this outcome: it must
                // map to AttestationVerificationFailed, which aborts (returns
                // Err) rather than proceeding to extraction/swap.
                let mapped = MindError::AttestationVerificationFailed { reason };
                assert_eq!(mapped.kind(), "attestation-verification-failed");
            }
            other => panic!("expected Some(GenuineFailure(_)), got {other:?}"),
        }

        let _ = std::fs::remove_dir_all(&scratch);
    }

    #[test]
    // spec: STO-80
    fn a_refused_request_renders_as_one_status_line_and_one_hint_line() {
        // Pins the shape the user actually reads, which the per-branch tests
        // above only assert pieces of: a status/endpoint line, then the hint on
        // its own line, and nothing else. `main` prints this after an `error: `
        // prefix and then walks the source chain, so a stray third line or a
        // trailing newline would show up as a blank line in the terminal.
        let msg = with_token_env(Some("t"), || {
            map_update_error(self_update::Error::http_status_error(401, API_URL))
        })
        .to_string();
        let lines: Vec<&str> = msg.lines().collect();
        assert_eq!(
            lines,
            vec![
                "GitHub rejected this request: HTTP 401 from \
                 https://api.github.com/repos/jaemk/mind/releases/latest",
                "hint: GH_TOKEN/GITHUB_TOKEN is set and was rejected; check that it is valid and \
                 not expired (gh auth status)",
            ],
            "the rendered shape changed: {msg:?}"
        );
    }
}