lean-ctx-cloud 3.12.13

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
// SPDX-License-Identifier: Apache-2.0
//! Enrollment (device flow), access tokens and lease renewal.
//!
//! The instance's Ed25519 key is its only long-lived secret: every access
//! token is minted from a fresh signed assertion and cached until shortly
//! before it expires. Nothing here runs inside a tool call.

use std::time::Duration;

use lean_ctx_time::UtcDateTime;
use serde_json::{Value, json};

use super::CloudError;
use super::consent::chat_key::ChatKeyOffer;
use super::consent::optin_key::{self, OptinKeyOffer};
use super::consent::task_key::{self, TaskKeyOffer};
use super::instance_key::InstanceKeys;
use super::lease::{VerifiedLease, load_lease, store_lease, verify_keyset, verify_lease};
use super::state::{CloudStore, INSTANCE_FILE, InstanceRecord, TOKEN_FILE, TokenRecord};
use super::transport::CloudClient;
use lean_ctx_protocol::cloud::app_keys::{
    AppKeyAnnouncementV1, AppKeyKind, PAIRING_FAILED_TEXT, app_key_bind_signing_input,
    validate_app_keys,
};
use lean_ctx_protocol::cloud::enroll::{
    AccessTokenV1, DEVICE_SETUP_REQUEST_SCHEMA, DEVICE_TOKEN_REQUEST_SCHEMA, DeviceCodeRequestV1,
    DeviceCodeV1, DeviceSetupProjectV1, DeviceSetupRequestV1, EngineOs, EnrollmentV1,
    MAX_INSTANCE_LABEL_CHARS, TOKEN_REQUEST_SCHEMA, TrialPlan, token_assertion_input,
};
use lean_ctx_protocol::cloud::lease::CloudLeaseEnvelopeV1;

/// Tokens are refreshed this long before they expire.
const TOKEN_REFRESH_MARGIN_SECONDS: i64 = 120;

pub fn engine_os() -> EngineOs {
    if cfg!(target_os = "macos") {
        EngineOs::Macos
    } else if cfg!(target_os = "linux") {
        EngineOs::Linux
    } else if cfg!(windows) {
        EngineOs::Windows
    } else {
        EngineOs::Other
    }
}

fn stamp(time: UtcDateTime) -> String {
    time.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true)
}

fn protocol(what: &str) -> impl Fn(lean_ctx_protocol::ValidationError) -> CloudError + '_ {
    move |e| CloudError::Protocol(format!("{what}: {e}"))
}

fn decode<T: serde::de::DeserializeOwned>(value: Value, what: &str) -> Result<T, CloudError> {
    serde_json::from_value(value).map_err(|e| CloudError::Protocol(format!("{what}: {e}")))
}

/// Step 1: ask Cloud for a device code; `label` is the user's `--label`, if
/// any (the approval page shows it, Cloud task #11).
pub fn start_device_flow(
    client: &CloudClient,
    label: Option<&str>,
) -> Result<DeviceCodeV1, CloudError> {
    start_device_flow_for(client, label, None)
}

/// Step 1 for `cloud connect [--trial]`: `trial` names the plan whose
/// one-step trial the browser starts (`None`: a plain sign-in, whose
/// request is exactly the one older Clouds know).
pub fn start_device_flow_for(
    client: &CloudClient,
    label: Option<&str>,
    trial: Option<TrialPlan>,
) -> Result<DeviceCodeV1, CloudError> {
    start_device_flow_with(client, label, trial, AppKeyOffers::default())
}

/// [`start_device_flow_for`] that announces the keys LeanCTX Code offers
/// (f3129 S2, app-key-bind-v1 §2): `app_keys` in `POST /v1/device/code`,
/// sorted by kind, absent when nothing is offered (the request is then
/// byte-identical to the plain one). A Cloud whose code answer does not
/// list `device_app_keys` binds no key: the pairing stops here, before the
/// browser opens or anything is polled (fail closed, §1).
pub fn start_device_flow_with(
    client: &CloudClient,
    label: Option<&str>,
    trial: Option<TrialPlan>,
    offers: AppKeyOffers<'_>,
) -> Result<DeviceCodeV1, CloudError> {
    let announced = offers.announcements();
    let mut plain =
        DeviceCodeRequestV1::new(env!("CARGO_PKG_VERSION"), engine_os()).with_trial(trial);
    if !announced.is_empty() {
        validate_app_keys(&announced).map_err(|e| CloudError::Config(e.to_string()))?;
        plain = plain.with_app_keys(announced.clone());
    }
    // The approval page names the machine from this label (rc3-5); one Cloud
    // would refuse (400) is trimmed and cut, else dropped: never fatal.
    let label = label.map(|l| {
        let cut: String = l.trim().chars().take(MAX_INSTANCE_LABEL_CHARS).collect();
        cut.trim_end().to_string()
    });
    let named = plain.clone().with_label(label);
    let request = if named.validate().is_ok() {
        named
    } else {
        plain
    };
    request
        .validate()
        .map_err(protocol("device code request"))?;
    let body = serde_json::to_value(&request).map_err(|e| CloudError::Protocol(e.to_string()))?;
    let answer = client.post("/device/code", &body).map_err(|error| {
        // An older Cloud's 400 on a trial with keys names the trial first.
        let error = trial_refusal(error, trial.is_some(), client.origin());
        app_keys_refusal(error, !announced.is_empty()).with_origin(client.origin())
    })?;
    let code: DeviceCodeV1 = decode(answer, "device code")?;
    code.validate().map_err(protocol("device code"))?;
    offers.require_capability(&code)?;
    // A Cloud that took the request but does not echo the trial would let
    // the person sign in without one: refused the same way as a 400.
    if let Some(plan) = trial
        && !code.starts_trial(plan)
    {
        return Err(CloudError::TrialUnsupported {
            origin: client.origin().trim_end_matches('/').to_string(),
        });
    }
    Ok(code)
}

/// A trial request an older Cloud refused for its unknown members (`400
/// invalid_request`) is [`CloudError::TrialUnsupported`]: it is never sent
/// again without them, so nobody signs in thinking a trial started.
pub fn trial_refusal(error: CloudError, trial: bool, origin: &str) -> CloudError {
    match error {
        CloudError::Server { status: 400, code } if trial && code == "invalid_request" => {
            CloudError::TrialUnsupported {
                origin: origin.trim_end_matches('/').to_string(),
            }
        }
        other => other,
    }
}

/// The words for a Cloud that cannot bind the keys of LeanCTX Code in the
/// pairing (its code answer lacks `device_app_keys`, or it refused the
/// announced `app_keys`): nothing was paired (app-key-bind-v1 §1).
pub const APP_KEYS_UNSUPPORTED: &str = "This Cloud cannot pair the keys of LeanCTX Code yet, \
     so nothing was paired. Connect again later, or connect without LeanCTX Code.";

/// A device-code request announcing app keys that an older Cloud refused for
/// its unknown member (`400 invalid_request`) is [`APP_KEYS_UNSUPPORTED`]:
/// never sent again without them, so no key is offered unannounced. A newer
/// Cloud that refuses the announcement itself (`400 invalid_field`, field
/// `app_keys`) reads [`APP_KEY_PAIRING_FAILED`]: nothing was paired.
fn app_keys_refusal(error: CloudError, announced: bool) -> CloudError {
    match error {
        CloudError::Server { status: 400, code } if announced && code == "invalid_request" => {
            CloudError::Config(APP_KEYS_UNSUPPORTED.into())
        }
        CloudError::Server { status: 400, code }
            if announced && code == super::error::APP_KEYS_INVALID =>
        {
            CloudError::Config(APP_KEY_PAIRING_FAILED.into())
        }
        other => other,
    }
}

/// The project an engine names at connect (C0 2.1 device setup): the
/// checkout's folder name and the remote it sees, plus what it shares.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetupAnnouncement {
    pub project: Option<(String, Option<String>)>,
    pub shares: Vec<String>,
}

/// Step 1b (C0 2.1): name the project and what this machine shares, so the
/// approval page asks "Share this project" too. Only when the Cloud offers
/// it; `false` when it did not take it (the terminal then asks, as before).
pub fn announce_setup(
    client: &CloudClient,
    code: &DeviceCodeV1,
    setup: &SetupAnnouncement,
) -> bool {
    if !code.offers_device_setup() {
        return false;
    }
    let request = DeviceSetupRequestV1 {
        schema: DEVICE_SETUP_REQUEST_SCHEMA.into(),
        device_code: code.device_code.clone(),
        project: setup
            .project
            .clone()
            .map(|(name, remote)| DeviceSetupProjectV1 { name, remote }),
        shares: setup.shares.clone(),
    };
    let Ok(body) = serde_json::to_value(&request) else {
        return false;
    };
    match client.post("/device/setup", &body) {
        Ok(_) => true,
        Err(error) => {
            tracing::info!("cloud device setup: {error}");
            false
        }
    }
}

/// What the device approval answered beyond the enrollment: the page's
/// answer to "Share this project" (C0 2.1). `None`: the page did not ask
/// (no setup, or an older Cloud); `Some(None)`: an older page approved it.
pub type SharingAnswer = Option<Option<bool>>;

/// Step 2: poll until the user approved (or the code expired), then verify
/// and persist the enrollment. `sleep` is injectable for tests.
pub fn finish_device_flow(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    label: &str,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    finish_device_flow_with(
        store,
        client,
        code,
        label,
        root,
        sleep,
        AppKeyOffers::default(),
    )
}

/// The keys LeanCTX Code may hand over with a pairing: its chat key
/// (rc4-32), its task-propose key (rc4-67) and its opt-in key (rc4-16), any
/// of them or none ([`Default`]).
#[derive(Debug, Clone, Copy, Default)]
pub struct AppKeyOffers<'a> {
    pub chat: Option<&'a ChatKeyOffer>,
    pub task: Option<&'a TaskKeyOffer>,
    pub optin: Option<&'a OptinKeyOffer>,
}

impl AppKeyOffers<'_> {
    /// Whether no key is offered (the plain pairing).
    pub fn is_empty(self) -> bool {
        self.chat.is_none() && self.task.is_none() && self.optin.is_none()
    }

    /// The `app_keys` of `POST /v1/device/code` (f3129 S2): one entry per
    /// offered kind, sorted by kind; empty when nothing is offered.
    pub fn announcements(self) -> Vec<AppKeyAnnouncementV1> {
        let entry = |kind, alg: &str, key_id: &str, public_key: &str| AppKeyAnnouncementV1 {
            kind,
            alg: alg.to_owned(),
            key_id: key_id.to_owned(),
            public_key: public_key.to_owned(),
        };
        let mut keys = Vec::new();
        if let Some(o) = self.chat {
            keys.push(entry(AppKeyKind::Chat, &o.alg, &o.key_id, &o.public_key));
        }
        if let Some(o) = self.optin {
            keys.push(entry(AppKeyKind::Optin, &o.alg, &o.key_id, &o.public_key));
        }
        if let Some(o) = self.task {
            keys.push(entry(
                AppKeyKind::TaskPropose,
                &o.alg,
                &o.key_id,
                &o.public_key,
            ));
        }
        keys.sort_by_key(|key| key.kind);
        keys
    }

    /// The announcement goes with the device-code request, before its
    /// answer; the offers and their bind signature go out only when that
    /// answer lists `device_app_keys` (app-key-bind-v1 §1). Otherwise
    /// nothing is bound and the pairing stops before anything is polled.
    pub fn require_capability(self, code: &DeviceCodeV1) -> Result<(), CloudError> {
        if self.is_empty() || code.offers_app_keys() {
            Ok(())
        } else {
            Err(CloudError::Config(APP_KEYS_UNSUPPORTED.into()))
        }
    }

    /// `app_keys_bind_signature` (f3129 S3): the instance key's Ed25519
    /// signature, base64url without padding, over the bind statement of
    /// `device_code`, the `instance_public_key` the request sends and the
    /// announced keys (exactly the offered ones). `None` when nothing is
    /// offered.
    fn bind_signature(
        self,
        keys: &InstanceKeys,
        device_code: &str,
    ) -> Result<Option<String>, CloudError> {
        use base64::Engine as _;
        use ed25519_dalek::Signer as _;
        if self.is_empty() {
            return Ok(None);
        }
        let input =
            app_key_bind_signing_input(device_code, &keys.public_key_b64(), &self.announcements())
                .map_err(|e| CloudError::Config(e.to_string()))?;
        let signature = keys.signing_key().sign(&input).to_bytes();
        Ok(Some(
            base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(signature),
        ))
    }

    /// Each offer must bind to exactly the `instance_public_key` the request
    /// sends; a bad one stops the pairing before anything is sent.
    fn check(self, keys: &InstanceKeys) -> Result<(), CloudError> {
        check_chat_key(keys, self.chat)?;
        check_task_key(keys, self.task)?;
        check_optin_key(keys, self.optin)
    }

    /// The offers' bind check against this computer's instance keys (made here if
    /// there are none yet, as the pairing would), for the connect to run
    /// before it contacts the Cloud at all: a bind that does not verify
    /// stops the connect before the device code is fetched or the browser
    /// opened (§46.3 point 1, §28a). Loads nothing when no key is offered.
    pub fn verify(self, store: &CloudStore) -> Result<(), CloudError> {
        if self.is_empty() {
            return Ok(());
        }
        self.check(&InstanceKeys::load_or_create(store)?)
    }

    /// Keep the public part of each accepted key. A kind not offered is
    /// forgotten only on a new enrollment (a key bound to an earlier
    /// enrollment is never kept); a re-pair of the same instance (`Rebind`)
    /// keeps it, because the Cloud keeps that kind bound (bd 40011 M2).
    fn keep(self, store: &CloudStore, pairing: Pairing) -> Result<(), CloudError> {
        keep_chat_key(store, self.chat, pairing)?;
        keep_task_key(store, self.task, pairing)?;
        keep_optin_key(store, self.optin, pairing)
    }

    /// The Cloud's refusal of an offered key in plain words (the task key's
    /// and the chat key's codes are distinct); `chat_advice` names the next
    /// step for a refused chat key.
    fn refusal(self, error: CloudError, chat_advice: &str) -> CloudError {
        let offered = !self.is_empty();
        let error = not_announced_refusal(error, offered);
        let error = task_key_refusal(error, self.task.is_some());
        let error = optin_key_refusal(error, self.optin.is_some());
        chat_key_refusal(error, self.chat.is_some(), chat_advice)
    }
}

/// [`finish_device_flow`] that also binds the keys LeanCTX Code offers
/// (chat key rc4-32, task-propose key rc4-67): each offer is checked against
/// this machine's public key before anything is sent, and its public part is
/// kept once the pairing is accepted. A pairing without one forgets a key of
/// that kind bound earlier.
pub fn finish_device_flow_with(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    label: &str,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
    offers: AppKeyOffers<'_>,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    finish_device_flow_at(
        store,
        client,
        code,
        label,
        root,
        sleep,
        offers,
        UtcDateTime::now,
    )
}

/// [`finish_device_flow_with`] with an injectable clock (`now` verifies the
/// lease).
#[allow(clippy::too_many_arguments)]
fn finish_device_flow_at(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    label: &str,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
    offers: AppKeyOffers<'_>,
    now: impl Fn() -> UtcDateTime,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    poll_device_flow(store, client, code, label, root, sleep, offers, now).map_err(|e| {
        forget_cleared_optin_key(store, offers.optin, &e);
        offers.refusal(e, CHAT_KEY_REFUSED)
    })
}

