granite-cli 0.1.7

CLI for discovering, configuring, and launching AI workflows powered by IBM Granite models.
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
// Standard
use std::collections::{HashMap, HashSet};

// Third Party
use alog::{MessageLevel, alog_channel, use_channel};
use anyhow::Result;

// Local
use crate::capabilities::{BindingType, CAPABILITY_REGISTRY, Dependency, ModelRequirement};
use crate::commands::model::ModelCommands;
use crate::dependency::{Configured, Requirement};
use crate::launchers::LAUNCHER_REGISTRY;
use crate::models::{
    ContextFit, MODEL_REGISTRY, ModelFunction, ModelMetadata, ModelType, ModelVariant,
};
use crate::providers::{HealthStatus, PROVIDER_REGISTRY, Provider};
use crate::utils::hardware::{HardwareProfile, detect_hardware};

use_channel!("SETUP");

/*-- public --*/

/// A single recommendation produced during discovery.
pub enum Recommendation {
    Provider {
        provider_type: &'static str,
        provider_name: String,
        health_healthy: bool,
        health_error: Option<String>,
    },
    Model {
        model_id: String,
        family: String,
        version: String,
        size: String,
        model_type: ModelType,
        best_variant: ModelVariant,
        context_fit: ContextFit,
        can_run_by: Vec<String>,
    },
    Launcher {
        launcher_type: String,
        launcher_name: String,
        binary_path: Option<String>,
    },
    Capability {
        capability_type: String,
        capability_name: String,
    },
}

/// The complete output of the discovery engine.
pub struct DiscoveryResult {
    pub recommendations: Vec<Recommendation>,
    /// Every unconfigured model with at least a partial hardware fit, one
    /// entry per catalog model_id (not deduplicated by family/version like
    /// `recommendations` is). Used by the "choose different models" escape
    /// hatch so a user can see and pick models that don't fully fit.
    pub all_model_candidates: Vec<Recommendation>,
    pub configured_provider_ids: Vec<String>,
    pub configured_model_ids: Vec<String>,
    pub configured_launcher_ids: Vec<String>,
    pub configured_capability_ids: Vec<String>,
}

/*-- private --*/

/// Discovers all available providers, models, launchers, and capabilities,
/// returning structured recommendations and a list of already-configured items.
struct Discover;

impl Discover {
    /// Run the full discovery pipeline against the machine's real hardware.
    pub async fn run(ctx: &crate::AppContext) -> DiscoveryResult {
        Self::run_with_hardware(ctx, &detect_hardware()).await
    }

    /// Run the full discovery pipeline against a given hardware profile.
    /// Split out from `run` so tests can pin the hardware profile instead of
    /// depending on `detect_hardware()`'s result on whatever machine the
    /// test happens to run on -- model-fit outcomes (and therefore which
    /// recommendations come out) are a direct function of the profile.
    async fn run_with_hardware(
        ctx: &crate::AppContext,
        profile: &HardwareProfile,
    ) -> DiscoveryResult {
        let (provider_recs, configured_providers) = Self::discover_providers(ctx).await;
        let model_recs = Self::discover_models(ctx, &configured_providers, profile);
        let all_model_candidates =
            Self::discover_all_model_candidates(ctx, &configured_providers, profile);
        let (launcher_recs, configured_launchers) = Self::discover_launchers(ctx).await;
        let (capability_recs, configured_capabilities) =
            Self::discover_capabilities(ctx, &provider_recs, &model_recs, &configured_providers);

        let mut recommendations: Vec<Recommendation> = Vec::new();
        recommendations.extend(provider_recs);
        recommendations.extend(model_recs);
        recommendations.extend(launcher_recs);
        recommendations.extend(capability_recs);

        // Sort for deterministic output
        recommendations.sort_by_key(display_name);

        DiscoveryResult {
            recommendations,
            all_model_candidates,
            configured_provider_ids: configured_providers,
            configured_model_ids: ctx.config.models.keys().cloned().collect(),
            configured_launcher_ids: configured_launchers,
            configured_capability_ids: configured_capabilities,
        }
    }

    // -- Provider discovery --------------------------------------------------

    async fn discover_providers(ctx: &crate::AppContext) -> (Vec<Recommendation>, Vec<String>) {
        let configured_ids: HashSet<&str> =
            ctx.config.providers.keys().map(|s| s.as_str()).collect();
        let mut configured: Vec<String> = Vec::new();
        let mut recommendations: Vec<Recommendation> = Vec::new();

        for (provider_type, metadata) in PROVIDER_REGISTRY.entries() {
            if configured_ids.contains(provider_type) {
                configured.push(provider_type.to_string());
                continue;
            }

            // Construct a transient instance with default config and run health check
            let default_config = PROVIDER_REGISTRY
                .default_config(provider_type)
                .unwrap_or_default();
            let result = PROVIDER_REGISTRY.construct(
                provider_type,
                provider_type,
                &default_config,
                &ctx.config,
            );

            match result {
                Ok(provider) => match Self::run_health_check(&*provider).await {
                    Ok(status) => recommendations.push(Recommendation::Provider {
                        provider_type,
                        provider_name: metadata.name.clone(),
                        health_healthy: status.healthy,
                        health_error: status.error,
                    }),
                    Err(e) => recommendations.push(Recommendation::Provider {
                        provider_type,
                        provider_name: metadata.name.clone(),
                        health_healthy: false,
                        health_error: Some(format!("Health check failed: {e}")),
                    }),
                },
                Err(_) => {
                    // Provider could not be constructed (e.g., missing schema).
                    // Still recommend it — user may need to configure it manually.
                    recommendations.push(Recommendation::Provider {
                        provider_type,
                        provider_name: metadata.name.clone(),
                        health_healthy: false,
                        health_error: Some(
                            "Could not construct provider with default config".to_string(),
                        ),
                    });
                }
            }
        }

        configured.sort();
        recommendations.sort_by_key(display_name);
        (recommendations, configured)
    }

    async fn run_health_check(
        provider: &dyn Provider,
    ) -> Result<HealthStatus, crate::providers::ProviderError> {
        provider.health_check().await
    }

    // -- Model discovery -----------------------------------------------------

    fn discover_models(
        ctx: &crate::AppContext,
        configured_provider_ids: &[String],
        profile: &HardwareProfile,
    ) -> Vec<Recommendation> {
        let configured_ids: HashSet<&str> = ctx.config.models.keys().map(|s| s.as_str()).collect();

        // Group models by family, keeping each model's real catalog id
        // alongside its metadata.
        let mut family_groups: HashMap<String, Vec<(String, ModelMetadata)>> = HashMap::new();
        for (model_id, model_md) in MODEL_REGISTRY.entries() {
            if configured_ids.contains(model_id) {
                continue;
            }
            let family = model_md.family.clone();
            family_groups
                .entry(family)
                .or_default()
                .push((model_id.to_string(), model_md));
        }

        let mut recommendations: Vec<Recommendation> = Vec::new();

        for models in family_groups.values() {
            // Find the latest version string present in this family (a
            // family can release several sizes at the same version, e.g.
            // granite-4.2-3b/8b/30b are all version "4.2").
            let Some((_, sample)) = find_latest_version(models) else {
                continue;
            };
            let latest_version = &sample.version;

            // Among every size released at that version, recommend only
            // the largest one that *fully* fits the current hardware.
            // Partially-fitting models are never auto-recommended here --
            // the user can still reach them via `select_models_manually`.
            let best = models
                .iter()
                .filter(|(_, md)| &md.version == latest_version)
                .filter_map(|(id, md)| {
                    Self::build_model_recommendation(
                        id,
                        md,
                        profile,
                        configured_provider_ids,
                        ctx,
                        true,
                    )
                })
                .max_by_key(|rec| match rec {
                    Recommendation::Model { size, .. } => parse_size(size),
                    _ => 0,
                });

            if let Some(rec) = best {
                recommendations.push(rec);
            }
        }

        sort_model_recommendations(&mut recommendations);
        recommendations
    }

    /// Every unconfigured model with at least a partial fit, one entry per
    /// catalog model_id -- the full pool "choose different models" picks
    /// from, unfiltered by the "only fully-fitting" / "one per family" rules
    /// `discover_models` applies for the default recommendation.
    fn discover_all_model_candidates(
        ctx: &crate::AppContext,
        configured_provider_ids: &[String],
        profile: &HardwareProfile,
    ) -> Vec<Recommendation> {
        let configured_ids: HashSet<&str> = ctx.config.models.keys().map(|s| s.as_str()).collect();

        let mut recommendations: Vec<Recommendation> = MODEL_REGISTRY
            .entries()
            .into_iter()
            .filter(|(model_id, _)| !configured_ids.contains(*model_id))
            .filter_map(|(model_id, md)| {
                Self::build_model_recommendation(
                    model_id,
                    &md,
                    profile,
                    configured_provider_ids,
                    ctx,
                    false,
                )
            })
            .collect();

        sort_model_recommendations(&mut recommendations);
        recommendations
    }

    /// Builds a `Recommendation::Model` for `model_id` using its best-fitting
    /// variant for `profile`. When `require_full_fit` is true, only a
    /// `ContextFit::Full` result is accepted (used for the default,
    /// one-per-family recommendation); otherwise any non-`None` fit is
    /// accepted (used for the full candidate pool).
    fn build_model_recommendation(
        model_id: &str,
        md: &ModelMetadata,
        profile: &crate::utils::hardware::HardwareProfile,
        configured_provider_ids: &[String],
        ctx: &crate::AppContext,
        require_full_fit: bool,
    ) -> Option<Recommendation> {
        let (variant, fit) = best_variant(md, profile)?;
        if require_full_fit && fit != ContextFit::Full {
            return None;
        }
        let can_run_by = Self::find_can_run_providers(&variant, configured_provider_ids, ctx);
        Some(Recommendation::Model {
            model_id: model_id.to_string(),
            family: md.family.clone(),
            version: md.version.clone(),
            size: format_size(md.size),
            model_type: md.model_type.clone(),
            best_variant: variant,
            context_fit: fit,
            can_run_by,
        })
    }