/// The Cloud removed the offered opt-in key (`optin_key_cleared`): the key
/// kept here, if it is that one, is pending again, so `cloud.pickup` stops
/// reporting `worker_on` (§46.3). A failure to write is logged, never
/// hides the refusal.
fn forget_cleared_optin_key(store: &CloudStore, offer: Option<&OptinKeyOffer>, error: &CloudError) {
    let cleared = matches!(error, CloudError::Server { code, .. }
        if code == super::error::OPTIN_KEY_CLEARED);
    let Some(offer) = offer.filter(|_| cleared) else {
        return;
    };
    if optin_key::pending_optin_key(store).is_some_and(|k| k.key_id == offer.key_id)
        && let Err(e) = optin_key::clear_accepted(store)
    {
        tracing::warn!("cloud connect: opt-in key back to pending: {e}");
    }
}

/// The words for a pairing whose task key LeanCTX Code offered was refused.
pub const TASK_KEY_REFUSED: &str =
    "The task key of LeanCTX Code was not accepted. Pair this computer again from LeanCTX Code.";
/// The words for a pairing while another task key is bound.
pub const TASK_KEY_ALREADY_BOUND: &str = "This computer has another task key already. \
     Remove it on the web, then pair this computer again from LeanCTX Code.";
/// The words for a pairing with a task key removed on the web.
pub const TASK_KEY_WAS_REMOVED: &str = "This task key was removed on the web. \
     LeanCTX Code makes a new one when you pair this computer again.";

/// The words for a pairing whose opt-in key LeanCTX Code offered was
/// refused (rc4-16).
pub const OPTIN_KEY_REFUSED: &str =
    "The opt-in key of LeanCTX Code was not accepted. Pair this computer again from LeanCTX Code.";
/// The words for a pairing while another opt-in key is bound (§46.4).
pub const OPTIN_KEY_ALREADY_BOUND: &str = "Another opt-in key is bound to this computer; \
     remove it on the computer's page, then pair again.";
/// The words for a pairing with an opt-in key removed on the web (§46.4).
pub const OPTIN_KEY_WAS_REMOVED: &str =
    "This opt-in key was removed; LeanCTX Code makes a new one and pairs again.";

/// The words for a pairing with an opt-in key of a computer connected with
/// an enrollment token (`400 token_enrollment`, §46.2).
pub const OPTIN_KEY_TOKEN_ENROLLMENT: &str = "This computer was connected with an enrollment \
     token; pairing the opt-in key needs a connect from LeanCTX Code.";

/// The words for a pairing whose offered keys the Cloud refused as not the
/// announced set or with a bind signature that does not verify (f3129
/// `app_key_not_announced` / `app_key_bind_invalid`, app-key-bind-v1 §11).
pub const APP_KEY_PAIRING_FAILED: &str = PAIRING_FAILED_TEXT;

fn not_announced_refusal(error: CloudError, offered: bool) -> CloudError {
    match error {
        CloudError::Server { code, .. }
            if offered
                && (code == super::error::APP_KEY_NOT_ANNOUNCED
                    || code == super::error::APP_KEY_BIND_INVALID) =>
        {
            CloudError::DeviceFlow(APP_KEY_PAIRING_FAILED.into())
        }
        other => other,
    }
}

fn task_key_refusal(error: CloudError, offered: bool) -> CloudError {
    use super::error::{TASK_KEY_BOUND, TASK_KEY_CLEARED, TASK_KEY_INVALID};
    match error {
        CloudError::Server { code, .. } if offered && code == TASK_KEY_INVALID => {
            CloudError::DeviceFlow(TASK_KEY_REFUSED.into())
        }
        CloudError::Server { code, .. } if offered && code == TASK_KEY_BOUND => {
            CloudError::DeviceFlow(TASK_KEY_ALREADY_BOUND.into())
        }
        CloudError::Server { code, .. } if offered && code == TASK_KEY_CLEARED => {
            CloudError::DeviceFlow(TASK_KEY_WAS_REMOVED.into())
        }
        other => other,
    }
}

fn optin_key_refusal(error: CloudError, offered: bool) -> CloudError {
    use super::error::{OPTIN_KEY_BOUND, OPTIN_KEY_CLEARED, OPTIN_KEY_INVALID};
    match error {
        CloudError::Server { code, .. } if offered && code == OPTIN_KEY_INVALID => {
            CloudError::DeviceFlow(OPTIN_KEY_REFUSED.into())
        }
        CloudError::Server { code, .. } if offered && code == OPTIN_KEY_BOUND => {
            CloudError::DeviceFlow(OPTIN_KEY_ALREADY_BOUND.into())
        }
        CloudError::Server { code, .. } if offered && code == OPTIN_KEY_CLEARED => {
            CloudError::DeviceFlow(OPTIN_KEY_WAS_REMOVED.into())
        }
        CloudError::Server { status: 400, code } if offered && code == "token_enrollment" => {
            CloudError::DeviceFlow(OPTIN_KEY_TOKEN_ENROLLMENT.into())
        }
        other => other,
    }
}

/// The local error for LeanCTX Cloud's `400 invalid_field` on a pairing
/// that carried a chat key (rc4-32): the bind did not verify there.
pub const CHAT_KEY_REFUSED: &str = "The Cloud refused the chat key of LeanCTX Code \
     (invalid_field chat_key). Pair this computer again from LeanCTX Code.";

fn chat_key_refusal(error: CloudError, offered: bool, advice: &str) -> CloudError {
    match error {
        CloudError::Server { status: 400, code }
            if offered && (code == "invalid_field" || code == "unknown") =>
        {
            CloudError::DeviceFlow(advice.into())
        }
        other => other,
    }
}

/// The instance this computer holds before a pairing, as far as "the same
/// instance" goes: its id, team and Cloud, the fields [`repair_chat_key_at`]
/// checks (its Cloud by the client it is sent with).
#[derive(Debug, Clone, PartialEq, Eq)]
struct HeldInstance {
    instance_id: String,
    org_id: String,
    url: String,
}

impl HeldInstance {
    /// The instance held now; an unreadable record counts as none (a new
    /// enrollment, which forgets keys not offered: fail closed).
    fn read(store: &CloudStore) -> Option<Self> {
        store.instance().ok().flatten().map(|record| Self {
            instance_id: record.instance_id,
            org_id: record.org_id,
            url: record.url,
        })
    }

    fn is(&self, record: &InstanceRecord) -> bool {
        self.instance_id == record.instance_id
            && self.org_id == record.org_id
            && self.url == record.url
    }
}

/// The device flow with an injectable clock (`now` verifies the lease).
#[allow(clippy::too_many_arguments)]
fn poll_device_flow(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    label: &str,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
    offers: AppKeyOffers<'_>,
    now: impl Fn() -> UtcDateTime,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    let keys = InstanceKeys::load_or_create(store)?;
    offers.check(&keys)?;
    offers.require_capability(code)?;
    let request = device_token_request(&keys, &code.device_code, label, offers)?;
    let body = poll_device_token(client, code, &request, sleep)?;
    // Read before the enrollment is accepted: that overwrites the record.
    let before = HeldInstance::read(store);
    let enrolled = accept_enrollment(store, client, body, label, root, now())?;
    offers.keep(store, Pairing::of(before.as_ref(), &enrolled.0))?;
    Ok(enrolled)
}

/// Poll `POST /v1/device/token` with `request` until the person approved
/// (the answer's body), refused, or the code expired.
fn poll_device_token(
    client: &CloudClient,
    code: &DeviceCodeV1,
    request: &Value,
    sleep: &dyn Fn(Duration),
) -> Result<Value, CloudError> {
    let expires_in = u64::from(code.expires_in);
    let deadline = std::time::Instant::now() + Duration::from_secs(expires_in);
    let mut poll = DevicePoll::new(code.interval);
    // Seconds slept so far: the deadline also holds when `sleep` does not
    // pass real time (tests), and never later than the wall clock's.
    let mut slept = 0u64;
    let mut wait = poll.interval;
    loop {
        let left = expires_in.saturating_sub(slept);
        let pause = wait.min(left.max(1));
        sleep(Duration::from_secs(pause));
        slept = slept.saturating_add(pause);
        match client.post("/device/token", request) {
            Ok(body) => return Ok(body),
            // A plans page named as a path is shown on this Cloud's origin.
            Err(error) => {
                wait = poll
                    .next_wait(error)
                    .map_err(|e| e.with_origin(client.origin()))?;
            }
        }
        if std::time::Instant::now() >= deadline || slept >= expires_in {
            return Err(CloudError::DeviceFlow(super::error::CODE_EXPIRED.into()));
        }
    }
}

/// The longest pause between two device-token polls after transient
/// failures without a `Retry-After` (seconds).
const DEVICE_POLL_BACKOFF_CAP_S: u64 = 60;

/// The pacing of the device-token polls (RFC 8628 §3.5): `authorization_pending`
/// keeps the interval, `slow_down` raises it by 5 s for good, a transient
/// failure (offline, timeout, 429, 5xx) waits its `Retry-After` or backs off
/// exponentially; every other answer (`access_denied`, `expired_token`,
/// `invalid_grant`, …) ends the flow.
#[derive(Debug)]
struct DevicePoll {
    interval: u64,
    /// Transient failures in a row.
    failures: u32,
}

impl DevicePoll {
    fn new(interval: u32) -> Self {
        Self {
            interval: u64::from(interval.max(1)),
            failures: 0,
        }
    }

    /// Seconds to wait before the next poll after `error`, or the error
    /// that ends the flow.
    fn next_wait(&mut self, error: CloudError) -> Result<u64, CloudError> {
        match &error {
            CloudError::Server { status: 400, code } if code == "authorization_pending" => {
                self.failures = 0;
                Ok(self.interval)
            }
            CloudError::Server { status: 400, code } if code == "slow_down" => {
                self.failures = 0;
                self.interval = self.interval.saturating_add(5);
                Ok(self.interval)
            }
            transient if transient.is_transient() => {
                self.failures = self.failures.saturating_add(1);
                let backoff = self
                    .interval
                    .saturating_mul(1u64 << self.failures.min(6))
                    .min(DEVICE_POLL_BACKOFF_CAP_S.max(self.interval));
                let asked = transient.retry_after_at(UtcDateTime::now()).unwrap_or(0);
                tracing::info!("cloud device token: {transient}; polling again");
                Ok(backoff.max(asked).max(self.interval))
            }
            _ => Err(error),
        }
    }
}

/// The `POST /v1/device/token` body: the device code and this instance's
/// public keys, plus the offered app keys with the instance key's
/// `app_keys_bind_signature` over them (f3129 S3): no offer goes out
/// unsigned.
fn device_token_request(
    keys: &InstanceKeys,
    device_code: &str,
    label: &str,
    offers: AppKeyOffers<'_>,
) -> Result<Value, CloudError> {
    let mut request = json!({
        "schema": DEVICE_TOKEN_REQUEST_SCHEMA,
        "device_code": device_code,
        "instance_public_key": keys.public_key_b64(),
        "instance_kx_public_key": keys.kx_public_key_b64(),
        "instance_label": label,
        "instance_version": env!("CARGO_PKG_VERSION"),
        "os": engine_os(),
    });
    // rc4-32, rc4-67: each only when LeanCTX Code hands one over; absent
    // otherwise, so an older Cloud that refuses unknown members serves every
    // plain pairing.
    if let Some(offer) = offers.chat {
        request["chat_key"] = offer.request_member();
    }
    if let Some(offer) = offers.task {
        request[task_key::REQUEST_MEMBER] = offer.request_member();
    }
    if let Some(offer) = offers.optin {
        request[optin_key::REQUEST_MEMBER] = offer.request_member();
    }
    if let Some(signature) = offers.bind_signature(keys, device_code)? {
        request["app_keys_bind_signature"] = Value::String(signature);
    }
    Ok(request)
}

/// The task key offer must bind to exactly the `instance_public_key` this
/// request sends; a bad one stops the pairing before anything is sent.
fn check_task_key(keys: &InstanceKeys, offer: Option<&TaskKeyOffer>) -> Result<(), CloudError> {
    match offer {
        Some(offer) => offer
            .check(&keys.public_key_b64())
            .map_err(CloudError::Config),
        None => Ok(()),
    }
}

/// The opt-in key offer must bind to exactly the `instance_public_key` this
/// request sends (its own signature over the bind, §46.3); a bad one stops
/// the pairing before anything is sent.
fn check_optin_key(keys: &InstanceKeys, offer: Option<&OptinKeyOffer>) -> Result<(), CloudError> {
    match offer {
        Some(offer) => offer
            .check(&keys.public_key_b64())
            .map_err(CloudError::Config),
        None => Ok(()),
    }
}

/// The chat key offer must bind to exactly the `instance_public_key` this
/// request sends; a bad one stops the pairing before anything is sent.
fn check_chat_key(keys: &InstanceKeys, chat_key: Option<&ChatKeyOffer>) -> Result<(), CloudError> {
    match chat_key {
        Some(offer) => offer
            .check(&keys.public_key_b64())
            .map_err(CloudError::Config),
        None => Ok(()),
    }
}

/// Whether a pairing enrolled a new instance or re-paired the one this
/// computer already had (the same id, team and Cloud: [`HeldInstance`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Pairing {
    New,
    Rebind,
}

impl Pairing {
    /// `before` is the instance held before the pairing (`None`: none, or
    /// an unreadable record).
    fn of(before: Option<&HeldInstance>, after: &InstanceRecord) -> Self {
        if before.is_some_and(|held| held.is(after)) {
            Pairing::Rebind
        } else {
            Pairing::New
        }
    }
}

/// Keep the public part of an accepted task key (never private material).
/// A new enrollment replaces it: the new offer, or none at all (a key bound
/// to an earlier enrollment is never kept). A rebind without an offer keeps
/// the bound key.
fn keep_task_key(
    store: &CloudStore,
    offer: Option<&TaskKeyOffer>,
    pairing: Pairing,
) -> Result<(), CloudError> {
    match (offer, pairing) {
        (Some(offer), _) => task_key::store_bound(store, offer),
        (None, Pairing::New) => task_key::remove_bound(store),
        (None, Pairing::Rebind) => Ok(()),
    }
}

/// Keep the public part of an accepted opt-in key (rc4-16), marked accepted:
/// the Cloud's 2xx token answer took it, so `cloud.pickup` reports
/// `worker_on` once the Cloud announces `instance_optin_key`. A new
/// enrollment without an offer puts a kept key back to pending (a key bound
/// to an earlier enrollment is never usable; the app pairs it again); a
/// rebind without an offer keeps it as it is.
fn keep_optin_key(
    store: &CloudStore,
    offer: Option<&OptinKeyOffer>,
    pairing: Pairing,
) -> Result<(), CloudError> {
    match (offer, pairing) {
        (Some(offer), _) => {
            optin_key::store_pending(store, offer)?;
            optin_key::mark_accepted(store, &offer.key_id).map(|_| ())
        }
        (None, Pairing::New) => optin_key::clear_accepted(store),
        (None, Pairing::Rebind) => Ok(()),
    }
}

/// Keep the public part of an accepted chat key (never private material).
/// A new enrollment replaces the bound chat key: the new offer, or none at
/// all (a key bound to an earlier enrollment is never kept). The re-pair of
/// a connected computer ([`repair_chat_key`]) stores its offer the same way.
/// A rebind without an offer keeps the bound key.
fn keep_chat_key(
    store: &CloudStore,
    chat_key: Option<&ChatKeyOffer>,
    pairing: Pairing,
) -> Result<(), CloudError> {
    match (chat_key, pairing) {
        (Some(offer), _) => crate::consent::chat_key::store_bound(store, offer),
        (None, Pairing::New) => store.remove_config(crate::consent::chat_key::CHAT_KEY_FILE),
        (None, Pairing::Rebind) => Ok(()),
    }
}

/// Verify and persist the enrollment Cloud answered at `/device/token`.
fn accept_enrollment(
    store: &CloudStore,
    client: &CloudClient,
    body: Value,
    label: &str,
    root: &[u8; 32],
    local_now: UtcDateTime,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    let keys_raw = body["keys"].clone();
    let sharing: SharingAnswer = body
        .get("sharing")
        .filter(|s| s.is_object())
        .map(|s| s["share"].as_bool());
    decode::<EnrollmentV1>(body, "enrollment")
        .and_then(|enrollment| {
            complete_enrollment(
                store,
                client.origin(),
                &enrollment,
                &keys_raw,
                root,
                label,
                local_now,
            )
        })
        .map(|instance| (instance, sharing))
        .map_err(|error| unenrolled(store, label, error))
}

/// What a chat key re-pair of a connected computer did (rc4-32, p37
/// §3.18.1): its `reason` in `cloud connect --json`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChatKeyRepair {
    /// A chat key is bound now (none was, or LeanCTX Code made a new one).
    Bound,
    /// LeanCTX Cloud already had this chat key for this computer.
    Unchanged,
}

impl ChatKeyRepair {
    /// The `reason` LeanCTX Code branches on.
    #[must_use]
    pub fn reason(self) -> &'static str {
        match self {
            Self::Bound => "bound",
            Self::Unchanged => "unchanged",
        }
    }
}

/// Another chat key is bound to this computer (`409 invalid_state`, reason
/// `chat_key_bound`).
pub const CHAT_KEY_BOUND: &str = "Another chat key is bound to this computer; remove it on the computer's page, then pair again.";

/// The offered chat key was removed (`409`, reason `chat_key_cleared`).
pub const CHAT_KEY_CLEARED: &str =
    "This chat key was removed; LeanCTX Code makes a new one and pairs again.";

/// A computer connected with an enrollment token (`400 token_enrollment`).
pub const CHAT_KEY_TOKEN_ENROLLMENT: &str = "This computer was connected with an enrollment token; pairing the chat key needs a connect from LeanCTX Code.";

/// The answers of a chat key re-pair that name why it was refused, kept by
/// `CloudError::from_response` as `Server { code }` (this protocol
/// version does not name them).
pub const CHAT_KEY_REPAIR_REASONS: &[&str] =
    &["chat_key_bound", "chat_key_cleared", "token_enrollment"];

/// A refused chat key re-pair: its `reason` (absent for `token_enrollment`,
/// for `invalid_field chat_key` and for an app-key refusal, whose event
/// `code` is `app_key_not_announced`) and fixed text; `None` for
/// any other error.
#[must_use]
pub fn chat_key_repair_refusal(error: &CloudError) -> Option<(Option<&'static str>, &'static str)> {
    match error {
        CloudError::Server { status: 409, code } if code == "chat_key_bound" => {
            Some((Some("chat_key_bound"), CHAT_KEY_BOUND))
        }
        CloudError::Server { status: 409, code } if code == "chat_key_cleared" => {
            Some((Some("chat_key_cleared"), CHAT_KEY_CLEARED))
        }
        CloudError::Server { status: 400, code } if code == "token_enrollment" => {
            Some((None, CHAT_KEY_TOKEN_ENROLLMENT))
        }
        CloudError::DeviceFlow(why) if why == CHAT_KEY_REFUSED => Some((None, CHAT_KEY_REFUSED)),
        CloudError::DeviceFlow(why) | CloudError::Config(why) if why == APP_KEY_PAIRING_FAILED => {
            Some((None, APP_KEY_PAIRING_FAILED))
        }
        CloudError::Config(why) if why == APP_KEYS_UNSUPPORTED => {
            Some((None, APP_KEYS_UNSUPPORTED))
        }
        _ => None,
    }
}

/// Pair LeanCTX Code's chat key with this ALREADY connected computer
/// (p37 §3.18.1): the normal device flow (`code` from
/// [`start_device_flow_for`], the person approves in the browser) with the
/// existing instance keys plus `chat_key`, and no `replaces`. LeanCTX Cloud
/// keeps the instance, its keys, vault links and workers; locally the
/// enrollment record, vault links and the worker state stay as they are:
/// only the lease (and the cached session token) the answer renews and the
/// bound chat key change.
pub fn repair_chat_key(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
    chat_key: &ChatKeyOffer,
) -> Result<ChatKeyRepair, CloudError> {
    repair_chat_key_at(store, client, code, root, sleep, chat_key, UtcDateTime::now)
}

fn repair_chat_key_at(
    store: &CloudStore,
    client: &CloudClient,
    code: &DeviceCodeV1,
    root: &[u8; 32],
    sleep: &dyn Fn(Duration),
    chat_key: &ChatKeyOffer,
    now: impl Fn() -> UtcDateTime,
) -> Result<ChatKeyRepair, CloudError> {
    let instance = store.instance()?.ok_or(CloudError::NotConnected)?;
    // The keys this computer is connected with; never new ones.
    let keys = InstanceKeys::load(store)?;
    check_chat_key(&keys, Some(chat_key))?;
    let offers = AppKeyOffers {
        chat: Some(chat_key),
        ..AppKeyOffers::default()
    };
    offers.require_capability(code)?;
    let request = device_token_request(&keys, &code.device_code, &instance.label, offers)?;
    let body = poll_device_token(client, code, &request, sleep).map_err(|e| {
        // f3129 §11: a refused announced set or bind signature reads
        // "Pairing failed", not its bare 400.
        let e = not_announced_refusal(e, true);
        chat_key_refusal(e, true, CHAT_KEY_REFUSED)
    })?;
    let keys_raw = body["keys"].clone();
    let enrollment = decode::<EnrollmentV1>(body, "enrollment")?;
    enrollment.validate().map_err(protocol("enrollment"))?;
    if enrollment.instance_id != instance.instance_id || enrollment.org.id != instance.org_id {
        return Err(CloudError::Protocol(
            "the chat key pairing answered for another computer".into(),
        ));
    }
    let local_now = now();
    let keyset = verify_keyset(&keys_raw, root)?;
    let verified = verify_lease(
        &enrollment.lease,
        &keyset,
        &instance.instance_id,
        local_now,
        None,
    )?;
    if verified.lease.org_id != instance.org_id {
        return Err(CloudError::Protocol(
            "the lease is for another organization".into(),
        ));
    }
    store_lease(store, &verified, &keys_raw, local_now)?;
    // A session token of the earlier lease is fetched again.
    store.remove_config(TOKEN_FILE)?;
    let before = crate::consent::chat_key::bound_chat_key(store);
    keep_chat_key(store, Some(chat_key), Pairing::Rebind)?;
    Ok(match before {
        Some(bound) if bound.key_id == chat_key.key_id => ChatKeyRepair::Unchanged,
        _ => ChatKeyRepair::Bound,
    })
}

/// The plaintext prefix of a pre-issued Host enrollment token.
pub const ENROLLMENT_TOKEN_PREFIX: &str = "het_";

/// Why a Cloud refused an enrollment token, in the words a person acts on.
pub const ENROLLMENT_TOKEN_UNUSABLE: &str =
    "the enrollment token is unknown, expired, revoked or used up; ask a team admin for a new one";

/// A pre-issued Host enrollment token (`het_` and 64 hex digits) that a
/// team admin handed out, e.g. through MDM. It enrolls this machine without
/// a browser approval. The value is wiped on drop and never printed: its
/// `Debug` shows the prefix only, and no error names it.
pub struct EnrollmentToken(zeroize::Zeroizing<String>);

impl EnrollmentToken {
    /// Check the format before anything is sent: surrounding whitespace
    /// (a trailing newline from stdin) is dropped, everything else must be
    /// `het_` and exactly 64 hex digits.
    pub fn parse(raw: &str) -> Result<Self, CloudError> {
        let value = raw.trim();
        let well_formed = value
            .strip_prefix(ENROLLMENT_TOKEN_PREFIX)
            .is_some_and(|hex| hex.len() == 64 && hex.bytes().all(|b| b.is_ascii_hexdigit()));
        if !well_formed {
            return Err(CloudError::Config(
                "the enrollment token is malformed: it is `het_` followed by 64 hex digits, as the team's Hosts page issued it".into(),
            ));
        }
        Ok(Self(zeroize::Zeroizing::new(value.to_string())))
    }

    fn expose(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Debug for EnrollmentToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "EnrollmentToken({ENROLLMENT_TOKEN_PREFIX}…)")
    }
}

/// Enroll this machine with a pre-issued enrollment token (no browser):
/// one `POST /v1/device/token` with the token as `device_code`, then the
/// enrollment is verified and persisted exactly as after a device approval.
pub fn enroll_with_token(
    store: &CloudStore,
    client: &CloudClient,
    token: &EnrollmentToken,
    label: &str,
    root: &[u8; 32],
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    enroll_with_token_chat(store, client, token, label, root, None)
}

/// An enrollment token has no device code to sign the app keys over (f3129
/// S3), so it binds none: a chat key with a token is refused before
/// anything is sent, like the task and opt-in keys.
pub const CHAT_KEY_WITH_TOKEN: &str = "The chat key of LeanCTX Code goes with a pairing \
     approved in the browser, not with an enrollment token; nothing was sent.";

/// [`enroll_with_token`] as LeanCTX Code calls it: a chat key offer is
/// refused (`config`) before the Cloud is contacted ([`CHAT_KEY_WITH_TOKEN`]).
pub fn enroll_with_token_chat(
    store: &CloudStore,
    client: &CloudClient,
    token: &EnrollmentToken,
    label: &str,
    root: &[u8; 32],
    chat_key: Option<&ChatKeyOffer>,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    if chat_key.is_some() {
        return Err(CloudError::Config(CHAT_KEY_WITH_TOKEN.into()));
    }
    enroll_with_token_at(
        store,
        client,
        token,
        label,
        root,
        lean_ctx_time::UtcDateTime::now,
    )
}

fn enroll_with_token_at(
    store: &CloudStore,
    client: &CloudClient,
    token: &EnrollmentToken,
    label: &str,
    root: &[u8; 32],
    now: impl Fn() -> UtcDateTime,
) -> Result<(InstanceRecord, SharingAnswer), CloudError> {
    let keys = InstanceKeys::load_or_create(store)?;
    // A token pairing offers no app key: nobody approved this computer's
    // keys as the person, and there is no device code to sign them over.
    let offers = AppKeyOffers::default();
    let request = device_token_request(&keys, token.expose(), label, offers)?;
    match client.post("/device/token", &request) {
        Ok(body) => {
            let enrolled = accept_enrollment(store, client, body, label, root, now())?;
            // A token pairing drops a stored key (local-read-v1 §28a): the
            // person did not approve this pairing's keys.
            offers.keep(store, Pairing::New)?;
            Ok(enrolled)
        }
        Err(error) => Err(token_refusal(error)),
    }
}

/// An enrollment token's refusal: Cloud answers `expired_token` for an
/// unknown, expired, revoked or used-up token (mapped to the device
/// code's wording by the transport), named here for a token.
fn token_refusal(error: CloudError) -> CloudError {
    match error {
        CloudError::DeviceFlow(why) if why == super::error::CODE_EXPIRED => {
            CloudError::DeviceFlow(ENROLLMENT_TOKEN_UNUSABLE.into())
        }
        CloudError::Server { status: 400, code } if code == "expired_token" => {
            CloudError::DeviceFlow(ENROLLMENT_TOKEN_UNUSABLE.into())
        }
        other => other,
    }
}

/// Cloud enrolled the instance key, but this engine refused the answer
/// (Cloud task #1): the key is spent, since Cloud now knows it and refuses
/// it a second time ("instance key is already enrolled"). A machine without
/// an enrollment of its own drops it with whatever the refused answer left,
/// so the next connect starts from a fresh key, and the error says so.
fn unenrolled(store: &CloudStore, label: &str, error: CloudError) -> CloudError {
    if !matches!(store.instance(), Ok(None)) {
        return error;
    }
    let next = match store.forget(true) {
        Ok(()) => format!(
            "Its unused instance key was removed: fix the cause and run `lean-ctx cloud connect` again. The Cloud may list an instance \"{label}\" that never connected; remove it there."
        ),
        Err(e) => format!(
            "Its unused instance key could not be removed ({e}); run `lean-ctx cloud disconnect` before connecting again."
        ),
    };
    match error {
        CloudError::Protocol(m) => CloudError::Protocol(format!("{m}. {next}")),
        CloudError::Config(m) => CloudError::Config(format!("{m}. {next}")),
        other => other,
    }
}

/// Verify an enrollment answer against the root key and persist it.
pub fn complete_enrollment(
    store: &CloudStore,
    origin: &str,
    enrollment: &EnrollmentV1,
    keys_raw: &Value,
    root: &[u8; 32],
    label: &str,
    local_now: UtcDateTime,
) -> Result<InstanceRecord, CloudError> {
    enrollment.validate().map_err(protocol("enrollment"))?;
    let keyset = verify_keyset(keys_raw, root)?;
    let verified = verify_lease(
        &enrollment.lease,
        &keyset,
        &enrollment.instance_id,
        local_now,
        None,
    )?;
    if verified.lease.org_id != enrollment.org.id {
        return Err(CloudError::Protocol(
            "the lease is for another workspace".into(),
        ));
    }
    // `cloud.org` / `cloud.url` of a managed machine: refused before anything
    // of this enrollment is stored.
    if let Some(refusal) = super::config::managed_enrollment_refusal(&enrollment.org.id, origin) {
        return Err(refusal);
    }
    store_lease(store, &verified, keys_raw, local_now)?;
    // Read before the new record replaces it: the Cloud and account a vault
    // map without an origin (or with a Cloud alone) was resolved for (rc3-81).
    let previous = store.instance().ok().flatten().map(|own| own.vault_scope());
    let record = InstanceRecord {
        instance_id: enrollment.instance_id.clone(),
        org_id: enrollment.org.id.clone(),
        org_name: enrollment.org.name.clone(),
        org_kind: enrollment.org.kind.clone(),
        company_name: enrollment.org.company.as_ref().map(|c| c.name.clone()),
        // rc4-1: the name chosen on the approval page wins over the one sent.
        label: enrollment.chosen_label().unwrap_or(label).to_string(),
        url: origin.to_string(),
        org_salt_id: enrollment.org_salt_id.clone(),
        web_url: enrollment
            .web_url
            .as_deref()
            .and_then(|url| super::config::normalize_origin(url).ok()),
        enrolled_at: stamp(local_now),
    };
    // Local Cloud data of another server or team goes before anything of
    // this enrolment reads it (3.12.8 account scope).
    store.scope_to_enrollment(&record)?;
    super::worker::prepare_enrollment(store)?;
    // Vaults another Cloud or account resolved are never shown or synced here. It runs
    // before the instance record is written: when it fails, the enrollment
    // fails, `projects.json` stays as it was (written only when the switch
    // is saved whole), and `unenrolled` drops the new lease and keys of a
    // machine without an earlier enrollment (`forget(true)`); a retry
    // starts the switch again.
    super::vault::enter_scope(store.data_dir(), &record.vault_scope(), previous.as_deref())?;
    store.write_config(INSTANCE_FILE, &record)?;
    // A new enrolment starts a new sharing period (Cloud' `since`): an
    // earlier engine-side off no longer bounds it (K2 review 3, NEW-1).
    super::metrics_v2::buffer::clear_local_on_since(store.data_dir());
    store.remove_config(TOKEN_FILE)?;
    store.record_success("enroll", &stamp(verified.service_time(local_now).to_utc()));
    Ok(record)
}

/// A connected instance: its record, verified lease and keys.
pub struct Session {
    pub store: CloudStore,
    pub instance: InstanceRecord,
    pub lease: VerifiedLease,
    root: [u8; 32],
}