    fn find_can_run_providers(
        variant: &ModelVariant,
        configured_provider_ids: &[String],
        ctx: &crate::AppContext,
    ) -> Vec<String> {
        configured_provider_ids
            .iter()
            .filter_map(|pid| ctx.config.get_provider(pid))
            .filter_map(|pc| {
                PROVIDER_REGISTRY
                    .construct(&pc.provider_type, &pc.provider_id, &pc.config, &ctx.config)
                    .ok()
                    .filter(|p| p.can_run_model(&variant.format, &variant.precision))
            })
            .map(|p| p.instance_id().to_string())
            .collect()
    }

    // -- Launcher discovery --------------------------------------------------

    async fn discover_launchers(ctx: &crate::AppContext) -> (Vec<Recommendation>, Vec<String>) {
        let configured_ids: HashSet<&str> =
            ctx.config.launchers.keys().map(|s| s.as_str()).collect();
        let mut configured: Vec<String> = Vec::new();
        let mut recommendations: Vec<Recommendation> = Vec::new();

        for (launcher_type, metadata) in LAUNCHER_REGISTRY.entries() {
            if configured_ids.contains(launcher_type) {
                configured.push(launcher_type.to_string());
                continue;
            }

            // Construct a transient instance with default config
            let default_config = LAUNCHER_REGISTRY
                .default_config(launcher_type)
                .unwrap_or_default();
            match LAUNCHER_REGISTRY.construct(
                launcher_type,
                launcher_type,
                &default_config,
                &ctx.config,
            ) {
                Ok(launcher) => match launcher.validate_command() {
                    Ok(path) => recommendations.push(Recommendation::Launcher {
                        launcher_type: launcher_type.to_string(),
                        launcher_name: metadata.name.clone(),
                        binary_path: Some(path.to_string_lossy().to_string()),
                    }),
                    Err(_) => recommendations.push(Recommendation::Launcher {
                        launcher_type: launcher_type.to_string(),
                        launcher_name: metadata.name.clone(),
                        binary_path: None,
                    }),
                },
                Err(_) => {
                    // Could not construct — still recommend but without binary info
                    recommendations.push(Recommendation::Launcher {
                        launcher_type: launcher_type.to_string(),
                        launcher_name: metadata.name.clone(),
                        binary_path: None,
                    });
                }
            }
        }

        configured.sort();
        recommendations.sort_by_key(display_name);
        (recommendations, configured)
    }

    // -- Capability discovery ------------------------------------------------

    fn discover_capabilities(
        ctx: &crate::AppContext,
        _provider_recs: &[Recommendation],
        _model_recs: &[Recommendation],
        _configured_provider_ids: &[String],
    ) -> (Vec<Recommendation>, Vec<String>) {
        let configured_ids: Vec<&str> =
            ctx.config.capabilities.keys().map(|s| s.as_str()).collect();
        let configured: Vec<String> = configured_ids.iter().copied().map(String::from).collect();

        let mut recommendations: Vec<Recommendation> = Vec::new();

        for (capability_type, metadata) in CAPABILITY_REGISTRY.entries() {
            if configured_ids.contains(&capability_type) {
                continue;
            }

            recommendations.push(Recommendation::Capability {
                capability_type: capability_type.to_string(),
                capability_name: metadata.name.clone(),
            });
        }

        recommendations.sort_by_key(display_name);
        (recommendations, configured)
    }
}

/// A re-evaluator that filters recommendations based on user selections from
/// earlier wizard sections. This implements the backward-from-capabilities
/// dependency flow.
struct Revaluator;

impl Revaluator {
    /// Filter launcher recommendations to only those that support at least one
    /// of the selected capability binding types.
    fn for_launchers<'a>(
        discovery: &'a DiscoveryResult,
        selected_cap_types: &HashSet<String>,
    ) -> Vec<&'a Recommendation> {
        // Determine which binding types are needed by selected capabilities
        let needed_types: HashSet<BindingType> = selected_cap_types
            .iter()
            .filter_map(|cap_type| CAPABILITY_REGISTRY.get(cap_type))
            .flat_map(|m| m.supported_binding_types.clone().into_iter())
            .collect();

        if needed_types.is_empty() {
            // No binding types needed — any launcher is fine
            return discovery
                .recommendations
                .iter()
                .filter(|r| matches!(r, Recommendation::Launcher { .. }))
                .collect();
        }

        discovery
            .recommendations
            .iter()
            .filter(move |r| {
                if let Recommendation::Launcher { launcher_type, .. } = r {
                    if let Some(launcher_meta) = LAUNCHER_REGISTRY.get(launcher_type) {
                        return launcher_meta
                            .supported_capabilities
                            .iter()
                            .any(|bt| needed_types.contains(bt));
                    }
                    // If we can't look up the launcher, include it anyway
                    return true;
                }
                false
            })
            .collect()
    }

    /// Filter provider recommendations to only those that can run at least one
    /// of the selected model variants.
    fn for_providers<'a>(
        discovery: &'a DiscoveryResult,
        selected_model_ids: &HashSet<String>,
        _ctx: &crate::AppContext,
    ) -> Vec<&'a Recommendation> {
        let provider_recs: Vec<_> = discovery
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Provider { .. }))
            .collect();

        // If no models selected, return all provider recommendations
        if selected_model_ids.is_empty() {
            return provider_recs;
        }

        // Build a set of selected model IDs for quick lookup
        let selected_ids: HashSet<&str> = selected_model_ids.iter().map(|s| s.as_str()).collect();

        // Check which providers can run the selected models by looking at the
        // can_run_by field in the model recommendations
        let providers_that_can_run: HashSet<String> = discovery
            .recommendations
            .iter()
            .filter_map(|r| match r {
                Recommendation::Model {
                    model_id,
                    can_run_by,
                    ..
                } => {
                    if selected_ids.contains(model_id.as_str()) {
                        Some(can_run_by.clone())
                    } else {
                        None
                    }
                }
                _ => None,
            })
            .flatten()
            .collect();

        if providers_that_can_run.is_empty() {
            // If no provider info available, show all providers
            return provider_recs;
        }

        provider_recs
            .into_iter()
            .filter(move |r| {
                if let Recommendation::Provider { provider_type, .. } = r {
                    providers_that_can_run.contains(*provider_type)
                } else {
                    false
                }
            })
            .collect()
    }

    /// Filter model recommendations to only those that would be used by at least
    /// one selected capability (i.e., satisfy the capability's `ModelRequirement`).
    fn for_models<'a>(
        recommendations: &'a [Recommendation],
        selected_cap_types: &HashSet<String>,
    ) -> Vec<&'a Recommendation> {
        // Collect all model requirements from selected capabilities
        let all_requirements: Vec<ModelRequirement> = selected_cap_types
            .iter()
            .filter_map(|cap_type| CAPABILITY_REGISTRY.get(cap_type))
            .flat_map(|m| {
                m.dependencies
                    .iter()
                    .filter_map(|d| match d {
                        Dependency::Model {
                            requirement,
                            resolved_id: None,
                            ..
                        } => Some(requirement.clone()),
                        _ => None,
                    })
                    .collect::<Vec<_>>()
            })
            .collect();

        if all_requirements.is_empty() {
            // No model requirements — show all model recommendations
            return recommendations
                .iter()
                .filter(|r| matches!(r, Recommendation::Model { .. }))
                .collect();
        }

        recommendations
            .iter()
            .filter(move |r| {
                if let Recommendation::Model { model_id, .. } = r {
                    // Look up the real catalog metadata (family/version/size
                    // alone can't tell us `supported_functions`, which most
                    // capability requirements actually key on).
                    match MODEL_REGISTRY.get(model_id) {
                        Some(md) => all_requirements
                            .iter()
                            .any(|req| admits_for_recommendation(req, &md)),
                        None => false,
                    }
                } else {
                    false
                }
            })
            .collect()
    }

    /// Filter capability recommendations to only those that could actually be
    /// used by at least one selected launcher (i.e., the launcher supports
    /// one of the capability's declared binding types). With no launchers
    /// selected, no capability has anywhere to bind, so none are shown.
    fn for_capabilities<'a>(
        discovery: &'a DiscoveryResult,
        selected_launcher_types: &HashSet<String>,
    ) -> Vec<&'a Recommendation> {
        if selected_launcher_types.is_empty() {
            return Vec::new();
        }

        let supported_types: HashSet<BindingType> = selected_launcher_types
            .iter()
            .filter_map(|lt| LAUNCHER_REGISTRY.get(lt))
            .flat_map(|m| m.supported_capabilities.clone().into_iter())
            .collect();

        discovery
            .recommendations
            .iter()
            .filter(move |r| {
                if let Recommendation::Capability {
                    capability_type, ..
                } = r
                {
                    if let Some(cap_meta) = CAPABILITY_REGISTRY.get(capability_type) {
                        return cap_meta
                            .supported_binding_types
                            .iter()
                            .any(|bt| supported_types.contains(bt));
                    }
                    // If we can't look up the capability, include it anyway
                    true
                } else {
                    false
                }
            })
            .collect()
    }
}

/*-- helpers -----------------------------------------------------------------*/

/// Compare semantic versions in descending order (higher versions first).
fn compare_versions_desc(a: &str, b: &str) -> std::cmp::Ordering {
    let parse_version =
        |v: &str| -> Vec<u32> { v.split('.').filter_map(|s| s.parse::<u32>().ok()).collect() };

    let va = parse_version(a);
    let vb = parse_version(b);

    for (a_part, b_part) in va.iter().zip(vb.iter()) {
        match b_part.cmp(a_part) {
            std::cmp::Ordering::Equal => continue,
            other => return other,
        }
    }

    vb.len().cmp(&va.len())
}

fn find_latest_version(models: &[(String, ModelMetadata)]) -> Option<&(String, ModelMetadata)> {
    // `compare_versions_desc` is a *reversed* comparator (higher version
    // sorts first when fed to `sort_by`/`sort_by_key`, i.e. it reports the
    // higher version as `Less`) -- so picking the "latest" requires
    // `min_by`, not `max_by`. `max_by` would select the lowest version in
    // the family instead.
    models
        .iter()
        .min_by(|(_, a), (_, b)| compare_versions_desc(&a.version, &b.version))
}

fn format_size(size: u64) -> String {
    match size {
        1_000_000_000.. => format!("{}B", size / 1_000_000_000),
        1_000_000.. => format!("{}M", size / 1_000_000),
        _ => size.to_string(),
    }
}