impl Session {
    /// Load and re-verify the local enrollment. `root` is the root key of the
    /// origin the instance is enrolled with.
    pub fn load(store: CloudStore, root: [u8; 32]) -> Result<Self, CloudError> {
        // Scoped: local data of another account is gone before a session
        // reads or sends any of it (3.12.8 account scope).
        let instance = store.instance()?.ok_or(CloudError::NotConnected)?;
        // A revoked instance fails closed here, before any Cloud call.
        if let Some(revoked) = super::revoked::recorded(&store, &instance) {
            return Err(revoked);
        }
        // An enrollment from before the machine was managed, with another
        // org or Cloud, is not used.
        if let Some(refusal) =
            super::config::managed_enrollment_refusal(&instance.org_id, &instance.url)
        {
            return Err(refusal);
        }
        let lease = load_lease(&store, &root, &instance.instance_id)?;
        super::root_cert::check_lease_org(&store, &instance.url, &lease.lease)?;
        Ok(Self {
            store,
            instance,
            lease,
            root,
        })
    }

    /// The root key the enrollment was verified with.
    pub fn root_key(&self) -> &[u8; 32] {
        &self.root
    }

    fn service_now(&self) -> UtcDateTime {
        self.lease.service_time(UtcDateTime::now()).to_utc()
    }

    /// The current access token (minted when needed), for transports that
    /// authenticate outside [`CloudClient`] (the bus WebSocket upgrade).
    /// `force` mints a new one.
    pub fn bearer_token(&self, force: bool) -> Result<zeroize::Zeroizing<String>, CloudError> {
        self.access_token(force).map(zeroize::Zeroizing::new)
    }

    /// An authenticated client, minting an access token when needed.
    pub fn client(&self) -> Result<CloudClient, CloudError> {
        let token = self.access_token(false)?;
        Ok(CloudClient::new(&self.instance.url)?.with_token(&self.instance.instance_id, &token))
    }

    /// Run `call` with an authenticated client; on 401 mint a new token once.
    pub fn with_client<T>(
        &self,
        call: impl Fn(&CloudClient) -> Result<T, CloudError>,
    ) -> Result<T, CloudError> {
        let result = match call(&self.client()?) {
            Err(CloudError::Unauthorized) => {
                let token = self.access_token(true)?;
                let client = CloudClient::new(&self.instance.url)?
                    .with_token(&self.instance.instance_id, &token);
                call(&client)
            }
            other => other,
        };
        result.map_err(|e| self.noted(e))
    }

    /// A revocation answer is recorded (the engine then fails closed) and
    /// named with this workspace; a used-up quota holds the paths it counts
    /// (`throttle`); a feature's `plan_required` names the lease's plan;
    /// any other error passes unchanged.
    pub(crate) fn noted(&self, error: CloudError) -> CloudError {
        let error = super::revoked::note(&self.store, &self.instance, error)
            .with_plan(self.lease.lease.plan);
        super::throttle::note_on(&self.store, None, error, lean_ctx_time::UtcDateTime::now())
    }

    /// An authenticated client for one call that must end by `deadline`,
    /// minting the token (if needed) inside the same budget and without the
    /// 401 retry of [`Self::with_client`] (C5c, rule 3 exception).
    pub fn bounded_client(&self, deadline: std::time::Instant) -> Result<CloudClient, CloudError> {
        let left = |what: &str| {
            deadline
                .checked_duration_since(std::time::Instant::now())
                .filter(|d| !d.is_zero())
                .ok_or_else(|| CloudError::Offline(format!("{what}: time budget used up")))
        };
        let mint = CloudClient::bounded(&self.instance.url, left("token")?)?;
        let token = self.access_token_via(false, &mint)?;
        Ok(CloudClient::bounded(&self.instance.url, left("request")?)?
            .with_token(&self.instance.instance_id, &token))
    }

    /// The token is minted in C0 2.0 like every other request.
    fn access_token(&self, force: bool) -> Result<String, CloudError> {
        self.access_token_via(force, &CloudClient::new(&self.instance.url)?)
    }

    fn access_token_via(&self, force: bool, client: &CloudClient) -> Result<String, CloudError> {
        let now = self.service_now().timestamp();
        if !force
            && let Some(cached) = self.store.read_config::<TokenRecord>(TOKEN_FILE)?
            && cached.expires_at - TOKEN_REFRESH_MARGIN_SECONDS > now
        {
            return Ok(cached.access_token);
        }
        // A 429 of the mint holds every process of this machine (they share
        // the instance key and the server's budget, which counts refused
        // attempts too): nothing is sent until it ends, a forced or manual
        // mint neither.
        if let Some(held) = super::throttle::token_hold(&self.store, UtcDateTime::now()) {
            return Err(held);
        }
        let keys = InstanceKeys::load(&self.store)?;
        let mut nonce_bytes = [0u8; 18];
        getrandom::fill(&mut nonce_bytes).map_err(|e| CloudError::Local(format!("random: {e}")))?;
        use base64::Engine as _;
        let nonce = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(nonce_bytes);
        let assertion = keys.sign_b64(&token_assertion_input(
            &self.instance.instance_id,
            now,
            &nonce,
        ));
        let request = json!({
            "schema": TOKEN_REQUEST_SCHEMA, "iat": now, "nonce": nonce, "assertion": assertion,
        });
        let path = format!("/instances/{}/token", self.instance.instance_id);
        let answer = client.post(&path, &request).map_err(|e| {
            super::throttle::note_token_limit(&self.store, self.noted(e), UtcDateTime::now())
        })?;
        let token: AccessTokenV1 = decode(answer, "access token")?;
        token.validate().map_err(protocol("access token"))?;
        super::throttle::token_minted(&self.store);
        self.store.write_config(
            TOKEN_FILE,
            &TokenRecord {
                access_token: token.access_token.clone(),
                expires_at: now + i64::from(token.expires_in),
            },
        )?;
        self.store
            .record_success("token", &stamp(self.service_now()));
        Ok(token.access_token)
    }

    /// Renew the lease and persist it; returns the new lease. Asked in C0
    /// 2.0 like the enrollment whose lease it replaces, and checked against
    /// the key set that holds its `kid` (which also feeds the runtime lease):
    /// the stored one, else key set v3, by which a C0 2.x Cloud signs leases
    /// once it serves v3, fetched once when it is not stored yet. The key
    /// set the lease verified with is stored with it, so every later load
    /// re-verifies it the same way.
    pub fn renew(&mut self) -> Result<&VerifiedLease, CloudError> {
        let path = format!("/instances/{}/lease/renew", self.instance.instance_id);
        let body = self.with_client(|client| client.post(&path, &json!({})))?;
        let local_now = UtcDateTime::now();
        let envelope: CloudLeaseEnvelopeV1 = decode(body, "lease")?;
        let stored: Value = self
            .store
            .read_config(super::state::KEYS_FILE)?
            .ok_or(CloudError::NotConnected)?;
        let pick = |session: &Self| {
            super::lease::renewal_key_set(
                &stored,
                super::keyset_v3::stored_raw(&session.store).as_ref(),
                &session.root,
                &envelope.kid,
            )
        };
        let keys_raw = if let Some(raw) = pick(self) {
            raw
        } else {
            super::keyset_v3::refresh(self)?;
            pick(self).ok_or_else(|| {
                CloudError::Protocol(format!(
                    "the renewed lease names key {}, which no verified key set holds",
                    envelope.kid
                ))
            })?
        };
        let keyset = verify_keyset(&keys_raw, &self.root)?;
        let verified = verify_lease(
            &envelope,
            &keyset,
            &self.instance.instance_id,
            local_now,
            None,
        )?;
        store_lease(&self.store, &verified, &keys_raw, local_now)?;
        self.store
            .record_success("lease", &stamp(verified.service_time(local_now).to_utc()));
        self.store.record_renew_error(None);
        self.lease = verified;
        Ok(&self.lease)
    }

    /// Free the seat in Cloud.
    pub fn delete_instance(&self) -> Result<(), CloudError> {
        let path = format!("/instances/{}", self.instance.instance_id);
        self.with_client(|client| client.delete(&path))
    }
}

/// A session loaded from the enrollment fixture (key set v1) in a fresh
/// store, for tests of passes that take a [`Session`] but must send
/// nothing. Its origin is never contacted by such a test.
#[cfg(test)]
pub(crate) fn fixture_session() -> (tempfile::TempDir, Session) {
    fixture_session_at("https://cloud.leanctx.com")
}

/// [`fixture_session`] enrolled with `origin` (a loopback Cloud of a test).
#[cfg(test)]
pub(crate) fn fixture_session_at(origin: &str) -> (tempfile::TempDir, Session) {
    let path = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../lean-ctx-protocol/tests/fixtures/cloud/enrollment.response.json"
    );
    let raw: Value = serde_json::from_slice(&std::fs::read(path).expect("fixture")).expect("json");
    let root = ed25519_dalek::SigningKey::from_bytes(&[0x52; 32])
        .verifying_key()
        .to_bytes();
    let dir = tempfile::tempdir().expect("tempdir");
    let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
    let enrollment: EnrollmentV1 = decode(raw.clone(), "enrollment").expect("enrollment");
    complete_enrollment(
        &store,
        origin,
        &enrollment,
        &raw["keys"],
        &root,
        "studio-mac",
        lean_ctx_time::OffsetDateTime::parse_from_rfc3339("2026-10-20T12:00:00Z")
            .expect("time")
            .to_utc(),
    )
    .expect("enrolled");
    let session = Session::load(store, root).expect("session");
    (dir, session)
}

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

    /// rc3-5: the approval page names the machine from `/device/code`; a
    /// label Cloud would refuse is trimmed, cut or dropped, never a 400.
    #[test]
    fn the_device_code_request_names_the_machine() {
        let long = "m".repeat(MAX_INSTANCE_LABEL_CHARS + 9);
        for (label, sent) in [
            ("  build-host  ", Some("build-host".to_string())),
            (long.as_str(), Some("m".repeat(MAX_INSTANCE_LABEL_CHARS))),
            ("bad\u{7}label", None),
            ("   ", None),
        ] {
            let (origin, recorded) = fake_cloud((200, fixture("cloud/device_code.response.json")));
            let client = CloudClient::new(&origin).unwrap();
            start_device_flow(&client, Some(label)).expect("login still starts");
            let body = recorded.lock().unwrap()[0].1.clone();
            assert_eq!(recorded.lock().unwrap()[0].0, "/v1/device/code");
            assert_eq!(body.get("instance_label").cloned(), sent.map(Value::from));
        }
    }

    /// An older Cloud refuses the trial members (`deny_unknown_fields`):
    /// `trial_unsupported`, never a silent sign-in; a sign-in's own 400 and
    /// every other trial refusal stay what they were.
    #[test]
    fn trial_request_refused_as_unknown_is_trial_unsupported() {
        let refused = || CloudError::Server {
            status: 400,
            code: "invalid_request".into(),
        };
        let mapped = trial_refusal(refused(), true, "https://cloud.leanctx.com/");
        assert_eq!(
            mapped,
            CloudError::TrialUnsupported {
                origin: "https://cloud.leanctx.com".into()
            }
        );
        assert_eq!(mapped.event_code(), "trial_unsupported");
        assert_eq!(
            mapped.to_string(),
            "LeanCTX does not offer the one-step trial here yet. Start the trial on https://cloud.leanctx.com, or sign in with `lean-ctx cloud connect`"
        );
        // Printed by the terminal and by LeanCTX Code alike.
        assert!(!mapped.to_string().contains("app"));
        assert_eq!(
            trial_refusal(refused(), false, "https://cloud.leanctx.com"),
            refused()
        );
        let offline = CloudError::Offline("reset".into());
        assert_eq!(
            trial_refusal(offline.clone(), true, "https://cloud.leanctx.com"),
            offline
        );
    }

    fn fixture(path: &str) -> Value {
        let path = format!(
            "{}/crates/lean-ctx-protocol/tests/fixtures/{path}",
            concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
        );
        serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
    }

    fn at(t: &str) -> UtcDateTime {
        lean_ctx_time::OffsetDateTime::parse_from_rfc3339(t)
            .unwrap()
            .to_utc()
    }

    /// Enroll `raw` in a fresh store; the directory lives as long as the
    /// returned guard.
    fn enroll(
        raw: &Value,
        root: &[u8; 32],
    ) -> Result<(tempfile::TempDir, CloudStore, InstanceRecord), CloudError> {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let enrollment: EnrollmentV1 = decode(raw.clone(), "enrollment")?;
        let record = complete_enrollment(
            &store,
            "https://cloud.leanctx.com",
            &enrollment,
            &raw["keys"],
            root,
            "studio-mac",
            at("2026-10-20T12:00:00Z"),
        )?;
        let lease = load_lease(&store, root, &record.instance_id)?;
        assert_eq!(lease.envelope.kid, enrollment.lease.kid);
        Ok((dir, store, record))
    }

    /// rc4-274: in a single-dir layout (a legacy `~/.lean-ctx`, a custom
    /// `LEAN_CTX_DATA_DIR`) `<config>/cloud` is `<data>/cloud`. The account
    /// scope of the enrolment removes only the data there: the instance key
    /// made before the approval, the lease and the key set stay, so the
    /// approved connect is connected.
    #[test]
    fn enrolling_in_a_single_dir_layout_keeps_the_keys_and_the_lease() {
        let raw = fixture("cloud/enrollment.response.json");
        let root = ed25519_dalek::SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let dir = tempfile::tempdir().unwrap();
        let cloud = dir.path().join("cloud");
        let store = CloudStore::at(&cloud, &cloud);
        let keys = crate::instance_key::InstanceKeys::load_or_create(&store).unwrap();
        // Data an older version left without a stamp: another account's.
        std::fs::write(cloud.join("projects.json"), b"{\"projects\":{}}").unwrap();
        let enrollment: EnrollmentV1 = decode(raw.clone(), "enrollment").unwrap();
        let record = complete_enrollment(
            &store,
            "https://cloud.leanctx.com",
            &enrollment,
            &raw["keys"],
            &root,
            "studio-mac",
            at("2026-10-20T12:00:00Z"),
        )
        .unwrap();

        assert!(!cloud.join("projects.json").exists(), "the data goes");
        let kept = crate::instance_key::InstanceKeys::load(&store).unwrap();
        assert_eq!(kept.public_key_b64(), keys.public_key_b64());
        assert_eq!(kept.kx_public_key_b64(), keys.kx_public_key_b64());
        load_lease(&store, &root, &record.instance_id).unwrap();
        assert_eq!(store.instance().unwrap(), Some(record));
        assert!(Session::load(store, root).is_ok(), "connected");
    }

    /// Below Cloud-Protocol 2.1: key set v1, the lease signed by its `kid`.
    #[test]
    fn an_enrollment_with_key_set_v1_is_verified_and_kept() {
        use ed25519_dalek::SigningKey;
        let raw = fixture("cloud/enrollment.response.json");
        let root = SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let (_dir, _store, record) = enroll(&raw, &root).unwrap();
        assert_eq!(record.org_id, "org_acme");
        let refused = enroll(&raw, &[7; 32]).expect_err("another root");
        assert!(
            refused
                .to_string()
                .contains(super::super::lease::ROOT_MISMATCH)
        );
    }

    /// rc3-81: connect to prod, disconnect keeping local data, connect to
    /// staging: the project's prod vault is not shown there (and not
    /// synced into). The 3.12.8 account scope removes the local data of
    /// the other server at that connect, so a connect back to prod finds
    /// nothing of it.
    #[test]
    fn vault_origin_staging_after_prod_with_keep_local_is_not_shared() {
        use crate::vault::{VaultMap, VaultRecord};
        use ed25519_dalek::SigningKey;
        let raw = fixture("cloud/enrollment.response.json");
        let root = SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let enrollment: EnrollmentV1 = decode(raw.clone(), "enrollment").unwrap();
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let repo = dir.path().join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        let connect = |origin: &str| {
            complete_enrollment(
                &store,
                origin,
                &enrollment,
                &raw["keys"],
                &root,
                "studio-mac",
                at("2026-10-20T12:00:00Z"),
            )
            .unwrap();
        };
        let shared = || {
            VaultMap::load(store.data_dir())
                .for_root(&repo.to_string_lossy())
                .map(|record| record.vault_id.clone())
        };
        let prod = "https://cloud.leanctx.com";
        connect(prod);
        let mut map = VaultMap::load(store.data_dir());
        map.vaults.insert(
            "s1".into(),
            VaultRecord {
                vault_id: "vault_prod".into(),
                ns: "acme".into(),
                name: "payments".into(),
                sealed: false,
                roots: vec![repo.to_string_lossy().into_owned()],
            },
        );
        map.save(store.data_dir()).unwrap();
        assert_eq!(shared().as_deref(), Some("vault_prod"));

        store.forget(true).unwrap();
        connect("https://cloud-staging.leanctx.com");
        assert_eq!(shared(), None, "the prod vault is not shared on staging");
        assert!(VaultMap::load(store.data_dir()).vaults.is_empty());

        store.forget(true).unwrap();
        connect(prod);
        assert_eq!(shared(), None, "gone after the switch, also back on prod");
        assert!(VaultMap::load(store.data_dir()).vaults.is_empty());
    }

    /// rc3-81 review M1: connect to `first`, a sync records a vault, a
    /// disconnect keeps the local data, then connects follow in `then`
    /// order. Returns the vault shown after each of them. `writer_stamps`:
    /// the record is written the way the pull resolver writes it (claim
    /// under the lock); otherwise unstamped, as an older engine wrote it.
    fn vault_after_keep_local(
        first: &str,
        then: &[&str],
        writer_stamps: bool,
    ) -> Vec<Option<String>> {
        use crate::vault::{VaultMap, VaultRecord, update_map};
        use ed25519_dalek::SigningKey;
        let raw = fixture("cloud/enrollment.response.json");
        let root = SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let enrollment: EnrollmentV1 = decode(raw.clone(), "enrollment").unwrap();
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let repo = dir.path().join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        let connect = |origin: &str| {
            complete_enrollment(
                &store,
                origin,
                &enrollment,
                &raw["keys"],
                &root,
                "studio-mac",
                at("2026-10-20T12:00:00Z"),
            )
            .unwrap();
        };
        connect(first);
        let record = VaultRecord {
            vault_id: "vault_first".into(),
            ns: "acme".into(),
            name: "payments".into(),
            sealed: false,
            roots: vec![repo.to_string_lossy().into_owned()],
        };
        if writer_stamps {
            let (claimed, _) = update_map(store.data_dir(), |map| {
                let claimed = map.claim(first);
                map.vaults.insert("s1".into(), record);
                claimed
            })
            .unwrap();
            assert!(claimed);
        } else {
            let mut map = VaultMap::default();
            map.vaults.insert("s1".into(), record);
            map.save(store.data_dir()).unwrap();
        }
        then.iter()
            .map(|origin| {
                store.forget(true).unwrap();
                connect(origin);
                VaultMap::load(store.data_dir())
                    .for_root(&repo.to_string_lossy())
                    .map(|record| record.vault_id.clone())
            })
            .collect()
    }

    /// rc3-81 review M1: staging first, then prod. The staging vault is not
    /// active on prod (also when the file was written unstamped: the
    /// disconnect stamps it). Back on staging it is gone: the 3.12.8
    /// account scope removed it at the switch to prod.
    #[test]
    fn vault_origin_prod_after_staging_with_keep_local_is_not_shared() {
        let staging = "https://cloud-staging.leanctx.com";
        let prod = "https://cloud.leanctx.com";
        for writer_stamps in [true, false] {
            assert_eq!(
                vault_after_keep_local(staging, &[prod, staging], writer_stamps),
                vec![None, None],
                "writer_stamps = {writer_stamps}"
            );
        }
    }

    /// rc3-81 review M1: a self-hosted Cloud finds its records after a
    /// disconnect that keeps local data; the hosted Cloud never shows them,
    /// and after that switch they are gone (3.12.8 account scope).
    #[test]
    fn vault_origin_self_hosted_reconnect_after_keep_local_finds_its_records() {
        let own = "https://ctx.example.com";
        let prod = "https://cloud.leanctx.com";
        for writer_stamps in [true, false] {
            assert_eq!(
                vault_after_keep_local(own, &[own, prod, own], writer_stamps),
                vec![Some("vault_first".to_string()), None, None],
                "writer_stamps = {writer_stamps}"
            );
        }
    }

    /// Cloud-Protocol 2.1 (`routes/enrollment.for_2x.response.json`): key set
    /// v3, verified as every v3 set, and the lease signed by the key of
    /// purpose `policy` its `kid` names.
    #[test]
    fn an_enrollment_with_key_set_v3_is_verified_with_its_policy_key() {
        let raw = fixture("cloud-v2/routes/enrollment.for_2x.response.json");
        let mut root = [0u8; 32];
        hex::decode_to_slice(
            fixture("cloud-v2/vectors/signing.vectors.json")["keys"]["root_new"]["public_key_hex"]
                .as_str()
                .unwrap(),
            &mut root,
        )
        .unwrap();
        let (_dir, store, record) = enroll(&raw, &root).unwrap();
        assert_eq!(record.org_id, "org_acme");
        assert_eq!(raw["lease"]["kid"], "552e43422b3589fa");
        let kept: Value = store
            .read_config(super::super::state::KEYS_FILE)
            .unwrap()
            .unwrap();
        assert_eq!(kept, raw["keys"], "the v3 set is kept as received");
        // Another root, or a lease kid of a key that signs no leases: refused.
        assert!(enroll(&raw, &[7; 32]).is_err());
        let mut event_kid = raw;
        event_kid["lease"]["kid"] = "4fc7bebc302357bd".into();
        assert!(enroll(&event_kid, &root).is_err());
    }

    const TOKEN: &str = "het_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

    #[test]
    fn an_enrollment_token_is_checked_before_anything_is_sent() {
        let token = EnrollmentToken::parse(&format!("  {TOKEN}\r\n")).unwrap();
        assert_eq!(
            token.expose(),
            TOKEN,
            "a trailing newline from stdin is dropped"
        );
        assert!(
            EnrollmentToken::parse(&TOKEN.to_ascii_uppercase().replace("HET_", "het_")).is_ok()
        );
        let short = &TOKEN[..TOKEN.len() - 1];
        let not_hex = format!("{}g", &TOKEN[..TOKEN.len() - 1]);
        let device_code = TOKEN.replace("het_", "hdc_");
        let inner_space = format!("{} {}", &TOKEN[..20], &TOKEN[20..]);
        for bad in [
            "",
            "het_",
            short,
            &not_hex,
            &device_code,
            &inner_space,
            &format!("{TOKEN}0"),
        ] {
            let error = EnrollmentToken::parse(bad).expect_err(bad).to_string();
            assert!(error.contains("malformed"), "{error}");
            assert!(
                bad.len() < 8 || !error.contains(bad),
                "the value is never echoed: {error}"
            );
        }
        assert_eq!(format!("{token:?}"), "EnrollmentToken(het_…)");
    }

    type Recorded = std::sync::Arc<std::sync::Mutex<Vec<(String, Value)>>>;

    /// A one-route Cloud on 127.0.0.1 that answers every request with
    /// `answer` and records what it was sent.
    #[allow(clippy::needless_pass_by_value)] // callers hand over a fresh `json!`
    fn fake_cloud(answer: (u16, Value)) -> (String, Recorded) {
        let (status, body) = answer;
        let raw = raw_answer(
            status,
            "Content-Type: application/json\r\n",
            &body.to_string(),
        );
        scripted_cloud(vec![raw])
    }

    /// A raw HTTP answer: status, extra header lines and body.
    fn raw_answer(status: u16, headers: &str, body: &str) -> String {
        format!(
            "HTTP/1.1 {status} X\r\n{headers}Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        )
    }

    /// A Cloud on 127.0.0.1 that answers the n-th request with the n-th raw
    /// HTTP answer (the last one repeats) and records what it was sent.
    fn scripted_cloud(answers: Vec<String>) -> (String, Recorded) {
        use std::io::{BufRead as _, Read as _, Write as _};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let origin = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());
        let recorded: Recorded = std::sync::Arc::default();
        let seen = std::sync::Arc::clone(&recorded);
        std::thread::spawn(move || {
            for stream in listener.incoming().flatten() {
                let mut writer = stream.try_clone().unwrap();
                let mut reader = std::io::BufReader::new(stream);
                let mut line = String::new();
                if reader.read_line(&mut line).unwrap_or(0) == 0 {
                    continue;
                }
                let path = line
                    .split_whitespace()
                    .nth(1)
                    .unwrap_or_default()
                    .to_owned();
                let mut length = 0;
                loop {
                    let mut header = String::new();
                    reader.read_line(&mut header).unwrap();
                    let header = header.trim_end();
                    if header.is_empty() {
                        break;
                    }
                    if let Some((name, value)) = header.split_once(':')
                        && name.eq_ignore_ascii_case("content-length")
                    {
                        length = value.trim().parse().unwrap();
                    }
                }
                let mut body = vec![0; length];
                reader.read_exact(&mut body).unwrap();
                let body = serde_json::from_slice(&body).unwrap_or(Value::Null);
                let n = {
                    let mut seen = seen.lock().unwrap();
                    seen.push((path, body));
                    seen.len() - 1
                };
                let raw = &answers[n.min(answers.len() - 1)];
                let _ = writer.write_all(raw.as_bytes());
            }
        });
        (origin, recorded)
    }

    fn device_code(interval: u32, expires_in: u32) -> DeviceCodeV1 {
        DeviceCodeV1 {
            schema: lean_ctx_protocol::cloud::enroll::DEVICE_CODE_SCHEMA.into(),
            device_code: "hdc_test".into(),
            user_code: "WDJB-MJHT".into(),
            verification_uri: "https://cloud.example.com/device".into(),
            verification_uri_complete: "https://cloud.example.com/device?c=WDJB-MJHT".into(),
            interval,
            expires_in,
            features: Vec::new(),
            intent: None,
            plan: None,
        }
    }

    const EDGE_502: &str = "<html><body><h1>502 Bad Gateway</h1></body></html>";

    /// Review fix: an edge error page (502/503 HTML, 429 with only a
    /// `Retry-After` header) or a refused connection does not end the
    /// device flow; the poll waits (`Retry-After`, else backoff) and goes
    /// on, `slow_down` raises the interval, `access_denied` ends it.
    #[test]
    fn the_device_flow_rides_out_transient_failures_until_a_final_answer() {
        let json = "Content-Type: application/json\r\n";
        let html = "Content-Type: text/html\r\n";
        let (origin, recorded) = scripted_cloud(vec![
            raw_answer(502, html, EDGE_502),
            raw_answer(
                429,
                &format!("{html}Retry-After: 30\r\n"),
                "Too Many Requests",
            ),
            raw_answer(503, html, ""),
            raw_answer(
                400,
                json,
                r#"{"error":{"code":"authorization_pending","message":"x"}}"#,
            ),
            raw_answer(400, json, r#"{"error":{"code":"slow_down","message":"x"}}"#),
            raw_answer(
                400,
                json,
                r#"{"error":{"code":"access_denied","message":"x"}}"#,
            ),
        ]);
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let client = CloudClient::new(&origin).unwrap();
        let sleeps = std::cell::RefCell::new(Vec::new());
        let error = finish_device_flow(
            &store,
            &client,
            &device_code(2, 600),
            "studio-mac",
            &[7; 32],
            &|d| sleeps.borrow_mut().push(d.as_secs()),
        )
        .expect_err("denied");
        assert!(matches!(error, CloudError::DeviceFlow(_)), "{error:?}");
        assert!(error.to_string().contains("Not mine"), "{error}");
        assert_eq!(recorded.lock().unwrap().len(), 6);
        // interval 2; 502 → 4 (backoff); 429 → its Retry-After 30; 503 →
        // 16 (third failure in a row); pending → 2; slow_down → 2 + 5.
        assert_eq!(sleeps.into_inner(), vec![2, 4, 30, 16, 2, 7]);
        assert!(store.instance().unwrap().is_none(), "nothing enrolled");
    }

    #[test]
    fn transient_failures_end_the_device_flow_only_at_its_deadline() {
        let (origin, recorded) = scripted_cloud(vec![raw_answer(
            503,
            "Content-Type: text/html\r\n",
            EDGE_502,
        )]);
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let client = CloudClient::new(&origin).unwrap();
        let sleeps = std::cell::RefCell::new(Vec::new());
        let error = finish_device_flow(
            &store,
            &client,
            &device_code(5, 120),
            "studio-mac",
            &[7; 32],
            &|d| sleeps.borrow_mut().push(d.as_secs()),
        )
        .expect_err("expired");
        assert_eq!(
            error,
            CloudError::DeviceFlow(super::super::error::CODE_EXPIRED.into())
        );
        let sleeps = sleeps.into_inner();
        assert_eq!(sleeps.iter().sum::<u64>(), 120, "{sleeps:?}");
        assert!(sleeps.iter().all(|s| *s <= DEVICE_POLL_BACKOFF_CAP_S));
        assert_eq!(recorded.lock().unwrap().len(), sleeps.len());
    }

    #[test]
    fn device_poll_pacing_keeps_final_refusals_fatal() {
        let mut poll = DevicePoll::new(0);
        assert_eq!(poll.interval, 1, "an interval of 0 polls each second");
        for fatal in [
            CloudError::DeviceFlow(super::super::error::CODE_EXPIRED.into()),
            CloudError::Server {
                status: 400,
                code: "unknown".into(),
            },
            CloudError::Unauthorized,
            CloudError::Server {
                status: 413,
                code: "payload_too_large".into(),
            },
        ] {
            assert_eq!(poll.next_wait(fatal.clone()), Err(fatal));
        }
        let offline = CloudError::Offline("timeout".into());
        assert_eq!(poll.next_wait(offline.clone()), Ok(2));
        assert_eq!(poll.next_wait(offline.clone()), Ok(4));
        for _ in 0..10 {
            assert!(poll.next_wait(offline.clone()).unwrap() <= DEVICE_POLL_BACKOFF_CAP_S);
        }
        let limited = CloudError::RateLimited {
            retry_after: Some(90),
        };
        assert_eq!(poll.next_wait(limited), Ok(90), "Retry-After wins");
    }

    #[test]
    fn an_enrollment_token_enrolls_in_one_request_without_a_browser() {
        use ed25519_dalek::SigningKey;
        let raw = fixture("cloud/enrollment.response.json");
        let root = SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let (origin, recorded) = fake_cloud((200, raw));
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let client = CloudClient::new(&origin).unwrap();
        let token = EnrollmentToken::parse(TOKEN).unwrap();
        let (record, sharing) =
            enroll_with_token_at(&store, &client, &token, "ci-runner-7", &root, || {
                at("2026-10-20T12:00:00Z")
            })
            .unwrap();
        assert_eq!(record.org_id, "org_acme");
        assert_eq!(record.label, "ci-runner-7");
        assert_eq!(sharing, None, "no approval page asked anything");
        assert_eq!(
            store.instance().unwrap().unwrap().instance_id,
            record.instance_id
        );
        let sent = recorded.lock().unwrap().clone();
        assert_eq!(sent.len(), 1, "one request, no device code, no polling");
        let (path, body) = &sent[0];
        assert_eq!(path, "/v1/device/token");
        assert_eq!(body["schema"], DEVICE_TOKEN_REQUEST_SCHEMA);
        assert_eq!(body["device_code"], TOKEN);
        assert_eq!(body["instance_label"], "ci-runner-7");
        let keys = InstanceKeys::load(&store).unwrap();
        assert_eq!(body["instance_public_key"], keys.public_key_b64());
        assert_eq!(body["instance_kx_public_key"], keys.kx_public_key_b64());
        assert_eq!(body["instance_version"], env!("CARGO_PKG_VERSION"));
    }

    /// rc4-1: the name typed on the approval page (`instance_label` in the
    /// enrollment answer) becomes this computer's name; an unusable one is
    /// ignored and the sent name stays.
    #[test]
    fn the_name_chosen_on_the_approval_page_names_this_computer() {
        use ed25519_dalek::SigningKey;
        let root = SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        let enroll = |answer_label: Option<&str>| {
            let mut raw = fixture("cloud/enrollment.response.json");
            if let Some(label) = answer_label {
                raw["instance_label"] = json!(label);
            }
            let (origin, _) = fake_cloud((200, raw));
            let dir = tempfile::tempdir().unwrap();
            let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
            let client = CloudClient::new(&origin).unwrap();
            let token = EnrollmentToken::parse(TOKEN).unwrap();
            let (record, _) =
                enroll_with_token_at(&store, &client, &token, "host-7", &root, || {
                    at("2026-10-20T12:00:00Z")
                })
                .unwrap();
            let stored = store.instance().unwrap().unwrap().label;
            assert_eq!(stored, record.label, "the stored record names it too");
            stored
        };
        assert_eq!(enroll(Some("Ada's laptop")), "Ada's laptop");
        assert_eq!(enroll(Some(" Ada")), "host-7", "no space at the start");
        assert_eq!(
            enroll(Some("Ada\u{a0}")),
            "host-7",
            "no no-break space at the end"
        );
        assert_eq!(enroll(None), "host-7", "an older Cloud keeps today's name");
        assert_eq!(enroll(Some(" ")), "host-7");
        assert_eq!(enroll(Some("bad\u{1b}[31m")), "host-7");
        assert_eq!(enroll(Some(&"x".repeat(65))), "host-7");
    }

    #[test]
    fn a_refused_enrollment_token_says_why_without_naming_it() {
        let refusal = json!({"error": {"code": "expired_token", "message":
            "Unknown, expired, revoked or used-up enrollment token; ask a team admin for a new one."}});
        let (origin, recorded) = fake_cloud((400, refusal));
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let client = CloudClient::new(&origin).unwrap();
        let token = EnrollmentToken::parse(TOKEN).unwrap();
        let error = enroll_with_token(&store, &client, &token, "ci", &[7; 32])
            .expect_err("refused")
            .to_string();
        assert!(error.contains(ENROLLMENT_TOKEN_UNUSABLE), "{error}");
        assert!(!error.contains(&TOKEN[4..]), "{error}");
        assert!(store.instance().unwrap().is_none(), "nothing enrolled");
        assert_eq!(recorded.lock().unwrap().len(), 1);
        // The transport's code-expired wording and a bare code map the same.
        for raw in [
            CloudError::DeviceFlow(super::super::error::CODE_EXPIRED.into()),
            CloudError::Server {
                status: 400,
                code: "expired_token".into(),
            },
        ] {
            assert!(
                token_refusal(raw)
                    .to_string()
                    .contains(ENROLLMENT_TOKEN_UNUSABLE)
            );
        }
    }

    /// Cloud contact findings A: a `429 rate_limited` of the token mint
    /// holds every process sharing `throttles.json` until `Retry-After`;
    /// none of them (a forced mint, a manual sync's, neither) calls the
    /// cloud again before it ends.
    #[test]
    fn a_token_mint_429_holds_every_session_sharing_the_store() {
        let limited = json!({"error": {"code": "rate_limited",
            "message": "Too many requests.", "retry_after_s": 420}});
        let (origin, recorded) = fake_cloud((429, limited));
        let (dir, mut first) = fixture_session();
        first.instance.url.clone_from(&origin);
        // The fixture enrolls without an instance key; a mint signs with one.
        InstanceKeys::load_or_create(&first.store).expect("instance key");
        let error = first.client().map(|_| ()).expect_err("limited");
        assert!(
            matches!(
                error,
                CloudError::RateLimited {
                    retry_after: Some(420)
                }
            ),
            "{error:?}"
        );
        assert_eq!(recorded.lock().unwrap().len(), 1, "one mint sent");

        // Another process: its own Session on the same store.
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let mut second = Session::load(store, *first.root_key()).unwrap();
        second.instance.url.clone_from(&origin);
        for attempt in [
            second.client().map(|_| ()),
            second.bearer_token(true).map(|_| ()),
            first.bearer_token(true).map(|_| ()),
        ] {
            match attempt.expect_err("held") {
                CloudError::RateLimited {
                    retry_after: Some(left),
                } => assert!((400..=420).contains(&left), "{left}"),
                other => panic!("{other:?}"),
            }
        }
        assert_eq!(
            recorded.lock().unwrap().len(),
            1,
            "no HTTP call before retry_after"
        );
        let held = super::super::throttle::status_lines(&second.store, UtcDateTime::now());
        assert_eq!(held.len(), 1, "{held:?}");
        assert!(held[0].contains("token until"), "{held:?}");
    }
}