fn parse_size(size_str: &str) -> u64 {
    let size_str = size_str.trim();
    if let Some(num) = size_str.strip_suffix('B') {
        num.parse::<u64>().unwrap_or(0) * 1_000_000_000
    } else if let Some(num) = size_str.strip_suffix('M') {
        num.parse::<u64>().unwrap_or(0) * 1_000_000
    } else {
        size_str.parse::<u64>().unwrap_or(0)
    }
}

fn best_variant(
    model: &ModelMetadata,
    profile: &crate::utils::hardware::HardwareProfile,
) -> Option<(ModelVariant, ContextFit)> {
    let fit_rank = |fit: &ContextFit| match fit {
        ContextFit::Full => 1,
        ContextFit::Partial(_) => 0,
        ContextFit::None => -1,
    };

    model
        .variants
        .iter()
        .map(|v| {
            let fit = crate::models::context_fit::estimate(
                model.context_length,
                &model.architecture,
                &model.native_dtype,
                v,
                profile,
            );
            (fit, v)
        })
        .filter(|(fit, _)| *fit != ContextFit::None)
        .max_by(|(fit_a, a), (fit_b, b)| {
            fit_rank(fit_a).cmp(&fit_rank(fit_b)).then_with(|| {
                a.size_gb
                    .partial_cmp(&b.size_gb)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
        })
        .map(|(fit, v)| (v.clone(), fit))
}

fn display_name(rec: &Recommendation) -> String {
    match rec {
        Recommendation::Provider { provider_name, .. } => provider_name.clone(),
        Recommendation::Model {
            family,
            version,
            size,
            ..
        } => format!("{family} {version} {size}"),
        Recommendation::Launcher { launcher_name, .. } => launcher_name.clone(),
        Recommendation::Capability {
            capability_name, ..
        } => capability_name.clone(),
    }
}

/// Extracts `(family, version, size)` from a model recommendation for
/// sorting purposes, without needing a full `ModelMetadata`.
fn model_sort_key(rec: &Recommendation) -> (&str, &str, u64) {
    match rec {
        Recommendation::Model {
            family,
            version,
            size,
            ..
        } => (family.as_str(), version.as_str(), parse_size(size)),
        _ => ("", "", 0),
    }
}

/// Sorts model recommendations by family, then version descending, then
/// size descending -- shared by every discovery pass that produces a list
/// of `Recommendation::Model`.
fn sort_model_recommendations(recommendations: &mut [Recommendation]) {
    recommendations.sort_by(|a, b| {
        let (a_family, a_version, a_size) = model_sort_key(a);
        let (b_family, b_version, b_size) = model_sort_key(b);
        a_family
            .cmp(b_family)
            .then_with(|| compare_versions_desc(a_version, b_version))
            .then_with(|| b_size.cmp(&a_size))
    });
}

/// Model functions that make a model "multi-modal" -- image or audio
/// input. A model reporting one of these can usually still `Chat`, but it's
/// not *intended* as a general chat model, so it shouldn't be recommended
/// for a capability that only asked for `Chat`/`ToolCalling`/etc.
const MULTIMODAL_FUNCTIONS: &[ModelFunction] = &[
    ModelFunction::ImageUnderstanding,
    ModelFunction::Transcription,
    ModelFunction::Translation,
    ModelFunction::SpeakerAttribution,
    ModelFunction::KeywordBiasing,
];

/// Whether `md` should be recommended for a capability declaring `req`.
/// Layers an extra rule on top of `ModelRequirement::admits_type`: if `req`
/// doesn't itself ask for any multi-modal function, a multi-modal model is
/// excluded even though it technically satisfies the requirement's function
/// list (e.g. a vision model supports `Chat` too, but a plain agent-model
/// binding isn't looking for a vision model).
fn admits_for_recommendation(req: &ModelRequirement, md: &ModelMetadata) -> bool {
    if !req.admits_type(md) {
        return false;
    }
    let req_wants_multimodal = req
        .supported_functions
        .iter()
        .any(|f| MULTIMODAL_FUNCTIONS.contains(f));
    req_wants_multimodal
        || !md
            .supported_functions
            .iter()
            .any(|f| MULTIMODAL_FUNCTIONS.contains(f))
}

/*-- SetupCommands -----------------------------------------------------------*/

pub struct SetupCommands;

impl SetupCommands {
    /// Entry point for `granite-cli setup`.
    pub async fn run(ctx: &mut crate::AppContext, auto: bool, skip_pull: bool) -> Result<()> {
        if auto {
            Self::run_auto(ctx).await
        } else {
            Self::run_wizard(ctx, skip_pull).await
        }
    }

    /// Run the interactive wizard.
    async fn run_wizard(ctx: &mut crate::AppContext, skip_pull: bool) -> Result<()> {
        let ui = &*ctx.ui;
        ui.info("=== granite-cli Setup Wizard ===\n");
        ui.info("Discovering available components...\n");

        let discovery = Discover::run(ctx).await;

        if discovery.recommendations.is_empty()
            && discovery.configured_provider_ids.is_empty()
            && discovery.configured_model_ids.is_empty()
            && discovery.configured_launcher_ids.is_empty()
            && discovery.configured_capability_ids.is_empty()
        {
            ui.info(
                "Nothing to configure. All components are either not available or already set up.",
            );
            return Ok(());
        }

        // Phase 1: Launchers selection (show all detected launchers)
        let selected_launchers = Self::select_launchers(ctx, &discovery).await?;

        // Phase 2: Capabilities selection (filtered by what the selected
        // launchers can actually bind)
        let selected_caps = Self::select_capabilities(ctx, &discovery, &selected_launchers).await?;

        // Phase 3: Models selection (filtered by capability requirements)
        let selected_models = Self::select_models(ctx, &discovery, &selected_caps).await?;

        // Phase 4: Providers selection (only healthy, filtered by model compatibility)
        let selected_providers = Self::select_providers(ctx, &discovery, &selected_models).await?;

        // Phase 4.5: Variant selection (limited to formats the selected
        // providers can actually run, with a VRAM estimate at full context)
        let selected_variants =
            Self::select_variants(ctx, &discovery, &selected_models, &selected_providers).await?;

        // Phase 5: Configuration
        Self::configure_all(
            ctx,
            &discovery,
            &selected_caps,
            &selected_launchers,
            &selected_providers,
            &selected_models,
            &selected_variants,
        )
        .await?;

        // Phase 6: Pull (optional)
        if !skip_pull {
            Self::prompt_pull(ctx, &selected_models).await?;
        }

        // Phase 7: Summary
        Self::print_summary(
            ctx,
            &selected_caps,
            &selected_launchers,
            &selected_providers,
            &selected_models,
        );

        Ok(())
    }

    /// Run auto mode — detect, configure everything with defaults.
    async fn run_auto(ctx: &mut crate::AppContext) -> Result<()> {
        let ui = &*ctx.ui;
        ui.info("=== granite-cli Auto Setup ===\n");
        ui.info("Auto-detecting and configuring all available components...\n");

        let discovery = Discover::run(ctx).await;

        if discovery.recommendations.is_empty() {
            ui.info("No components available to configure.");
            return Ok(());
        }

        // Auto-select everything that's recommended, following the same
        // Launchers → Capabilities → Models → Providers dependency chain as
        // the interactive wizard.
        let selected_launchers: HashSet<String> = discovery
            .recommendations
            .iter()
            .filter_map(|r| match r {
                Recommendation::Launcher {
                    launcher_type,
                    binary_path: Some(_),
                    ..
                } => Some(launcher_type.clone()),
                _ => None,
            })
            .collect();

        let selected_caps: HashSet<String> =
            Revaluator::for_capabilities(&discovery, &selected_launchers)
                .into_iter()
                .filter_map(|r| match r {
                    Recommendation::Capability {
                        capability_type, ..
                    } => Some(capability_type.clone()),
                    _ => None,
                })
                .collect();

        let selected_models: HashSet<String> =
            Revaluator::for_models(&discovery.recommendations, &selected_caps)
                .into_iter()
                .filter_map(|r| match r {
                    Recommendation::Model { model_id, .. } => Some(model_id.clone()),
                    _ => None,
                })
                .collect();

        let selected_providers: HashSet<String> =
            Revaluator::for_providers(&discovery, &selected_models, ctx)
                .into_iter()
                .filter_map(|r| match r {
                    Recommendation::Provider {
                        provider_type,
                        health_healthy,
                        ..
                    } if *health_healthy => Some(provider_type.to_string()),
                    _ => None,
                })
                .collect();

        // --auto is non-interactive, so there's no prompt for variant
        // selection -- `configure_all` falls back to discovery's
        // hardware-fit `best_variant` for every model.
        Self::configure_all(
            ctx,
            &discovery,
            &selected_caps,
            &selected_launchers,
            &selected_providers,
            &selected_models,
            &HashMap::new(),
        )
        .await?;

        // Never auto-pull in --auto mode
        Self::print_summary(
            ctx,
            &selected_caps,
            &selected_launchers,
            &selected_providers,
            &selected_models,
        );

        Ok(())
    }

    /*-- Selection phases ----------------------------------------------------*/

    async fn select_capabilities(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_launchers: &HashSet<String>,
    ) -> Result<HashSet<String>> {
        let ui = &*ctx.ui;

        let caps: Vec<_> = Revaluator::for_capabilities(discovery, selected_launchers)
            .into_iter()
            .filter_map(|r| match r {
                Recommendation::Capability {
                    capability_type,
                    capability_name,
                } => Some((capability_type.clone(), capability_name.clone())),
                _ => None,
            })
            .collect();

        if caps.is_empty() {
            ui.info("No capabilities available for the selected launchers.");
            return Ok(HashSet::new());
        }

        let items: Vec<String> = caps
            .iter()
            .map(|(id, name)| format!("{id} — {name}"))
            .collect();
        let defaults = vec![true; items.len()];

        let selected = ui.multi_select("Select capabilities to configure", &items, &defaults)?;

        Ok(selected.into_iter().map(|i| caps[i].0.clone()).collect())
    }

    async fn select_launchers(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
    ) -> Result<HashSet<String>> {
        let ui = &*ctx.ui;

        let filtered: Vec<_> = Revaluator::for_launchers(discovery, &HashSet::new())
            .into_iter()
            .filter_map(|r| match r {
                Recommendation::Launcher {
                    launcher_type,
                    launcher_name,
                    binary_path: Some(binary_path),
                } => Some((launcher_type.clone(), launcher_name, binary_path.clone())),
                _ => None,
            })
            .collect();

        if filtered.is_empty() {
            ui.info("No launchers detected on this system.");
            return Ok(HashSet::new());
        }

        let items: Vec<String> = filtered
            .iter()
            .map(|(id, name, path)| format!("{id} — {name} ({path})"))
            .collect();
        let defaults = vec![false; items.len()];

        let selected = ui.multi_select("Select launchers to configure", &items, &defaults)?;

        Ok(selected
            .into_iter()
            .map(|i| filtered[i].0.clone())
            .collect())
    }

    async fn select_providers(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_models: &HashSet<String>,
    ) -> Result<HashSet<String>> {
        let ui = &*ctx.ui;

        let filtered: Vec<_> = Revaluator::for_providers(discovery, selected_models, ctx)
            .into_iter()
            .filter_map(|r| match r {
                Recommendation::Provider {
                    provider_type,
                    provider_name,
                    health_healthy,
                    health_error,
                } if *health_healthy => Some((
                    provider_type.to_string(),
                    provider_name,
                    health_error.clone(),
                )),
                _ => None,
            })
            .collect();

        if filtered.is_empty() {
            ui.info("No healthy providers available to configure.");
            return Ok(HashSet::new());
        }

        let items: Vec<String> = filtered
            .iter()
            .map(|(id, name, error)| {
                let status = if error.is_none() || error.as_ref().is_some_and(|e| e.is_empty()) {
                    "healthy".to_string()
                } else if let Some(e) = &error {
                    format!("healthy ({e})")
                } else {
                    "healthy".to_string()
                };
                format!("{id} — {name} ({status})")
            })
            .collect();
        let defaults = vec![false; items.len()];

        let selected = ui.multi_select("Select providers to configure", &items, &defaults)?;

        Ok(selected
            .into_iter()
            .map(|i| filtered[i].0.clone())
            .collect())
    }

    /// Phase 4.5: let the user pick a specific variant for each selected
    /// model, once both capabilities and providers are known. Options are
    /// limited to variants at least one selected provider can actually run,
    /// each annotated with an estimated VRAM/RAM footprint at the model's
    /// full configured context length. Models with only one compatible
    /// variant are auto-selected without prompting.
    async fn select_variants(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_models: &HashSet<String>,
        selected_providers: &HashSet<String>,
    ) -> Result<HashMap<String, ModelVariant>> {
        let mut chosen = HashMap::new();

        for model_id in selected_models {
            let Some(md) = MODEL_REGISTRY.get(model_id) else {
                continue;
            };

            let candidates = Self::candidate_variants(&md, selected_providers, ctx);
            if candidates.is_empty() {
                ctx.ui.warn(&format!(
                    "No selected provider can run any variant of '{model_id}'; leaving its variant unset."
                ));
                continue;
            }

            if candidates.len() == 1 {
                let (variant, gb) = &candidates[0];
                ctx.ui.info(&format!(
                    "Only one compatible variant for '{model_id}': {} — selected automatically.",
                    Self::format_variant_option(variant, *gb, md.context_length)
                ));
                chosen.insert(model_id.clone(), variant.clone());
                continue;
            }

            let recommended = discovery.recommendations.iter().find_map(|r| match r {
                Recommendation::Model {
                    model_id: rec_id,
                    best_variant,
                    ..
                } if rec_id == model_id => Some(best_variant.clone()),
                _ => None,
            });

            let default_idx = recommended
                .as_ref()
                .and_then(|rv| {
                    candidates.iter().position(|(v, _)| {
                        v.format.eq_ignore_ascii_case(&rv.format)
                            && v.precision.eq_ignore_ascii_case(&rv.precision)
                    })
                })
                .unwrap_or(0);

            let items: Vec<String> = candidates
                .iter()
                .map(|(v, gb)| Self::format_variant_option(v, *gb, md.context_length))
                .collect();

            let idx = ctx.ui.select(
                &format!("Select variant for {model_id}"),
                &items,
                default_idx,
            )?;
            chosen.insert(model_id.clone(), candidates[idx].0.clone());
        }

        Ok(chosen)
    }

    /// Variants of `md` that at least one selected provider can run, paired
    /// with their estimated required memory (GB) at `md.context_length`
    /// (i.e. full context). Providers are constructed transiently with
    /// registry defaults, matching how discovery evaluates them, since this
    /// runs before providers are written to config.
    fn candidate_variants(
        md: &ModelMetadata,
        selected_providers: &HashSet<String>,
        ctx: &crate::AppContext,
    ) -> Vec<(ModelVariant, f64)> {
        let providers: Vec<Box<dyn Provider>> = selected_providers
            .iter()
            .filter_map(|pid| {
                let default_config = PROVIDER_REGISTRY.default_config(pid).unwrap_or_default();
                PROVIDER_REGISTRY
                    .construct(pid, pid, &default_config, &ctx.config)
                    .ok()
            })
            .collect();

        md.variants
            .iter()
            .filter(|v| {
                providers
                    .iter()
                    .any(|p| p.can_run_model(&v.format, &v.precision))
            })
            .map(|v| {
                let gb = crate::models::required_gb(
                    &md.architecture,
                    v,
                    &md.native_dtype,
                    md.context_length,
                );
                (v.clone(), gb)
            })
            .collect()
    }

    fn format_variant_option(
        variant: &ModelVariant,
        required_gb: f64,
        context_length: u64,
    ) -> String {
        match variant.size_gb {
            Some(size) => format!(
                "{} / {} — {:.1} GB file, ~{:.1} GB VRAM @ full context ({context_length} tokens)",
                variant.format, variant.precision, size, required_gb
            ),
            None => format!(
                "{} / {} — ~{:.1} GB VRAM @ full context ({context_length} tokens)",
                variant.format, variant.precision, required_gb
            ),
        }
    }

    /// Label for the extra row appended to the default model list that
    /// hands off to `select_models_manually`.
    const CHOOSE_DIFFERENT_MODELS_LABEL: &'static str = "→ Choose different models…";

    fn format_model_option(id: &str, size: &str, fit: ContextFit, providers: &[String]) -> String {
        let providers_str = if providers.is_empty() {
            "none".to_string()
        } else {
            providers.join(", ")
        };
        format!("{id} — {size} — Fit: {fit} ({providers_str})")
    }

    fn model_options(recs: Vec<&Recommendation>) -> Vec<(String, String, ContextFit, Vec<String>)> {
        recs.into_iter()
            .filter_map(|r| match r {
                Recommendation::Model {
                    model_id,
                    size,
                    context_fit,
                    can_run_by,
                    ..
                } => Some((
                    model_id.clone(),
                    size.clone(),
                    *context_fit,
                    can_run_by.clone(),
                )),
                _ => None,
            })
            .collect()
    }

    async fn select_models(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_caps: &HashSet<String>,
    ) -> Result<HashSet<String>> {
        let ui = &*ctx.ui;

        let filtered = Self::model_options(Revaluator::for_models(
            &discovery.recommendations,
            selected_caps,
        ));

        let mut chosen: HashSet<String> = HashSet::new();
        let mut choose_different = filtered.is_empty();

        if filtered.is_empty() {
            ui.info("No models fully fit your hardware for the selected capabilities.");
        } else {
            let escape_hatch_idx = filtered.len();
            let mut items: Vec<String> = filtered
                .iter()
                .map(|(id, size, fit, providers)| {
                    Self::format_model_option(id, size, *fit, providers)
                })
                .collect();
            items.push(Self::CHOOSE_DIFFERENT_MODELS_LABEL.to_string());

            let mut defaults = vec![true; filtered.len()];
            defaults.push(false);

            let selected = ui.multi_select("Select models to configure", &items, &defaults)?;

            choose_different = selected.contains(&escape_hatch_idx);
            chosen = selected
                .into_iter()
                .filter(|&i| i < escape_hatch_idx)
                .map(|i| filtered[i].0.clone())
                .collect();
        }

        if choose_different {
            let manual = Self::select_models_manually(ctx, discovery, selected_caps).await?;
            chosen.extend(manual);
        }

        Ok(chosen)
    }

    /// Escape hatch from `select_models`: lets the user pick directly from
    /// every model that satisfies the selected capabilities' requirements,
    /// regardless of family/version deduplication or hardware fit (so long
    /// as it fits at least partially) -- with each option's fit value shown,
    /// including partial fits `discover_models` excludes from the default
    /// recommendation.
    async fn select_models_manually(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_caps: &HashSet<String>,
    ) -> Result<HashSet<String>> {
        let ui = &*ctx.ui;

        let filtered = Self::model_options(Revaluator::for_models(
            &discovery.all_model_candidates,
            selected_caps,
        ));

        if filtered.is_empty() {
            ui.info("No candidate models satisfy the selected capabilities.");
            return Ok(HashSet::new());
        }

        let items: Vec<String> = filtered
            .iter()
            .map(|(id, size, fit, providers)| Self::format_model_option(id, size, *fit, providers))
            .collect();
        let defaults = vec![false; items.len()];

        let selected = ui.multi_select("Choose models directly", &items, &defaults)?;

        Ok(selected
            .into_iter()
            .map(|i| filtered[i].0.clone())
            .collect())
    }

    /*-- Configuration phase -------------------------------------------------*/

    async fn configure_all(
        ctx: &mut crate::AppContext,
        discovery: &DiscoveryResult,
        selected_caps: &HashSet<String>,
        selected_launchers: &HashSet<String>,
        selected_providers: &HashSet<String>,
        selected_models: &HashSet<String>,
        selected_variants: &HashMap<String, ModelVariant>,
    ) -> Result<()> {
        let ui = &*ctx.ui;

        // Configure providers first
        for provider_id in selected_providers {
            ui.info(&format!("\nConfiguring provider: {provider_id}..."));
            let default_config = PROVIDER_REGISTRY
                .default_config(provider_id)
                .unwrap_or_default();

            let provider_config = crate::config::ProviderConfig {
                provider_id: provider_id.clone(),
                provider_type: provider_id.to_string(),
                config: default_config,
            };

            if ctx
                .config
                .insert_provider(provider_id, provider_config)
                .is_err()
            {
                ui.warn(&format!(
                    "Failed to save provider config for '{provider_id}'"
                ));
            }
        }

        // Configure launchers
        for launcher_id in selected_launchers {
            ui.info(&format!("\nConfiguring launcher: {launcher_id}..."));
            let default_config = LAUNCHER_REGISTRY
                .default_config(launcher_id)
                .unwrap_or_default();

            let launcher_config = crate::config::LauncherConfig {
                launcher_id: launcher_id.to_string(),
                launcher_type: launcher_id.to_string(),
                enabled_capabilities: Vec::new(),
                config: default_config,
            };

            if ctx
                .config
                .insert_launcher(launcher_id, launcher_config)
                .is_err()
            {
                ui.warn(&format!(
                    "Failed to save launcher config for '{launcher_id}'"
                ));
            }
        }

        // Configure models. Providers were just configured above, so
        // `ctx.config` already has real entries for every id in
        // `selected_providers` -- look one up and construct it live to
        // check actual format/precision compatibility, rather than relying
        // on discovery's `can_run_by` (which only reflects providers that
        // were *already* configured before this wizard run, and so is
        // always empty on a first-time setup).
        for model_id in selected_models {
            ui.info(&format!("\nConfiguring model: {model_id}..."));

            // Prefer the variant the user picked in the variant-selection
            // phase; fall back to discovery's hardware-fit recommendation
            // (e.g. in --auto mode, where that phase never runs).
            let chosen_variant = selected_variants.get(model_id).cloned().or_else(|| {
                discovery.recommendations.iter().find_map(|r| match r {
                    Recommendation::Model {
                        model_id: rec_id,
                        best_variant,
                        ..
                    } if rec_id == model_id => Some(best_variant.clone()),
                    _ => None,
                })
            });

            let (provider_id, variant) = match &chosen_variant {
                Some(v) => (
                    Self::find_compatible_provider(v, selected_providers, ctx)
                        .or_else(|| selected_providers.iter().next().cloned()),
                    Some(format!("{}/{}", v.format, v.precision)),
                ),
                None => (selected_providers.iter().next().cloned(), None),
            };
            if provider_id.is_none() {
                let err = format!("Failed to find provider for {model_id}/{chosen_variant:#?}");
                ui.warn(&err);
                anyhow::bail!(err);
            }

            let model_config = crate::config::ModelConfig {
                model_id: model_id.clone(),
                model_type: model_id.clone(),
                config: serde_json::json!({}),
                provider_id: provider_id.unwrap(),
                variant,
            };

            if ctx.config.insert_model(model_id, model_config).is_err() {
                ui.warn(&format!("Failed to save model config for '{model_id}'"));
            }
        }

        // Configure capabilities
        for cap_type in selected_caps {
            let model_id = Self::find_model_for_capability(cap_type, selected_models);

            // Skip capabilities that have a required model dependency but no
            // matching model was selected — saving them with model_id="" would
            // panic when CapabilitySource constructs the instance below.
            let needs_model = CAPABILITY_REGISTRY.get(cap_type).is_some_and(|meta| {
                meta.dependencies
                    .iter()
                    .any(|d| matches!(d, Dependency::Model { required: true, .. }))
            });
            if needs_model && model_id.is_none() {
                ui.warn(&format!(
                    "Skipping '{cap_type}': no compatible model available."
                ));
                continue;
            }

            ui.info(&format!("\nConfiguring capability: {cap_type}..."));

            let mut config = CAPABILITY_REGISTRY
                .default_config(cap_type)
                .unwrap_or_default();

            // Set model_id if the capability requires a model
            if let Some(model_id) = model_id {
                config["model_id"] = model_id.into();
            }

            let capability_config = crate::config::CapabilityConfig {
                capability_id: cap_type.clone(),
                capability_type: cap_type.clone(),
                config,
            };

            if ctx
                .config
                .insert_capability(cap_type, capability_config)
                .is_err()
            {
                ui.warn(&format!(
                    "Failed to save capability config for '{cap_type}'"
                ));
            }
        }

        // Enable every configured capability on each configured launcher
        // that supports it. Must run after both loops above, since it needs
        // a live `CapabilitySource` built from the now-populated capability
        // configs.
        let capability_source = crate::capabilities::CapabilitySource::from_config(&ctx.config);
        for launcher_id in selected_launchers {
            let Some(launcher_type) = ctx
                .config
                .get_launcher(launcher_id)
                .map(|l| l.launcher_type.clone())
            else {
                continue;
            };
            let Some(launcher_meta) = LAUNCHER_REGISTRY.get(&launcher_type) else {
                continue;
            };

            let mut enabled: Vec<String> = capability_source
                .instances()
                .into_iter()
                .filter(|(_, cap)| {
                    cap.binding_types()
                        .iter()
                        .any(|bt| launcher_meta.supported_capabilities.contains(bt))
                })
                .map(|(id, _)| id)
                .collect();
            enabled.sort();

            if ctx
                .config
                .update_launcher(launcher_id, |l| l.enabled_capabilities = enabled.clone())
                .is_err()
            {
                ui.warn(&format!(
                    "Failed to enable capabilities for launcher '{launcher_id}'"
                ));
            }
        }

        Ok(())
    }

    /// Picks a selected provider that can actually run `variant`, by
    /// constructing each one and checking `can_run_model`. Assumes providers
    /// have already been written to `ctx.config` (as `configure_all` does,
    /// before configuring models).
    fn find_compatible_provider(
        variant: &ModelVariant,
        selected_providers: &HashSet<String>,
        ctx: &crate::AppContext,
    ) -> Option<String> {
        selected_providers
            .iter()
            .find(|pid| {
                ctx.config
                    .get_provider(pid)
                    .and_then(|pc| {
                        PROVIDER_REGISTRY
                            .construct(&pc.provider_type, &pc.provider_id, &pc.config, &ctx.config)
                            .ok()
                    })
                    .is_some_and(|p| p.can_run_model(&variant.format, &variant.precision))
            })
            .cloned()
    }

    /// Find a model_id from selected_models that satisfies a capability's model
    /// requirement. Returns the first matching model or None if no match.
    fn find_model_for_capability(
        cap_type: &str,
        selected_models: &HashSet<String>,
    ) -> Option<String> {
        let cap_meta = CAPABILITY_REGISTRY.get(cap_type)?;

        let all_requirements: Vec<ModelRequirement> = cap_meta
            .dependencies
            .iter()
            .filter_map(|d| match d {
                Dependency::Model {
                    requirement,
                    resolved_id: None,
                    ..
                } => Some(requirement.clone()),
                _ => None,
            })
            .collect();

        if all_requirements.is_empty() {
            return None;
        }

        // Check each selected model's real catalog metadata to see if it
        // satisfies any requirement.
        selected_models
            .iter()
            .find(|model_id| {
                MODEL_REGISTRY.get(model_id).is_some_and(|md| {
                    all_requirements
                        .iter()
                        .any(|req| admits_for_recommendation(req, &md))
                })
            })
            .cloned()
    }

    /*-- Pull phase ----------------------------------------------------------*/

    async fn prompt_pull(
        ctx: &mut crate::AppContext,
        selected_models: &HashSet<String>,
    ) -> Result<()> {
        // Cloned (not `&*ctx.ui`) so it doesn't hold a borrow of `ctx` --
        // `ModelCommands::pull` below needs `&mut ctx` for the whole struct.
        let ui = ctx.ui.clone();

        // Find local provider models that were configured
        let pullable: Vec<_> = selected_models
            .iter()
            .filter_map(|model_id| {
                ctx.config.get_model(model_id).and_then(|mc| {
                    ctx.config.get_provider(mc.provider_id.as_str()).map(|pc| {
                        (
                            model_id.clone(),
                            mc.provider_id.clone(),
                            pc.provider_type.clone(),
                        )
                    })
                })
            })
            .collect();

        if pullable.is_empty() {
            alog_channel!(MessageLevel::Debug2, "No pullable models");
            return Ok(());
        }
        alog_channel!(MessageLevel::Debug2, "Pullable models: {:#?}", pullable);

        let items: Vec<String> = pullable
            .iter()
            .map(|(model, provider, ptype)| format!("{provider} → {model} ({ptype})"))
            .collect();

        let pull_now = ui.confirm("\n→ Pull model weights now?", !items.is_empty())?;

        if pull_now {
            for (model_id, _provider_id, _provider_type) in &pullable {
                ui.info(&format!("Pulling {model_id}..."));
                // `ModelCommands::pull` already reports success/failure to
                // `ctx.ui` itself; just keep going on error rather than
                // aborting the rest of the pull phase over one model.
                if let Err(e) = ModelCommands::pull(ctx, model_id).await {
                    alog_channel!(MessageLevel::Warning, "Pull failed for '{model_id}': {e}");
                }
            }
        }

        Ok(())
    }

    /*-- Summary phase -------------------------------------------------------*/

    fn print_summary(
        ctx: &crate::AppContext,
        selected_caps: &HashSet<String>,
        selected_launchers: &HashSet<String>,
        selected_providers: &HashSet<String>,
        selected_models: &HashSet<String>,
    ) {
        let ui = &*ctx.ui;

        ui.info("\n=== Setup Complete ===");
        ui.info(&format!(
            "Providers: {}",
            if selected_providers.is_empty() {
                "none".to_string()
            } else {
                selected_providers
                    .iter()
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        ));
        ui.info(&format!(
            "Models: {}",
            if selected_models.is_empty() {
                "none".to_string()
            } else {
                selected_models
                    .iter()
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        ));
        ui.info(&format!(
            "Launchers: {}",
            if selected_launchers.is_empty() {
                "none".to_string()
            } else {
                selected_launchers
                    .iter()
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        ));
        ui.info(&format!(
            "Capabilities: {}",
            if selected_caps.is_empty() {
                "none".to_string()
            } else {
                selected_caps.iter().cloned().collect::<Vec<_>>().join(", ")
            }
        ));

        ui.info("\nRun `granite-cli launcher list` to see configured launchers.");
        if !selected_launchers.is_empty() {
            let first_launcher = selected_launchers.iter().next().unwrap();
            ui.info(&format!(
                "Run `granite-cli launch {first_launcher}` to launch with Granite overlay.",
            ));
        }
    }
}

/*-- tests -------------------------------------------------------------------*/

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Config, ModelConfig, ProviderConfig};
    use crate::utils::ui::base::tests::CaptureUi;
    use std::sync::Arc;

    fn test_ctx() -> crate::AppContext {
        crate::AppContext {
            config: Config::default(),
            ui: Arc::new(CaptureUi::default()),
        }
    }

    /// A fixed hardware profile for discovery tests, in place of
    /// `detect_hardware()`'s result on whatever machine happens to run the
    /// test. Model-fit outcomes are a direct function of the hardware
    /// profile, so exercising the real detector here would make these tests
    /// pass or fail depending on the CI runner's actual RAM/VRAM rather than
    /// on the discovery logic under test.
    ///
    /// 32GB of usable memory (ram_gb / 2.0, no GPU) is calibrated against the
    /// real "Granite Language" 4.2 catalog entries: granite-4.2-8b's
    /// smallest GGUF variant fully fits at its full 131072-token context,
    /// while even granite-4.2-30b's smallest variant needs far more than
    /// that for full context and only ever partially fits.
    fn test_hardware_profile() -> HardwareProfile {
        HardwareProfile {
            os: "test".to_string(),
            cpu_cores: 8,
            cpu_arch: "test".to_string(),
            gpu_vendor: None,
            vram_gb: None,
            ram_gb: 64.0,
        }
    }

    async fn run_discovery(ctx: &crate::AppContext) -> DiscoveryResult {
        Discover::run_with_hardware(ctx, &test_hardware_profile()).await
    }

    fn ctx_with_provider(
        id: &str,
        provider_type: &str,
        config: serde_json::Value,
    ) -> crate::AppContext {
        let mut ctx = test_ctx();
        ctx.config.providers.insert(
            id.to_string(),
            ProviderConfig {
                provider_id: id.to_string(),
                provider_type: provider_type.to_string(),
                config,
            },
        );
        ctx
    }

    fn ctx_with_model(id: &str, provider_id: Option<&str>) -> crate::AppContext {
        let mut ctx = test_ctx();
        ctx.config.models.insert(
            id.to_string(),
            ModelConfig {
                model_id: id.to_string(),
                model_type: id.to_string(),
                config: serde_json::json!({}),
                provider_id: provider_id.unwrap_or("ollama").to_string(),
                variant: None,
            },
        );
        ctx
    }

    // -- discover_providers ----------------------------------------------------

    #[tokio::test]
    async fn discover_providers_skips_configured() {
        let ctx = ctx_with_provider("ollama", "ollama", serde_json::json!({}));
        let result = run_discovery(&ctx).await;
        assert!(
            result
                .configured_provider_ids
                .contains(&"ollama".to_string())
        );
    }

    #[tokio::test]
    async fn discover_providers_recommends_unconfigured() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;
        let provider_recs: Vec<_> = result
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Provider { .. }))
            .collect();
        // Should have at least some provider recommendations
        assert!(
            !provider_recs.is_empty(),
            "expected at least one provider recommendation"
        );
    }

    // -- discover_models -------------------------------------------------------

    #[tokio::test]
    async fn discover_models_groups_by_family() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;
        let model_recs: Vec<_> = result
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Model { .. }))
            .collect();
        // Should have at least one model recommendation
        assert!(
            !model_recs.is_empty(),
            "expected at least one model recommendation"
        );
    }

    #[tokio::test]
    async fn discover_models_recommendation_carries_real_registry_model_id() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;
        for rec in &result.recommendations {
            if let Recommendation::Model {
                model_id, family, ..
            } = rec
            {
                assert!(
                    MODEL_REGISTRY.get(model_id).is_some(),
                    "model_id '{model_id}' should be a real catalog key, not the family name"
                );
                assert_ne!(
                    model_id, family,
                    "model_id should be the specific model's catalog id, not its family"
                );
            }
        }
    }

    #[tokio::test]
    async fn discover_models_only_recommends_full_fit_and_picks_largest_full_fitting_size() {
        // Granite Language 4.2 ships three sizes (3b/8b/30b) at the same
        // version. The 30b only partially fits typical hardware -- it must
        // never be the default recommendation, and whichever size *is*
        // recommended must be a full fit.
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;

        let rec = result.recommendations.iter().find(
            |r| matches!(r, Recommendation::Model { family, .. } if family == "Granite Language"),
        );

        if let Some(Recommendation::Model {
            model_id,
            context_fit,
            ..
        }) = rec
        {
            assert_eq!(
                *context_fit,
                ContextFit::Full,
                "the default recommendation must fully fit, got {model_id} with {context_fit}"
            );
            assert_ne!(
                model_id, "granite-4.2-30b",
                "the 30b variant only partially fits on typical hardware and must not be auto-recommended"
            );
        }
        // If no size fully fits, no recommendation for the family is also
        // acceptable -- the important thing is a partial fit is never
        // silently promoted to the default recommendation.
    }

    #[tokio::test]
    async fn discover_all_model_candidates_includes_every_size_with_its_own_fit() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;

        let granite_4_2: Vec<_> = result
            .all_model_candidates
            .iter()
            .filter_map(|r| match r {
                Recommendation::Model {
                    model_id,
                    family,
                    version,
                    ..
                } if family == "Granite Language" && version == "4.2" => Some(model_id.as_str()),
                _ => None,
            })
            .collect();

        assert!(
            granite_4_2.contains(&"granite-4.2-8b"),
            "expected granite-4.2-8b among all-candidates, got {granite_4_2:?}"
        );
        assert!(
            granite_4_2.contains(&"granite-4.2-30b"),
            "the 30b size should still appear in the full candidate pool despite only partially fitting, got {granite_4_2:?}"
        );
    }

    #[tokio::test]
    async fn discover_models_skips_configured() {
        let ctx = ctx_with_model("granite-3.1-8b-instruct", Some("ollama"));
        let result = run_discovery(&ctx).await;
        assert!(
            result
                .configured_model_ids
                .contains(&"granite-3.1-8b-instruct".to_string())
        );
    }

    // -- discover_launchers ----------------------------------------------------

    #[tokio::test]
    async fn discover_launchers_skips_configured() {
        let mut ctx = test_ctx();
        ctx.config.launchers.insert(
            "claude".to_string(),
            crate::config::LauncherConfig {
                launcher_id: "claude".to_string(),
                launcher_type: "claude".to_string(),
                enabled_capabilities: vec![],
                config: serde_json::json!({}),
            },
        );
        let result = run_discovery(&ctx).await;
        assert!(
            result
                .configured_launcher_ids
                .contains(&"claude".to_string())
        );
    }

    #[tokio::test]
    async fn discover_launchers_recommends_unconfigured() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;
        let launcher_recs: Vec<_> = result
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Launcher { .. }))
            .collect();
        assert!(
            !launcher_recs.is_empty(),
            "expected at least one launcher recommendation"
        );
    }

    // -- discover_capabilities -------------------------------------------------

    #[tokio::test]
    async fn discover_capabilities_recommends_unconfigured() {
        let ctx = test_ctx();
        let result = run_discovery(&ctx).await;
        let cap_recs: Vec<_> = result
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Capability { .. }))
            .collect();
        assert!(
            !cap_recs.is_empty(),
            "expected at least one capability recommendation"
        );
    }

    // -- version comparison ----------------------------------------------------

    #[test]
    fn compare_versions_desc_simple() {
        assert_eq!(
            compare_versions_desc("3.1", "3.0"),
            std::cmp::Ordering::Less
        );
        assert_eq!(
            compare_versions_desc("3.0", "3.1"),
            std::cmp::Ordering::Greater
        );
        assert_eq!(
            compare_versions_desc("3.1", "3.1"),
            std::cmp::Ordering::Equal
        );
    }

    #[test]
    fn compare_versions_desc_multi_part() {
        assert_eq!(
            compare_versions_desc("3.1.1", "3.1.0"),
            std::cmp::Ordering::Less
        );
        assert_eq!(
            compare_versions_desc("3.1", "3.1.0"),
            std::cmp::Ordering::Greater
        );
    }

    #[test]
    fn compare_versions_desc_major_difference() {
        assert_eq!(
            compare_versions_desc("4.0", "3.1"),
            std::cmp::Ordering::Less
        );
    }

    #[test]
    fn find_latest_version_picks_the_highest_version_not_the_lowest() {
        // Regression test: `compare_versions_desc` is a reversed comparator
        // (by design, for descending display sorts), so `find_latest_version`
        // must pair it with `min_by`, not `max_by` -- using `max_by` silently
        // picked the *lowest* version in the family instead.
        fn md(version: &str) -> ModelMetadata {
            ModelMetadata {
                family: "Test Family".to_string(),
                version: version.to_string(),
                size: 0,
                context_length: 0,
                model_type: ModelType::Text,
                huggingface_repo: String::new(),
                native_dtype: String::new(),
                architecture: crate::models::ModelArchitecture {
                    num_hidden_layers: 0,
                    hidden_size: 0,
                    num_attention_heads: 0,
                    num_key_value_heads: 0,
                    head_dim: 0,
                    layer_types: vec![],
                },
                variants: vec![],
                description: None,
                tags: vec![],
                supported_functions: vec![],
            }
        }
        let models = vec![
            ("a".to_string(), md("4.0")),
            ("b".to_string(), md("3.1")),
            ("c".to_string(), md("4.1")),
            ("d".to_string(), md("3.3")),
        ];
        let (id, latest) = find_latest_version(&models).expect("non-empty input");
        assert_eq!(id, "c");
        assert_eq!(latest.version, "4.1");
    }

    // -- size helpers ----------------------------------------------------------

    #[test]
    fn format_size_billion() {
        assert_eq!(format_size(8_000_000_000), "8B");
    }

    #[test]
    fn format_size_million() {
        assert_eq!(format_size(2_000_000), "2M");
    }

    #[test]
    fn parse_size_billion() {
        assert_eq!(parse_size("8B"), 8_000_000_000);
    }

    #[test]
    fn parse_size_million() {
        assert_eq!(parse_size("2M"), 2_000_000);
    }

    // -- Revaluator ------------------------------------------------------------

    #[tokio::test]
    async fn revaluator_for_models_filters_by_capability_requirements() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        // agent-model requires Chat + ToolCalling support.
        let selected_caps: HashSet<String> = ["agent-model".to_string()].into_iter().collect();
        let filtered = Revaluator::for_models(&discovery.recommendations, &selected_caps);

        assert!(
            !filtered.is_empty(),
            "expected at least one granite model to satisfy agent-model's Chat+ToolCalling requirement"
        );
        // Every surviving recommendation's real catalog metadata must
        // actually admit the requirement -- this is the regression check for
        // the bug where a hand-rolled mock always reported empty
        // `supported_functions`, so nothing ever matched.
        for rec in &filtered {
            if let Recommendation::Model { model_id, .. } = rec {
                let md = MODEL_REGISTRY.get(model_id).expect("real catalog entry");
                assert!(
                    md.supported_functions
                        .contains(&crate::models::ModelFunction::Chat),
                    "{model_id} should support Chat"
                );
            }
        }
    }

    #[test]
    fn admits_for_recommendation_excludes_unrequested_multimodal_functions() {
        fn md_with_functions(functions: Vec<ModelFunction>) -> ModelMetadata {
            ModelMetadata {
                family: "Test".to_string(),
                version: "1.0".to_string(),
                size: 0,
                context_length: 8192,
                model_type: ModelType::Text,
                huggingface_repo: String::new(),
                native_dtype: String::new(),
                architecture: crate::models::ModelArchitecture {
                    num_hidden_layers: 0,
                    hidden_size: 0,
                    num_attention_heads: 0,
                    num_key_value_heads: 0,
                    head_dim: 0,
                    layer_types: vec![],
                },
                variants: vec![],
                description: None,
                tags: vec![],
                supported_functions: functions,
            }
        }

        let chat_only_req = ModelRequirement {
            supported_functions: vec![ModelFunction::Chat, ModelFunction::ToolCalling],
            ..Default::default()
        };
        let vision_req = ModelRequirement {
            supported_functions: vec![
                ModelFunction::Chat,
                ModelFunction::ToolCalling,
                ModelFunction::ImageUnderstanding,
            ],
            ..Default::default()
        };

        let text_model = md_with_functions(vec![ModelFunction::Chat, ModelFunction::ToolCalling]);
        let vision_model = md_with_functions(vec![
            ModelFunction::Chat,
            ModelFunction::ToolCalling,
            ModelFunction::ImageUnderstanding,
        ]);
        let speech_model =
            md_with_functions(vec![ModelFunction::Chat, ModelFunction::Transcription]);

        assert!(admits_for_recommendation(&chat_only_req, &text_model));
        assert!(
            !admits_for_recommendation(&chat_only_req, &vision_model),
            "a plain Chat/ToolCalling requirement should exclude a vision model even though it can chat"
        );
        assert!(
            !admits_for_recommendation(&chat_only_req, &speech_model),
            "a plain Chat/ToolCalling requirement should exclude a speech model even though it can chat"
        );
        // A requirement that explicitly wants vision should still admit it.
        assert!(admits_for_recommendation(&vision_req, &vision_model));
    }

    #[tokio::test]
    async fn revaluator_for_models_excludes_multimodal_for_plain_chat_capability() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        // agent-model only requires Chat + ToolCalling -- no multi-modal
        // function -- so vision/speech models must not show up even though
        // `all_model_candidates` (unlike the deduped default list) is
        // guaranteed to contain some.
        let selected_caps: HashSet<String> = ["agent-model".to_string()].into_iter().collect();
        let filtered = Revaluator::for_models(&discovery.all_model_candidates, &selected_caps);

        assert!(!filtered.is_empty());
        for rec in &filtered {
            if let Recommendation::Model { model_id, .. } = rec {
                let md = MODEL_REGISTRY.get(model_id).expect("real catalog entry");
                assert!(
                    !md.supported_functions
                        .iter()
                        .any(|f| MULTIMODAL_FUNCTIONS.contains(f)),
                    "{model_id} is multi-modal and should be excluded from a plain-chat capability's recommendations"
                );
            }
        }
    }

    #[tokio::test]
    async fn revaluator_for_models_still_includes_vision_for_vision_mcp() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        let selected_caps: HashSet<String> = ["vision-mcp".to_string()].into_iter().collect();
        let filtered = Revaluator::for_models(&discovery.all_model_candidates, &selected_caps);

        assert!(
            filtered.iter().any(|r| matches!(r, Recommendation::Model { model_id, .. } if model_id.contains("vision"))),
            "vision-mcp explicitly requires ImageUnderstanding, so vision models must still be recommended"
        );
    }

    #[tokio::test]
    async fn revaluator_for_models_with_no_requirements_returns_all() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;
        let filtered = Revaluator::for_models(&discovery.recommendations, &HashSet::new());
        let all_models: Vec<_> = discovery
            .recommendations
            .iter()
            .filter(|r| matches!(r, Recommendation::Model { .. }))
            .collect();
        assert_eq!(filtered.len(), all_models.len());
    }

    #[tokio::test]
    async fn revaluator_for_launchers_filters_by_binding_types() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        // agent-model needs the AgentModel binding, which bob does not support.
        let selected_caps: HashSet<String> = ["agent-model".to_string()].into_iter().collect();
        let filtered = Revaluator::for_launchers(&discovery, &selected_caps);
        assert!(
            !filtered
                .iter()
                .any(|r| matches!(r, Recommendation::Launcher { launcher_type, .. } if launcher_type == "bob")),
            "bob only supports Mcp and should be filtered out for agent-model"
        );

        // vision-mcp only needs the Mcp binding, which bob does support.
        let mcp_caps: HashSet<String> = ["vision-mcp".to_string()].into_iter().collect();
        let mcp_filtered = Revaluator::for_launchers(&discovery, &mcp_caps);
        assert!(
            mcp_filtered
                .iter()
                .any(|r| matches!(r, Recommendation::Launcher { launcher_type, .. } if launcher_type == "bob")),
            "bob supports Mcp and should be included for vision-mcp"
        );
    }

    #[tokio::test]
    async fn revaluator_for_capabilities_filters_by_selected_launchers() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        // bob only supports the Mcp binding, so only vision-mcp should show.
        let bob_only: HashSet<String> = ["bob".to_string()].into_iter().collect();
        let filtered = Revaluator::for_capabilities(&discovery, &bob_only);
        assert!(
            filtered
                .iter()
                .all(|r| matches!(r, Recommendation::Capability { capability_type, .. } if capability_type == "vision-mcp")),
            "with only bob (Mcp-only) selected, only vision-mcp should be recommended"
        );
    }

    #[tokio::test]
    async fn revaluator_for_capabilities_with_no_launchers_returns_none() {
        let ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;
        let filtered = Revaluator::for_capabilities(&discovery, &HashSet::new());
        assert!(filtered.is_empty());
    }

    // -- Variant selection -------------------------------------------------------

    fn model_recommendation(
        model_id: &str,
        md: &ModelMetadata,
        best_variant: ModelVariant,
    ) -> Recommendation {
        Recommendation::Model {
            model_id: model_id.to_string(),
            family: md.family.clone(),
            version: md.version.clone(),
            size: format_size(md.size),
            model_type: md.model_type.clone(),
            best_variant,
            context_fit: ContextFit::Full,
            can_run_by: vec![],
        }
    }

    #[test]
    fn candidate_variants_excludes_formats_no_selected_provider_supports() {
        let ctx = test_ctx();
        let md = MODEL_REGISTRY
            .get("granite-vision-4.1-4b")
            .expect("fixture model should exist in the catalog");
        let selected: HashSet<String> = ["lm-studio".to_string()].into_iter().collect();

        let candidates = SetupCommands::candidate_variants(&md, &selected, &ctx);

        assert!(
            !candidates.is_empty(),
            "lm-studio should be able to run at least one GGUF variant"
        );
        assert!(
            candidates
                .iter()
                .all(|(v, gb)| v.format.eq_ignore_ascii_case("gguf") && *gb > 0.0),
            "every candidate should be a GGUF variant with a positive VRAM estimate"
        );
        assert!(
            !candidates
                .iter()
                .any(|(v, _)| v.format.eq_ignore_ascii_case("safetensors")),
            "lm-studio cannot run safetensors, so it must not appear as a candidate"
        );
    }

    #[tokio::test]
    async fn select_variants_auto_selects_when_only_one_candidate_without_prompting() {
        let capture = Arc::new(CaptureUi::default());
        let mut ctx = crate::AppContext {
            config: Config::default(),
            ui: capture.clone(),
        };

        // granite-docling-258M-mlx has exactly one (safetensors) variant;
        // openai-compatible's default `can_run_model` accepts any format.
        let md = MODEL_REGISTRY
            .get("granite-docling-258M-mlx")
            .expect("fixture model should exist in the catalog");
        assert_eq!(
            md.variants.len(),
            1,
            "fixture assumption: exactly one variant"
        );

        let discovery = DiscoveryResult {
            recommendations: vec![model_recommendation(
                "granite-docling-258M-mlx",
                &md,
                md.variants[0].clone(),
            )],
            all_model_candidates: vec![],
            configured_provider_ids: vec![],
            configured_model_ids: vec![],
            configured_launcher_ids: vec![],
            configured_capability_ids: vec![],
        };
        let selected_models: HashSet<String> = ["granite-docling-258M-mlx".to_string()]
            .into_iter()
            .collect();
        let selected_providers: HashSet<String> =
            ["openai-compatible".to_string()].into_iter().collect();

        let chosen = SetupCommands::select_variants(
            &mut ctx,
            &discovery,
            &selected_models,
            &selected_providers,
        )
        .await
        .unwrap();

        let picked = chosen
            .get("granite-docling-258M-mlx")
            .expect("should have auto-selected the sole candidate");
        assert_eq!(picked.format, md.variants[0].format);
        assert_eq!(picked.precision, md.variants[0].precision);
        assert!(
            capture.select_prompts.borrow().is_empty(),
            "a model with only one compatible variant should not prompt"
        );
    }

    #[tokio::test]
    async fn select_variants_defaults_to_discoverys_best_variant() {
        let capture = Arc::new(CaptureUi::default());
        let mut ctx = crate::AppContext {
            config: Config::default(),
            ui: capture.clone(),
        };

        let md = MODEL_REGISTRY
            .get("granite-vision-4.1-4b")
            .expect("fixture model should exist in the catalog");
        let gguf_variants: Vec<ModelVariant> = md
            .variants
            .iter()
            .filter(|v| v.format.eq_ignore_ascii_case("gguf"))
            .cloned()
            .collect();
        assert!(
            gguf_variants.len() > 1,
            "fixture assumption: multiple GGUF variants, so a real choice is offered"
        );
        let recommended = gguf_variants[gguf_variants.len() / 2].clone();

        let discovery = DiscoveryResult {
            recommendations: vec![model_recommendation(
                "granite-vision-4.1-4b",
                &md,
                recommended.clone(),
            )],
            all_model_candidates: vec![],
            configured_provider_ids: vec![],
            configured_model_ids: vec![],
            configured_launcher_ids: vec![],
            configured_capability_ids: vec![],
        };
        let selected_models: HashSet<String> =
            ["granite-vision-4.1-4b".to_string()].into_iter().collect();
        let selected_providers: HashSet<String> = ["lm-studio".to_string()].into_iter().collect();

        // No canned select answer -- CaptureUi::select falls back to
        // whatever `default` it was passed, so this proves that default
        // index actually points at discovery's recommended variant.
        let chosen = SetupCommands::select_variants(
            &mut ctx,
            &discovery,
            &selected_models,
            &selected_providers,
        )
        .await
        .unwrap();

        let picked = chosen
            .get("granite-vision-4.1-4b")
            .expect("should have selected a variant");
        assert_eq!(picked.format, recommended.format);
        assert_eq!(picked.precision, recommended.precision);
        assert_eq!(capture.select_prompts.borrow().len(), 1);
    }

    #[tokio::test]
    async fn select_models_choose_different_models_surfaces_partial_fit_candidates() {
        let capture = Arc::new(CaptureUi::default());
        let mut ctx = crate::AppContext {
            config: Config::default(),
            ui: capture.clone(),
        };

        let discovery = run_discovery(&ctx).await;
        let selected_caps: HashSet<String> = ["agent-model".to_string()].into_iter().collect();

        // granite-4.2-30b only partially fits, so it must not be in the
        // default recommendation list, but must be reachable through
        // "choose different models".
        let default_ids: HashSet<&str> =
            Revaluator::for_models(&discovery.recommendations, &selected_caps)
                .into_iter()
                .filter_map(|r| match r {
                    Recommendation::Model { model_id, .. } => Some(model_id.as_str()),
                    _ => None,
                })
                .collect();
        assert!(
            !default_ids.contains("granite-4.2-30b"),
            "granite-4.2-30b only partially fits and must not be a default recommendation"
        );

        let manual_candidates =
            Revaluator::for_models(&discovery.all_model_candidates, &selected_caps);
        let thirty_b_idx = manual_candidates
            .iter()
            .position(|r| matches!(r, Recommendation::Model { model_id, .. } if model_id == "granite-4.2-30b"))
            .expect("granite-4.2-30b should be a manual candidate");

        // First multi_select (the default list): pick only the escape hatch
        // row (its index is the number of default items).
        let default_count = default_ids.len();
        capture
            .multi_select_answers
            .borrow_mut()
            .push_back(vec![default_count]);
        // Second multi_select (the manual list): pick granite-4.2-30b.
        capture
            .multi_select_answers
            .borrow_mut()
            .push_back(vec![thirty_b_idx]);

        let chosen = SetupCommands::select_models(&mut ctx, &discovery, &selected_caps)
            .await
            .unwrap();

        assert_eq!(
            chosen,
            HashSet::from(["granite-4.2-30b".to_string()]),
            "should have picked exactly the manually-chosen partial-fit model"
        );
    }

    // -- configure_all -----------------------------------------------------

    #[tokio::test]
    async fn configure_all_enables_only_capabilities_a_launcher_supports() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = test_ctx();
        let discovery = run_discovery(&ctx).await;

        // claude supports the AgentModel binding that "agent-model" uses;
        // bob supports only the Mcp binding, so it must not get it.
        let selected_caps: HashSet<String> = ["agent-model".to_string()].into_iter().collect();
        let selected_launchers: HashSet<String> = ["claude".to_string(), "bob".to_string()]
            .into_iter()
            .collect();
        let selected_providers: HashSet<String> = ["ollama".to_string()].into_iter().collect();
        let selected_models: HashSet<String> = ["granite-3.1-8b-instruct".to_string()]
            .into_iter()
            .collect();

        SetupCommands::configure_all(
            &mut ctx,
            &discovery,
            &selected_caps,
            &selected_launchers,
            &selected_providers,
            &selected_models,
            &HashMap::new(),
        )
        .await
        .unwrap();

        let claude_enabled = &ctx
            .config
            .get_launcher("claude")
            .unwrap()
            .enabled_capabilities;
        assert_eq!(
            claude_enabled,
            &vec!["agent-model".to_string()],
            "claude supports the AgentModel binding, so agent-model should be enabled"
        );

        let bob_enabled = &ctx.config.get_launcher("bob").unwrap().enabled_capabilities;
        assert!(
            bob_enabled.is_empty(),
            "bob only supports the Mcp binding, so agent-model must not be enabled"
        );
    }

    // -- SetupCommands ---------------------------------------------------------

    #[tokio::test]
    async fn prompt_pull_actually_invokes_model_commands_pull() {
        // Regression test: `prompt_pull` used to just log "Pulling
        // {model}..." and do nothing -- the pull was never actually
        // triggered. Point the configured provider at a closed local port
        // so the real pull attempt fails fast (no network dependency), and
        // assert on the error message that only `ModelCommands::pull`
        // itself produces, proving it was actually called rather than the
        // old no-op.
        let capture = Arc::new(CaptureUi::default());
        capture.confirm_answers.borrow_mut().push_back(true);
        let mut ctx = crate::AppContext {
            config: Config::default(),
            ui: capture.clone(),
        };

        ctx.config.providers.insert(
            "ollama".to_string(),
            ProviderConfig {
                provider_id: "ollama".to_string(),
                provider_type: "ollama".to_string(),
                config: serde_json::json!({ "base_url": "http://127.0.0.1:1" }),
            },
        );

        let model_id = "granite-3.1-8b-instruct";
        let md = MODEL_REGISTRY
            .get(model_id)
            .expect("fixture model should exist");
        let variant = md
            .variants
            .iter()
            .find(|v| v.format.eq_ignore_ascii_case("ollama"))
            .expect("fixture model should have an Ollama-format variant");
        ctx.config.models.insert(
            model_id.to_string(),
            ModelConfig {
                model_id: model_id.to_string(),
                model_type: model_id.to_string(),
                config: serde_json::json!({}),
                provider_id: "ollama".to_string(),
                variant: Some(format!("{}/{}", variant.format, variant.precision)),
            },
        );

        let selected_models: HashSet<String> = [model_id.to_string()].into_iter().collect();
        let result = SetupCommands::prompt_pull(&mut ctx, &selected_models).await;

        assert!(
            result.is_ok(),
            "prompt_pull should not propagate a per-model pull failure"
        );
        assert!(
            capture
                .errors
                .borrow()
                .iter()
                .any(|e| e.contains("Failed to pull model")),
            "expected ModelCommands::pull's own failure message, proving it was actually invoked; got: {:?}",
            capture.errors.borrow()
        );
    }

    #[tokio::test]
    async fn run_wizard_with_empty_config_shows_info() {
        let mut ctx = test_ctx();
        let _ = SetupCommands::run(&mut ctx, false, true).await;
        // Wizard should complete without error even with no recommendations
        // (it will show info messages)
    }

    #[tokio::test]
    async fn run_auto_with_no_recommendations_shows_info() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = test_ctx();
        let result = SetupCommands::run(&mut ctx, true, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn select_launchers_excludes_launchers_without_a_resolved_binary() {
        let capture = Arc::new(CaptureUi::default());
        let mut ctx = crate::AppContext {
            config: Config::default(),
            ui: capture.clone(),
        };

        let discovery = DiscoveryResult {
            recommendations: vec![
                Recommendation::Launcher {
                    launcher_type: "found".to_string(),
                    launcher_name: "Found Launcher".to_string(),
                    binary_path: Some("/usr/bin/found".to_string()),
                },
                Recommendation::Launcher {
                    launcher_type: "missing".to_string(),
                    launcher_name: "Missing Launcher".to_string(),
                    binary_path: None,
                },
            ],
            all_model_candidates: vec![],
            configured_provider_ids: vec![],
            configured_model_ids: vec![],
            configured_launcher_ids: vec![],
            configured_capability_ids: vec![],
        };

        SetupCommands::select_launchers(&mut ctx, &discovery)
            .await
            .unwrap();

        let prompts = capture.multi_select_prompts.borrow();
        let (_, items, _) = &prompts[0];
        assert_eq!(
            items.len(),
            1,
            "the missing binary should have been excluded"
        );
        assert!(items[0].contains("found"));
    }

    #[tokio::test]
    async fn find_compatible_provider_rejects_format_mismatch() {
        let mut ctx = test_ctx();
        ctx.config.providers.insert(
            "lm-studio".to_string(),
            crate::config::ProviderConfig {
                provider_id: "lm-studio".to_string(),
                provider_type: "lm-studio".to_string(),
                config: PROVIDER_REGISTRY
                    .default_config("lm-studio")
                    .unwrap_or_default(),
            },
        );
        let selected: HashSet<String> = ["lm-studio".to_string()].into_iter().collect();

        let gguf_variant = ModelVariant {
            format: "GGUF".to_string(),
            precision: "Q4_K_M".to_string(),
            size_gb: Some(4.0),
            url: "https://example.com/model.gguf".to_string(),
        };
        assert_eq!(
            SetupCommands::find_compatible_provider(&gguf_variant, &selected, &ctx),
            Some("lm-studio".to_string())
        );

        let safetensors_variant = ModelVariant {
            format: "safetensors".to_string(),
            precision: "bfloat16".to_string(),
            size_gb: Some(4.0),
            url: "https://example.com/model".to_string(),
        };
        assert_eq!(
            SetupCommands::find_compatible_provider(&safetensors_variant, &selected, &ctx),
            None,
            "lm-studio cannot serve safetensors and should not be picked"
        );
    }
}