#[cfg(test)]
mod task_key_tests {
    use super::*;
    use crate::consent::task_key::tests::{instance_public_key, offer, test_key};

    /// rc4-67: the offer binds to the `instance_public_key` the request
    /// sends, travels as `task_propose_key`, and only its public part stays.
    #[test]
    fn a_task_key_offer_binds_to_the_instance_public_key_the_request_sends() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let plain = device_token_request(&keys, "dc", "label", AppKeyOffers::default()).unwrap();
        assert!(plain.get("task_propose_key").is_none());
        // Bound to another instance key: refused before anything is sent.
        let foreign = offer(&test_key(), &instance_public_key());
        assert!(check_task_key(&keys, Some(&foreign)).is_err());
        let good = offer(&test_key(), &keys.public_key_b64());
        check_task_key(&keys, Some(&good)).unwrap();
        let offers = AppKeyOffers {
            task: Some(&good),
            ..AppKeyOffers::default()
        };
        let request = device_token_request(&keys, "dc", "label", offers).unwrap();
        assert_eq!(request["task_propose_key"], good.request_member());
        assert!(request.get("chat_key").is_none());
        keep_task_key(&store, Some(&good), Pairing::New).unwrap();
        assert_eq!(task_key::bound_task_key(&store), Some(good.public()));
        keep_task_key(&store, None, Pairing::New).unwrap();
        assert_eq!(task_key::bound_task_key(&store), None);
    }

    /// The Cloud's pairing refusals of a task key become plain words; the
    /// same codes without an offer stay as they are.
    #[test]
    fn task_key_pairing_refusals_are_plain_words() {
        let refusal = |status: u16, body: Value| CloudError::from_response(status, &body);
        let invalid = refusal(
            400,
            json!({"error": {"code": "invalid_field", "field": "task_propose_key",
                "reason": "token_enrollment", "message": "x"}}),
        );
        let bound = refusal(
            409,
            json!({"error": {"code": "invalid_state", "reason": "task_propose_key_bound",
                "message": "x"}}),
        );
        let cleared = refusal(
            409,
            json!({"error": {"code": "invalid_state", "reason": "task_propose_key_cleared",
                "message": "x"}}),
        );
        for (error, words, code) in [
            (
                invalid.clone(),
                TASK_KEY_REFUSED,
                crate::error::TASK_KEY_INVALID,
            ),
            (bound, TASK_KEY_ALREADY_BOUND, crate::error::TASK_KEY_BOUND),
            (
                cleared,
                TASK_KEY_WAS_REMOVED,
                crate::error::TASK_KEY_CLEARED,
            ),
        ] {
            let refused = task_key_refusal(error, true);
            // The `connect --json` error event keeps the reason as its code.
            assert_eq!(refused.event_code(), code);
            match refused {
                CloudError::DeviceFlow(text) => assert_eq!(text, words),
                other => panic!("{other:?}"),
            }
        }
        assert!(matches!(
            task_key_refusal(invalid, false),
            CloudError::Server { status: 400, .. }
        ));
    }

    /// rc4-32 + rc4-67: LeanCTX Code may offer its chat key and its task key
    /// in one pairing; each is checked, both travel in the one request, both
    /// are kept, and each refusal keeps its own words.
    #[test]
    fn a_pairing_carries_a_chat_key_and_a_task_key_together() {
        use crate::consent::chat_key::tests as chat;
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let chat_offer = chat::offer_for(&chat::test_key(), &keys.public_key_b64());
        let task_offer = offer(&test_key(), &keys.public_key_b64());
        let both = AppKeyOffers {
            chat: Some(&chat_offer),
            task: Some(&task_offer),
            optin: None,
        };
        both.check(&keys).unwrap();
        // One foreign offer stops the pairing, whichever it is.
        let foreign_task = offer(&test_key(), &instance_public_key());
        let bad_task = AppKeyOffers {
            task: Some(&foreign_task),
            ..both
        };
        assert!(bad_task.check(&keys).is_err());
        let foreign_chat = chat::offer(&chat::test_key());
        let bad_chat = AppKeyOffers {
            chat: Some(&foreign_chat),
            ..both
        };
        assert!(bad_chat.check(&keys).is_err());
        let request = device_token_request(&keys, "dc", "label", both).unwrap();
        assert_eq!(request["chat_key"], chat_offer.request_member());
        assert_eq!(
            request[task_key::REQUEST_MEMBER],
            task_offer.request_member()
        );
        both.keep(&store, Pairing::New).unwrap();
        assert_eq!(
            crate::consent::chat_key::bound_chat_key(&store),
            Some(chat_offer.public())
        );
        assert_eq!(task_key::bound_task_key(&store), Some(task_offer.public()));
        // Each kind's refusal maps to its own words.
        let task_refused = CloudError::from_response(
            409,
            &json!({"error": {"code": "invalid_state", "reason": "task_propose_key_bound",
                "message": "x"}}),
        );
        assert!(matches!(
            both.refusal(task_refused, CHAT_KEY_REFUSED),
            CloudError::DeviceFlow(text) if text == TASK_KEY_ALREADY_BOUND
        ));
        let chat_refused = CloudError::Server {
            status: 400,
            code: "invalid_field".into(),
        };
        assert!(matches!(
            both.refusal(chat_refused, CHAT_KEY_REFUSED),
            CloudError::DeviceFlow(text) if text == CHAT_KEY_REFUSED
        ));
        // bd 40011 S1: a Cloud that needs the keys announced (f3129) is
        // named as "update lean-ctx", even when it names the task key field.
        let not_announced = CloudError::from_response(
            400,
            &json!({"error": {"code": "invalid_field", "field": "task_propose_key",
                "reason": "app_key_not_announced", "message": "x"}}),
        );
        assert!(matches!(
            both.refusal(not_announced, CHAT_KEY_REFUSED),
            CloudError::DeviceFlow(text) if text == APP_KEY_PAIRING_FAILED
        ));
        // bd 40011 M2: a rebind of the same instance offering only a new
        // task key keeps the bound chat key (the Cloud keeps it bound).
        let task_only = AppKeyOffers {
            chat: None,
            task: Some(&task_offer),
            optin: None,
        };
        task_only.keep(&store, Pairing::Rebind).unwrap();
        assert_eq!(
            crate::consent::chat_key::bound_chat_key(&store),
            Some(chat_offer.public())
        );
        assert_eq!(task_key::bound_task_key(&store), Some(task_offer.public()));
        // A rebind without offers keeps both.
        AppKeyOffers::default()
            .keep(&store, Pairing::Rebind)
            .unwrap();
        assert_eq!(
            crate::consent::chat_key::bound_chat_key(&store),
            Some(chat_offer.public())
        );
        assert_eq!(task_key::bound_task_key(&store), Some(task_offer.public()));
        // A new enrollment without offers forgets both.
        AppKeyOffers::default().keep(&store, Pairing::New).unwrap();
        assert_eq!(crate::consent::chat_key::bound_chat_key(&store), None);
        assert_eq!(task_key::bound_task_key(&store), None);
    }
}

#[cfg(test)]
mod optin_key_tests {
    use super::*;
    use crate::consent::optin_key::test_key::{INSTANCE_PUBLIC_KEY, key, offer};

    fn store() -> (tempfile::TempDir, CloudStore, InstanceKeys) {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        (dir, store, keys)
    }

    /// rc4-16: the opt-in key binds to the `instance_public_key` the request
    /// sends (a foreign or broken bind stops the pairing before anything is
    /// sent) and travels as `optin_key` beside the other offers.
    #[test]
    fn an_optin_key_offer_is_checked_then_sent_as_optin_key() {
        let (_dir, _store, keys) = store();
        let plain = device_token_request(&keys, "dc", "label", AppKeyOffers::default()).unwrap();
        assert!(plain.get("optin_key").is_none());
        let foreign = offer(&key(), INSTANCE_PUBLIC_KEY);
        let refused = AppKeyOffers {
            optin: Some(&foreign),
            ..AppKeyOffers::default()
        };
        assert!(matches!(refused.check(&keys), Err(CloudError::Config(_))));
        let mut forged = offer(&key(), &keys.public_key_b64());
        forged.bind_signature = foreign.bind_signature.clone();
        assert!(check_optin_key(&keys, Some(&forged)).is_err());
        let good = offer(&key(), &keys.public_key_b64());
        let offers = AppKeyOffers {
            optin: Some(&good),
            ..AppKeyOffers::default()
        };
        offers.check(&keys).unwrap();
        let request = device_token_request(&keys, "dc", "label", offers).unwrap();
        assert_eq!(request["optin_key"], good.request_member());
        assert_eq!(
            serde_json::from_value::<OptinKeyOffer>(request["optin_key"].clone()).unwrap(),
            good
        );
        assert!(request.get("chat_key").is_none());
        assert!(request.get("task_propose_key").is_none());
    }

    /// rc4-16: an accepted pairing keeps the offered key accepted (pending
    /// before); a new enrollment without it puts it back to pending, a
    /// rebind without it keeps it.
    #[test]
    fn a_paired_optin_key_is_kept_accepted_until_a_new_enrollment() {
        let (_dir, store, keys) = store();
        let good = offer(&key(), &keys.public_key_b64());
        // Handed over by `cloud.pickup.key` first: pending.
        optin_key::store_pending(&store, &good).unwrap();
        assert!(!optin_key::pending_optin_key(&store).unwrap().accepted);
        let offers = AppKeyOffers {
            optin: Some(&good),
            ..AppKeyOffers::default()
        };
        offers.keep(&store, Pairing::New).unwrap();
        let kept = optin_key::pending_optin_key(&store).unwrap();
        assert!(kept.accepted);
        assert_eq!(kept.key_id, good.key_id);
        AppKeyOffers::default()
            .keep(&store, Pairing::Rebind)
            .unwrap();
        assert!(optin_key::pending_optin_key(&store).unwrap().accepted);
        AppKeyOffers::default().keep(&store, Pairing::New).unwrap();
        let pending = optin_key::pending_optin_key(&store).unwrap();
        assert!(
            !pending.accepted,
            "a key of an earlier enrollment is not usable"
        );
        assert!(optin_key::bound_optin_key(&store).is_none());
    }

    /// The Cloud's pairing refusals of an opt-in key become the §46.4 texts
    /// with their own codes; `optin_key_cleared` puts the kept key back to
    /// pending.
    #[test]
    fn optin_key_pairing_refusals_keep_their_reasons() {
        let refusal = |status: u16, body: Value| CloudError::from_response(status, &body);
        let invalid = refusal(
            400,
            json!({"error": {"code": "invalid_field", "field": "optin_key.bind_signature",
                "message": "x"}}),
        );
        let bound = refusal(
            409,
            json!({"error": {"code": "conflict", "reason": "optin_key_bound", "message": "x"}}),
        );
        let cleared = refusal(
            409,
            json!({"error": {"code": "optin_key_cleared", "message": "x"}}),
        );
        let foreign = offer(&key(), INSTANCE_PUBLIC_KEY);
        let offered = AppKeyOffers {
            optin: Some(&foreign),
            ..AppKeyOffers::default()
        };
        for (error, words, code) in [
            (invalid.clone(), OPTIN_KEY_REFUSED, "optin_key_invalid"),
            (bound, OPTIN_KEY_ALREADY_BOUND, "optin_key_bound"),
            (cleared.clone(), OPTIN_KEY_WAS_REMOVED, "optin_key_cleared"),
        ] {
            let refused = offered.refusal(error, CHAT_KEY_REFUSED);
            assert_eq!(refused.event_code(), code);
            match refused {
                CloudError::DeviceFlow(text) => assert_eq!(text, words),
                other => panic!("{other:?}"),
            }
        }
        assert!(matches!(
            AppKeyOffers::default().refusal(invalid, CHAT_KEY_REFUSED),
            CloudError::Server { status: 400, .. }
        ));
        // A cleared key that is the one kept here is pending again.
        let (_dir, store, keys) = store();
        let good = offer(&key(), &keys.public_key_b64());
        keep_optin_key(&store, Some(&good), Pairing::New).unwrap();
        let mut other = offer(&key(), &keys.public_key_b64());
        other.key_id = "0000000000000000".into();
        forget_cleared_optin_key(&store, Some(&other), &cleared);
        assert!(
            optin_key::pending_optin_key(&store).unwrap().accepted,
            "another key's removal keeps this one"
        );
        forget_cleared_optin_key(&store, Some(&good), &CloudError::Unauthorized);
        assert!(optin_key::pending_optin_key(&store).unwrap().accepted);
        forget_cleared_optin_key(&store, Some(&good), &cleared);
        assert!(!optin_key::pending_optin_key(&store).unwrap().accepted);
    }
}

#[cfg(test)]
mod chat_key_tests {
    use super::*;
    use crate::consent::chat_key::tests::{offer, offer_for, test_key};

    #[test]
    fn a_chat_key_offer_binds_to_the_instance_public_key_the_request_sends() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let plain = device_token_request(&keys, "dc", "label", AppKeyOffers::default()).unwrap();
        assert!(plain.get("chat_key").is_none());
        // Bound to another instance key: refused before anything is sent.
        assert!(check_chat_key(&keys, Some(&offer(&test_key()))).is_err());
        let good = offer_for(&test_key(), &keys.public_key_b64());
        check_chat_key(&keys, Some(&good)).unwrap();
        let offers = AppKeyOffers {
            chat: Some(&good),
            ..AppKeyOffers::default()
        };
        let request = device_token_request(&keys, "dc", "label", offers).unwrap();
        assert_eq!(request["chat_key"], good.request_member());
        assert!(request.get("task_propose_key").is_none());
        keep_chat_key(&store, Some(&good), Pairing::New).unwrap();
        assert_eq!(
            crate::consent::chat_key::bound_chat_key(&store),
            Some(good.public())
        );
    }

    fn fixture_answer() -> (Value, [u8; 32]) {
        let path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../lean-ctx-protocol/tests/fixtures/cloud/enrollment.response.json"
        );
        let raw: Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
        let root = ed25519_dalek::SigningKey::from_bytes(&[0x52; 32])
            .verifying_key()
            .to_bytes();
        (raw, root)
    }

    fn fixture_now() -> UtcDateTime {
        lean_ctx_time::OffsetDateTime::parse_from_rfc3339("2026-10-20T12:00:00Z")
            .unwrap()
            .to_utc()
    }

    fn code() -> DeviceCodeV1 {
        DeviceCodeV1 {
            schema: lean_ctx_protocol::cloud::enroll::DEVICE_CODE_SCHEMA.into(),
            device_code: "hdc_repair".into(),
            user_code: "WDJB-MJHT".into(),
            verification_uri: "https://cloud.example.com/device".into(),
            verification_uri_complete: "https://cloud.example.com/device?c=WDJB-MJHT".into(),
            interval: 1,
            expires_in: 60,
            features: vec![lean_ctx_protocol::cloud::app_keys::FEATURE_DEVICE_APP_KEYS.into()],
            intent: None,
            plan: None,
        }
    }

    /// One-answer Cloud on 127.0.0.1 recording each request body.
    fn one_answer_cloud(
        status: u16,
        body: &Value,
    ) -> (String, std::sync::Arc<std::sync::Mutex<Vec<Value>>>) {
        use std::io::{BufRead as _, Read as _, Write as _};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let origin = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());
        let seen: std::sync::Arc<std::sync::Mutex<Vec<Value>>> = std::sync::Arc::default();
        let record = std::sync::Arc::clone(&seen);
        let text = body.to_string();
        let raw = format!(
            "HTTP/1.1 {status} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{text}",
            text.len()
        );
        std::thread::spawn(move || {
            for stream in listener.incoming().flatten() {
                let mut writer = stream.try_clone().unwrap();
                let mut reader = std::io::BufReader::new(stream);
                let mut line = String::new();
                if reader.read_line(&mut line).unwrap_or(0) == 0 {
                    continue;
                }
                let mut length = 0;
                loop {
                    let mut header = String::new();
                    reader.read_line(&mut header).unwrap();
                    let header = header.trim_end();
                    if header.is_empty() {
                        break;
                    }
                    if let Some((name, value)) = header.split_once(':')
                        && name.eq_ignore_ascii_case("content-length")
                    {
                        length = value.trim().parse().unwrap();
                    }
                }
                let mut body = vec![0; length];
                reader.read_exact(&mut body).unwrap();
                record
                    .lock()
                    .unwrap()
                    .push(serde_json::from_slice(&body).unwrap_or(Value::Null));
                let _ = writer.write_all(raw.as_bytes());
            }
        });
        (origin, seen)
    }

    /// A connected computer, its instance keys made.
    fn connected(origin: &str) -> (tempfile::TempDir, CloudStore, InstanceKeys) {
        let (dir, session) = fixture_session_at(origin);
        let store = session.store;
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        (dir, store, keys)
    }

    /// rc4-32 re-pair: the device flow of a connected computer sends its
    /// existing instance key plus the chat key, never `replaces`; the
    /// enrollment record stays, the chat key is bound.
    #[test]
    fn a_chat_key_repair_keeps_the_instance_and_sends_no_replaces() {
        let (answer, root) = fixture_answer();
        let (origin, seen) = one_answer_cloud(200, &answer);
        let (_dir, store, keys) = connected(&origin);
        let before = store.instance().unwrap().unwrap();
        let client = CloudClient::new(&origin).unwrap();
        let good = offer_for(&test_key(), &keys.public_key_b64());
        let outcome =
            repair_chat_key_at(&store, &client, &code(), &root, &|_| {}, &good, fixture_now)
                .unwrap();
        assert_eq!(outcome, ChatKeyRepair::Bound);
        assert_eq!(outcome.reason(), "bound");
        let sent = seen.lock().unwrap().clone();
        assert_eq!(sent.len(), 1);
        assert_eq!(sent[0]["device_code"], "hdc_repair");
        assert_eq!(sent[0]["instance_public_key"], keys.public_key_b64());
        assert_eq!(sent[0]["instance_label"], before.label.as_str());
        assert_eq!(sent[0]["chat_key"], good.request_member());
        assert!(sent[0].get("replaces").is_none(), "{}", sent[0]);
        let after = store.instance().unwrap().unwrap();
        assert_eq!(after.instance_id, before.instance_id);
        assert_eq!(after.enrolled_at, before.enrolled_at, "the record is kept");
        assert_eq!(
            InstanceKeys::load(&store).unwrap().public_key_b64(),
            keys.public_key_b64()
        );
        assert_eq!(
            crate::consent::chat_key::bound_chat_key(&store),
            Some(good.public())
        );
        // The same key again: unchanged.
        let again =
            repair_chat_key_at(&store, &client, &code(), &root, &|_| {}, &good, fixture_now)
                .unwrap();
        assert_eq!(again.reason(), "unchanged");
    }

    #[test]
    fn each_chat_key_repair_refusal_maps_to_its_reason_and_text() {
        let (_, root) = fixture_answer();
        for (status, error, reason, text) in [
            (
                409,
                json!({"code": "invalid_state", "reason": "chat_key_bound", "message": "x"}),
                Some("chat_key_bound"),
                CHAT_KEY_BOUND,
            ),
            (
                409,
                json!({"code": "invalid_state", "reason": "chat_key_cleared", "message": "x"}),
                Some("chat_key_cleared"),
                CHAT_KEY_CLEARED,
            ),
            (
                400,
                json!({"code": "invalid_field", "field": "chat_key", "message": "x"}),
                None,
                CHAT_KEY_REFUSED,
            ),
            (
                400,
                json!({"code": "token_enrollment", "message": "x"}),
                None,
                CHAT_KEY_TOKEN_ENROLLMENT,
            ),
            // bd 40051: a Cloud that needs the keys announced (f3129).
            (
                400,
                json!({"code": "invalid_field", "field": "chat_key",
                    "reason": "app_key_not_announced", "message": "x"}),
                None,
                APP_KEY_PAIRING_FAILED,
            ),
        ] {
            let (origin, seen) = one_answer_cloud(status, &json!({ "error": error }));
            let (_dir, store, keys) = connected(&origin);
            let client = CloudClient::new(&origin).unwrap();
            let good = offer_for(&test_key(), &keys.public_key_b64());
            let refused =
                repair_chat_key_at(&store, &client, &code(), &root, &|_| {}, &good, fixture_now)
                    .expect_err("refused");
            assert_eq!(
                chat_key_repair_refusal(&refused),
                Some((reason, text)),
                "{status} {error}: {refused:?}"
            );
            if text == APP_KEY_PAIRING_FAILED {
                // The app's `connect --json` error event says which.
                assert_eq!(refused.event_code(), "app_key_not_announced");
            }
            assert_eq!(seen.lock().unwrap().len(), 1, "a final answer: no polling");
            assert!(store.instance().unwrap().is_some(), "still connected");
            assert_eq!(crate::consent::chat_key::bound_chat_key(&store), None);
        }
        assert_eq!(chat_key_repair_refusal(&CloudError::Unauthorized), None);
    }

    /// bd 40051 S2: "the same instance" is the same id, team and Cloud; any
    /// other record, none, or an unreadable one is a new enrollment.
    #[test]
    fn a_rebind_is_the_same_instance_id_team_and_cloud() {
        let (_dir, session) = fixture_session_at("https://cloud.example.com");
        let store = session.store;
        let after = store.instance().unwrap().unwrap();
        let held = HeldInstance::read(&store).expect("held");
        assert_eq!(Pairing::of(Some(&held), &after), Pairing::Rebind);
        assert_eq!(Pairing::of(None, &after), Pairing::New);
        for other in [
            HeldInstance {
                instance_id: "inst_other".into(),
                ..held.clone()
            },
            HeldInstance {
                org_id: "org_other".into(),
                ..held.clone()
            },
            HeldInstance {
                url: "https://other.example.com".into(),
                ..held.clone()
            },
        ] {
            assert_eq!(Pairing::of(Some(&other), &after), Pairing::New, "{other:?}");
        }
        // Unreadable: none held, so a new enrollment (fail closed).
        std::fs::write(store.config_dir().join(INSTANCE_FILE), b"{ not json").unwrap();
        assert_eq!(HeldInstance::read(&store), None);
    }

    /// bd 40051 S1: the device flow reads the held instance before the
    /// enrollment is accepted (which overwrites it). A rebind of the same
    /// instance offering only a task key keeps the bound chat key; a pairing
    /// that enrolls another instance forgets it.
    #[test]
    fn a_task_only_pairing_keeps_the_chat_key_of_the_same_instance_only() {
        use crate::consent::task_key::tests as task;
        let (answer, root) = fixture_answer();
        let (origin, seen) = one_answer_cloud(200, &answer);
        for same in [true, false] {
            let (_dir, store, keys) = connected(&origin);
            let chat = offer_for(&test_key(), &keys.public_key_b64());
            keep_chat_key(&store, Some(&chat), Pairing::New).unwrap();
            if !same {
                let mut record = store.instance().unwrap().unwrap();
                record.instance_id = "inst_held_before".into();
                store.write_config(INSTANCE_FILE, &record).unwrap();
            }
            let task_offer = task::offer(&task::test_key(), &keys.public_key_b64());
            let offers = AppKeyOffers {
                chat: None,
                task: Some(&task_offer),
                optin: None,
            };
            let client = CloudClient::new(&origin).unwrap();
            let (record, _) = poll_device_flow(
                &store,
                &client,
                &code(),
                "studio-mac",
                &root,
                &|_| {},
                offers,
                fixture_now,
            )
            .unwrap();
            assert_eq!(record.instance_id, "inst_01jb8z3k4m5n6p7q8r9s0t1v2w");
            assert_eq!(task_key::bound_task_key(&store), Some(task_offer.public()));
            let kept = crate::consent::chat_key::bound_chat_key(&store);
            if same {
                assert_eq!(kept, Some(chat.public()), "a rebind keeps the chat key");
            } else {
                assert_eq!(kept, None, "a new instance forgets the chat key");
            }
        }
        assert_eq!(seen.lock().unwrap().len(), 2);
    }

    /// rc4-16 (bd 40254 S2): through the device flow against a Cloud, the
    /// opt-in key travels as `optin_key` in `POST /v1/device/token`; a 2xx
    /// keeps it accepted, a `409 optin_key_cleared` for that key puts it back
    /// to pending with the §46.4 text, and a `400 token_enrollment` gives the
    /// §46.2 text with its own code.
    #[test]
    fn an_optin_key_pairing_flow_sends_keeps_and_clears_the_optin_key() {
        use crate::consent::optin_key::test_key as optin;
        let (answer, root) = fixture_answer();
        let offered = |keys: &InstanceKeys| optin::offer(&optin::key(), &keys.public_key_b64());

        // 2xx: sent as `optin_key`, kept accepted.
        let (origin, seen) = one_answer_cloud(200, &answer);
        let (_dir, store, keys) = connected(&origin);
        let good = offered(&keys);
        optin_key::store_pending(&store, &good).unwrap();
        let offers = AppKeyOffers {
            optin: Some(&good),
            ..AppKeyOffers::default()
        };
        offers.verify(&store).unwrap();
        let client = CloudClient::new(&origin).unwrap();
        finish_device_flow_at(
            &store,
            &client,
            &code(),
            "studio-mac",
            &root,
            &|_| {},
            offers,
            fixture_now,
        )
        .unwrap();
        let sent = seen.lock().unwrap().clone();
        assert_eq!(sent.len(), 1);
        assert_eq!(sent[0]["instance_public_key"], keys.public_key_b64());
        assert_eq!(sent[0]["optin_key"], good.request_member());
        let kept = optin_key::pending_optin_key(&store).unwrap();
        assert!(kept.accepted, "a 2xx keeps the key accepted");
        assert_eq!(kept.key_id, good.key_id);

        // 409 optin_key_cleared for the same key: pending again, §46.4 text.
        let cleared = json!({"error": {"code": "optin_key_cleared", "message": "x"}});
        let (origin, seen) = one_answer_cloud(409, &cleared);
        let (_dir, store, keys) = connected(&origin);
        let good = offered(&keys);
        keep_optin_key(&store, Some(&good), Pairing::New).unwrap();
        assert!(optin_key::pending_optin_key(&store).unwrap().accepted);
        let offers = AppKeyOffers {
            optin: Some(&good),
            ..AppKeyOffers::default()
        };
        let client = CloudClient::new(&origin).unwrap();
        let refused = finish_device_flow_at(
            &store,
            &client,
            &code(),
            "studio-mac",
            &root,
            &|_| {},
            offers,
            fixture_now,
        )
        .expect_err("cleared");
        assert_eq!(seen.lock().unwrap()[0]["optin_key"], good.request_member());
        assert_eq!(refused.event_code(), "optin_key_cleared");
        assert!(
            matches!(&refused, CloudError::DeviceFlow(text) if text == OPTIN_KEY_WAS_REMOVED),
            "{refused:?}"
        );
        let pending = optin_key::pending_optin_key(&store).unwrap();
        assert!(!pending.accepted, "the cleared key is pending again");
        assert_eq!(pending.key_id, good.key_id);

        // A computer enrolled with a token: §46.2 text, own code.
        let token = json!({"error": {"code": "token_enrollment", "message": "x"}});
        let (origin, _seen) = one_answer_cloud(400, &token);
        let (_dir, store, keys) = connected(&origin);
        let good = offered(&keys);
        let client = CloudClient::new(&origin).unwrap();
        let refused = finish_device_flow_at(
            &store,
            &client,
            &code(),
            "studio-mac",
            &root,
            &|_| {},
            AppKeyOffers {
                optin: Some(&good),
                ..AppKeyOffers::default()
            },
            fixture_now,
        )
        .expect_err("token enrollment");
        assert_eq!(refused.event_code(), "token_enrollment");
        assert!(
            matches!(&refused, CloudError::DeviceFlow(text) if text == OPTIN_KEY_TOKEN_ENROLLMENT),
            "{refused:?}"
        );
    }

    /// bd 40254 S1: the offers are verified against this computer's instance
    /// key before the connect contacts the Cloud; nothing offered makes no
    /// key.
    #[test]
    fn app_key_offers_verify_before_the_cloud_is_contacted_for_optin_key() {
        use crate::consent::optin_key::test_key as optin;
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        AppKeyOffers::default().verify(&store).unwrap();
        assert!(
            InstanceKeys::load(&store).is_err(),
            "nothing offered: no key made"
        );
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let foreign = optin::offer(&optin::key(), optin::INSTANCE_PUBLIC_KEY);
        let chat_foreign = offer(&test_key());
        for refused in [
            AppKeyOffers {
                optin: Some(&foreign),
                ..AppKeyOffers::default()
            },
            AppKeyOffers {
                chat: Some(&chat_foreign),
                ..AppKeyOffers::default()
            },
        ] {
            assert!(matches!(refused.verify(&store), Err(CloudError::Config(_))));
        }
        let good = optin::offer(&optin::key(), &keys.public_key_b64());
        AppKeyOffers {
            optin: Some(&good),
            ..AppKeyOffers::default()
        }
        .verify(&store)
        .unwrap();
    }

    /// The device-code answer of a Cloud with or without `device_app_keys`.
    fn code_answer(app_keys: bool) -> Value {
        let mut answer = code();
        if !app_keys {
            answer.features.clear();
        }
        serde_json::to_value(answer).unwrap()
    }

    /// f3129 S2: every offer is announced at `POST /v1/device/code` as
    /// `app_keys`, sorted chat < optin < task_propose; without offers the
    /// request carries no `app_keys` member and is the plain pairing's.
    #[test]
    fn f3129_app_keys_are_announced_sorted_at_the_device_code() {
        use crate::consent::optin_key::test_key as optin;
        use crate::consent::task_key::tests as task;
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let pk = InstanceKeys::load_or_create(&store)
            .unwrap()
            .public_key_b64();
        let chat = offer_for(&test_key(), &pk);
        let task_offer = task::offer(&task::test_key(), &pk);
        let optin_offer = optin::offer(&optin::key(), &pk);
        let offers = AppKeyOffers {
            chat: Some(&chat),
            task: Some(&task_offer),
            optin: Some(&optin_offer),
        };
        let (origin, seen) = one_answer_cloud(200, &code_answer(true));
        let client = CloudClient::new(&origin).unwrap();
        start_device_flow_with(&client, Some("studio-mac"), None, offers).unwrap();
        let announced = offers.announcements();
        assert_eq!(
            announced.iter().map(|a| a.kind).collect::<Vec<_>>(),
            [AppKeyKind::Chat, AppKeyKind::Optin, AppKeyKind::TaskPropose]
        );
        for (a, (public_key, key_id)) in announced.iter().zip([
            (&chat.public_key, &chat.key_id),
            (&optin_offer.public_key, &optin_offer.key_id),
            (&task_offer.public_key, &task_offer.key_id),
        ]) {
            assert_eq!(a.alg, "ed25519");
            assert_eq!((&a.public_key, &a.key_id), (public_key, key_id));
        }
        let sent = seen.lock().unwrap().clone();
        assert_eq!(sent.len(), 1);
        assert_eq!(
            sent[0]["app_keys"],
            serde_json::to_value(&announced).unwrap()
        );
        assert_eq!(sent[0]["app_keys"][0]["kind"], "chat");
        assert_eq!(sent[0]["app_keys"][2]["kind"], "task_propose");

        // No offers: no `app_keys`, byte for byte the plain pairing.
        let (origin, seen) = one_answer_cloud(200, &code_answer(false));
        let client = CloudClient::new(&origin).unwrap();
        start_device_flow_with(&client, Some("studio-mac"), None, AppKeyOffers::default()).unwrap();
        start_device_flow_for(&client, Some("studio-mac"), None).unwrap();
        let sent = seen.lock().unwrap().clone();
        assert_eq!(sent.len(), 2);
        assert!(sent[0].get("app_keys").is_none(), "{}", sent[0]);
        assert_eq!(sent[0].to_string(), sent[1].to_string());
        let plain = DeviceCodeRequestV1::new(env!("CARGO_PKG_VERSION"), engine_os())
            .with_label(Some("studio-mac".into()));
        assert_eq!(sent[0], serde_json::to_value(plain).unwrap());
    }

    /// f3129: a Cloud without `device_app_keys` (or one refusing `app_keys`
    /// as an unknown member) gets no offer: a clear `config` error before
    /// any polling; the pairing without offers is unaffected.
    #[test]
    fn f3129_app_keys_fail_closed_without_the_cloud_feature() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let chat = offer_for(&test_key(), &keys.public_key_b64());
        let offers = AppKeyOffers {
            chat: Some(&chat),
            ..AppKeyOffers::default()
        };
        let (origin, seen) = one_answer_cloud(200, &code_answer(false));
        let client = CloudClient::new(&origin).unwrap();
        let refused = start_device_flow_with(&client, None, None, offers).expect_err("no feature");
        assert!(
            matches!(&refused, CloudError::Config(text) if text == APP_KEYS_UNSUPPORTED),
            "{refused:?}"
        );
        assert_eq!(seen.lock().unwrap().len(), 1, "no polling");
        // Polling with offers on such a code is refused before it is sent.
        let (_, root) = fixture_answer();
        let mut old = code();
        old.features.clear();
        let refused = poll_device_flow(
            &store,
            &client,
            &old,
            "m",
            &root,
            &|_| {},
            offers,
            fixture_now,
        )
        .expect_err("no feature");
        assert!(matches!(&refused, CloudError::Config(_)), "{refused:?}");
        assert_eq!(seen.lock().unwrap().len(), 1, "nothing sent");
        start_device_flow_with(&client, None, None, AppKeyOffers::default()).unwrap();
        // An older Cloud's 400 on the unknown member reads the same.
        let old_cloud = json!({"error": {"code": "invalid_request", "message": "x"}});
        let (origin, _) = one_answer_cloud(400, &old_cloud);
        let client = CloudClient::new(&origin).unwrap();
        let refused = start_device_flow_with(&client, None, None, offers).expect_err("old");
        assert!(
            matches!(&refused, CloudError::Config(text) if text == APP_KEYS_UNSUPPORTED),
            "{refused:?}"
        );
    }

    /// f3129 S3: the device-token request carries `app_keys_bind_signature`,
    /// the instance key's signature over the bind statement of the device
    /// code, the sent instance key and the announced set; none without
    /// offers.
    #[test]
    fn f3129_app_keys_bind_signature_verifies_with_the_instance_key() {
        use crate::consent::task_key::tests as task;
        use base64::Engine as _;
        use ed25519_dalek::Verifier as _;
        let (answer, root) = fixture_answer();
        let (origin, seen) = one_answer_cloud(200, &answer);
        let (_dir, store, keys) = connected(&origin);
        let pk = keys.public_key_b64();
        let chat = offer_for(&test_key(), &pk);
        let task_offer = task::offer(&task::test_key(), &pk);
        let offers = AppKeyOffers {
            chat: Some(&chat),
            task: Some(&task_offer),
            optin: None,
        };
        let client = CloudClient::new(&origin).unwrap();
        poll_device_flow(
            &store,
            &client,
            &code(),
            "m",
            &root,
            &|_| {},
            offers,
            fixture_now,
        )
        .unwrap();
        let sent = seen.lock().unwrap().clone();
        assert_eq!(sent.len(), 1);
        assert_eq!(sent[0]["instance_public_key"], pk);
        let signature = sent[0]["app_keys_bind_signature"].as_str().expect("signed");
        let raw = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .decode(signature)
            .unwrap();
        let signature = ed25519_dalek::Signature::from_slice(&raw).unwrap();
        let verifying = keys.signing_key().verifying_key();
        let input = app_key_bind_signing_input("hdc_repair", &pk, &offers.announcements()).unwrap();
        verifying
            .verify(&input, &signature)
            .expect("the instance key signed the statement");
        // Another device code does not verify.
        let other = app_key_bind_signing_input("hdc_other", &pk, &offers.announcements()).unwrap();
        assert!(verifying.verify(&other, &signature).is_err());
        // No offers, no signature.
        let plain = device_token_request(&keys, "dc", "m", AppKeyOffers::default()).unwrap();
        assert!(plain.get("app_keys_bind_signature").is_none());
    }

    /// f3129 §11: `400 invalid_field` with reason `app_key_not_announced` or
    /// `app_key_bind_invalid` reads "Pairing failed" with the app's code.
    #[test]
    fn f3129_app_keys_refusals_read_pairing_failed() {
        let (_, root) = fixture_answer();
        for reason in ["app_key_not_announced", "app_key_bind_invalid"] {
            let error = json!({"error": {"code": "invalid_field",
                "field": "app_keys_bind_signature", "reason": reason, "message": "x"}});
            let (origin, seen) = one_answer_cloud(400, &error);
            let (_dir, store, keys) = connected(&origin);
            let chat = offer_for(&test_key(), &keys.public_key_b64());
            let client = CloudClient::new(&origin).unwrap();
            let refused = finish_device_flow_at(
                &store,
                &client,
                &code(),
                "m",
                &root,
                &|_| {},
                AppKeyOffers {
                    chat: Some(&chat),
                    ..AppKeyOffers::default()
                },
                fixture_now,
            )
            .expect_err("refused");
            assert!(
                matches!(&refused, CloudError::DeviceFlow(text) if text == PAIRING_FAILED_TEXT),
                "{reason}: {refused:?}"
            );
            assert_eq!(refused.event_code(), "app_key_not_announced");
            assert_eq!(seen.lock().unwrap().len(), 1, "a final answer");
            assert_eq!(crate::consent::chat_key::bound_chat_key(&store), None);
        }
    }

    /// f3129: a newer Cloud refusing the announcement itself at the device
    /// code (`400 invalid_field`, field `app_keys`) is a clear Config error
    /// that the re-pair names; without an announcement it stays as it was.
    #[test]
    fn f3129_app_keys_bad_announcement_is_a_clear_config_error() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let chat = offer_for(&test_key(), &keys.public_key_b64());
        let offers = AppKeyOffers {
            chat: Some(&chat),
            ..AppKeyOffers::default()
        };
        let refusal = json!({"error": {"code": "invalid_field", "field": "app_keys",
            "message": "x"}});
        let (origin, seen) = one_answer_cloud(400, &refusal);
        let client = CloudClient::new(&origin).unwrap();
        let refused = start_device_flow_with(&client, None, None, offers).expect_err("refused");
        assert!(
            matches!(&refused, CloudError::Config(text) if text == APP_KEY_PAIRING_FAILED),
            "{refused:?}"
        );
        assert_eq!(
            chat_key_repair_refusal(&refused),
            Some((None, APP_KEY_PAIRING_FAILED))
        );
        assert_eq!(
            seen.lock().unwrap().len(),
            1,
            "sent once, never without them"
        );
        // Without an announcement the same answer is not remapped.
        let (origin, _) = one_answer_cloud(400, &refusal);
        let client = CloudClient::new(&origin).unwrap();
        let plain = start_device_flow_with(&client, None, None, AppKeyOffers::default())
            .expect_err("refused");
        assert!(!matches!(&plain, CloudError::Config(_)), "{plain:?}");
    }

    /// f3129: a re-pair against a code answer without `device_app_keys`
    /// sends nothing and binds nothing.
    #[test]
    fn f3129_chat_key_repair_fails_closed_without_the_cloud_feature() {
        let (answer, root) = fixture_answer();
        let (origin, seen) = one_answer_cloud(200, &answer);
        let (_dir, store, keys) = connected(&origin);
        let client = CloudClient::new(&origin).unwrap();
        let good = offer_for(&test_key(), &keys.public_key_b64());
        let mut old = code();
        old.features.clear();
        let refused = repair_chat_key_at(&store, &client, &old, &root, &|_| {}, &good, fixture_now)
            .expect_err("no feature");
        assert!(
            matches!(&refused, CloudError::Config(text) if text == APP_KEYS_UNSUPPORTED),
            "{refused:?}"
        );
        assert_eq!(
            chat_key_repair_refusal(&refused),
            Some((None, APP_KEYS_UNSUPPORTED))
        );
        assert!(seen.lock().unwrap().is_empty(), "nothing sent");
        assert_eq!(crate::consent::chat_key::bound_chat_key(&store), None);
    }

    /// bd 40488: an older Cloud's 400 on a trial with keys names the trial.
    #[test]
    fn f3129_an_old_cloud_refusing_a_trial_with_keys_names_the_trial() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let keys = InstanceKeys::load_or_create(&store).unwrap();
        let chat = offer_for(&test_key(), &keys.public_key_b64());
        let offers = AppKeyOffers {
            chat: Some(&chat),
            ..AppKeyOffers::default()
        };
        let old_cloud = json!({"error": {"code": "invalid_request", "message": "x"}});
        let (origin, _) = one_answer_cloud(400, &old_cloud);
        let client = CloudClient::new(&origin).unwrap();
        let refused = start_device_flow_with(&client, None, Some(TrialPlan::Personal), offers)
            .expect_err("old");
        assert!(
            matches!(&refused, CloudError::TrialUnsupported { .. }),
            "{refused:?}"
        );
    }

    /// f3129 S3: a token has no device code to sign over, so a chat key with
    /// a token is refused before anything is sent.
    #[test]
    fn f3129_app_keys_never_go_with_an_enrollment_token() {
        let (answer, root) = fixture_answer();
        let (origin, seen) = one_answer_cloud(200, &answer);
        let (_dir, store, keys) = connected(&origin);
        let chat = offer_for(&test_key(), &keys.public_key_b64());
        let client = CloudClient::new(&origin).unwrap();
        let token = EnrollmentToken::parse(
            "het_0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
        )
        .unwrap();
        let refused = enroll_with_token_chat(&store, &client, &token, "m", &root, Some(&chat))
            .expect_err("refused");
        assert!(
            matches!(&refused, CloudError::Config(text) if text == CHAT_KEY_WITH_TOKEN),
            "{refused:?}"
        );
        assert!(seen.lock().unwrap().is_empty(), "nothing sent");
    }
}