llmman 0.1.689

Run any agent on any model, models stored as OCI images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
//! `llmman launch` — launch AI agent integrations backed by llmman serve.
//!
//! Mirrors `ollama launch`: sets integration-specific environment variables
//! pointing at the local inference server, then exec's the integration binary.
//!
//! `--provider` extends that to models llmman does not serve itself, from
//! the same models.dev catalog opencode resolves its providers from (see
//! [`crate::providers`]). It does not change the shape above: the
//! integration is still pointed at `llmman serve`, which forwards upstream
//! on its behalf. There is deliberately no path here that hands an
//! integration a provider's URL directly — one endpoint, one place
//! integrations are configured, whether or not the weights are local.
//!
//! `--overflow-provider`/`--overflow-model` hand the integration one
//! reference naming the local `--model` and a hosted one, and the daemon
//! picks a side per request (see [`crate::hybrid`]); the integration
//! never learns two are involved.

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

use anyhow::Context;
use clap::Args;

use crate::chat_template::{ThinkingControls, EFFORT_LEVELS};
use crate::daemon;
use crate::providers;

mod agy;
mod aider;
mod claude;
mod cline;
mod codex;
mod common;
mod copilot;
mod docker_agent;
mod droid;
mod dsh;
mod gemini;
mod goose;
mod goose_desktop;
mod grok;
mod hdrm;
mod hermes;
mod kimi;
mod omp;
mod openclaw;
mod opencode;
mod optimizer;
mod pi;
mod qwen;
mod sandbox;
mod vibe;

pub use optimizer::Optimizer;
pub use sandbox::Sandbox;

// ---------------------------------------------------------------------------
// CLI
// ---------------------------------------------------------------------------

#[derive(Args, Debug, Clone)]
pub struct LaunchArgs {
    /// Integration to launch (claude, opencode, codex, cline, aider, …)
    /// Omit to list available integrations.
    #[arg(value_name = "INTEGRATION")]
    pub integration: Option<String>,

    /// Model to use
    #[arg(long, short, value_name = "MODEL")]
    pub model: Option<String>,

    /// Serve --model from this provider (openai, anthropic, openrouter, …)
    /// instead of locally. Requires --model. See `llmman providers`.
    #[arg(long, short = 'p', value_name = "PROVIDER")]
    pub provider: Option<String>,

    /// Send requests too large for the local --model to this provider
    /// instead (openai, anthropic, openrouter, ...). Needs
    /// --overflow-model; not combinable with --provider.
    #[arg(long, value_name = "PROVIDER")]
    pub overflow_provider: Option<String>,

    /// The --overflow-provider model that serves requests too large for
    /// the local --model. Everything that fits stays on this machine.
    #[arg(long, value_name = "MODEL")]
    pub overflow_model: Option<String>,

    /// Run the integration in a sandbox, sbx unless one is named. See
    /// docs/sandbox.md.
    #[arg(
        long,
        value_enum,
        value_name = "SANDBOX",
        num_args = 0..=1,
        default_missing_value = "sbx"
    )]
    pub sandbox: Option<Sandbox>,

    /// Start at this variant of the model (see `llmman show`), as
    /// opencode's `run --variant`.
    #[arg(long, value_name = "VARIANT", value_parser = super::run::variant_parser())]
    pub variant: Option<String>,

    /// What to launch with to make the session cheaper: none (the default)
    /// launches as is; tk compacts the output of the shell commands the
    /// agent runs (claude, opencode); cvmn asks for terse replies (claude,
    /// codex, opencode); hdrm compresses what the agent sends the model
    /// (claude, codex, opencode). See docs/optimizers.md.
    #[arg(long, value_enum, value_name = "OPTIMIZER", default_value = "none")]
    pub optimizer: Optimizer,

    /// Extra arguments forwarded to the integration binary (after --)
    #[arg(last = true, value_name = "ARGS")]
    pub extra_args: Vec<String>,
}

pub fn run(args: &LaunchArgs) -> anyhow::Result<()> {
    let configured;
    let args = match args.integration.as_deref() {
        Some(name) => {
            let (with_defaults, applied) =
                apply_defaults(args, name, || crate::config::launch_defaults(name))?;
            if !applied.is_empty() {
                // So a refusal of a flag that was not typed makes sense.
                eprintln!(
                    "[llmman] {name}: llmman.conf supplies {}",
                    applied.join(" ")
                );
            }
            configured = with_defaults;
            &configured
        }
        None => args,
    };
    let provider = providers::provider_flag(args.provider.as_deref())?;
    let overflow = crate::hybrid::overflow_flags(
        args.overflow_provider.as_deref(),
        args.overflow_model.as_deref(),
        provider,
    )?;

    let Some(ref name) = args.integration else {
        print_integrations();
        return Ok(());
    };

    // Before the daemon starts. A forwarded --model would get the
    // variant checked against this one.
    let variant = args.variant.as_deref();
    if let Some(variant) = variant {
        anyhow::ensure!(
            args.sandbox != Some(Sandbox::Sbx),
            "--variant does not work with --sandbox sbx"
        );
        anyhow::ensure!(
            !has_forwarded_model_flag(name, &args.extra_args),
            "--variant needs the model as llmman's --model, not after --"
        );
        spell_variant(name, variant)?;
    }

    // Before sbx returns below, which would never reach it, and before the
    // daemon starts. An unknown name is left to the check that reports it.
    if let Some(id) = canonical_integration(name) {
        args.optimizer
            .check(id, args.sandbox.is_some(), &args.extra_args)?;
    }

    // sbx serves the model itself and validates its own flags, so none
    // of what follows applies.
    if args.sandbox == Some(Sandbox::Sbx) {
        return sandbox::run_sbx(
            name,
            args.model.as_deref(),
            provider,
            overflow,
            &args.extra_args,
        );
    }

    // Before either arm starts the daemon; see `check_model_flag`.
    let speech = std::env::var(goose_desktop::TRANSCRIPTION_MODEL_ENV).ok();
    check_model_flag(
        name,
        args.model.as_deref(),
        provider,
        speech.as_deref(),
        &args.extra_args,
    )?;
    if name.eq_ignore_ascii_case("copilot")
        || name.eq_ignore_ascii_case("copilot-cli")
        || name.eq_ignore_ascii_case("droid")
    {
        common::check_daemon_key_transport(name)?;
    }
    anyhow::ensure!(
        overflow.is_none() || args.model.as_deref().is_some_and(|m| !m.trim().is_empty()),
        "--overflow-model needs --model naming the local model to pair it with"
    );
    // Before the Cline install check, which an image-based sandbox makes
    // moot (see `find_on_path`), and before the daemon starts.
    if let Some(kind) = args.sandbox {
        let id = name.to_lowercase();
        let carries_key =
            provider.is_some() || overflow.is_some() || crate::auth::client_key().is_some();
        // A --variant goes into opencode's state (`write_opencode_variant`).
        let configured_by_file =
            CONFIGURED_BY_FILE.contains(&id.as_str()) || (id == "opencode" && variant.is_some());
        sandbox::prepare(
            kind,
            &id,
            sandbox_state(&id)?,
            configured_by_file,
            carries_key,
        )?;
    }
    // Cline follows the install-on-demand behavior expected by its launch
    // integration. Do this before starting the daemon or pulling a model.
    if name.eq_ignore_ascii_case("cline") {
        cline::ensure_cline_installed()?;
    }
    // Rejects the arguments after `--` that docker-agent cannot be
    // launched with. Here rather than in the launcher so the refusal
    // comes before the daemon starts and a model is pulled.
    if name.eq_ignore_ascii_case("docker-agent") {
        docker_agent::check_docker_agent_args(&args.extra_args)?;
    }
    // Under `--provider` or an overflow pair, kimi is refused further on.
    if name.eq_ignore_ascii_case("kimi") && provider.is_none() && overflow.is_none() {
        kimi::check_kimi_args(args.model.as_deref(), &args.extra_args)?;
    }
    // Each launcher looks for its program only once it runs, which is
    // after the daemon has started and the model has been pulled: a
    // missing agent would otherwise cost a download first.
    check_installed(name)?;
    // The model's thinking choices (see `opencode_variants`), from its
    // template or the catalog; whether it takes images (see
    // `dsh::write_dsh_settings`) and the trained context only a local model
    // has, which `local_context_window` falls back on.
    let mut thinking = None;
    let mut vision = false;
    let mut context_length = None;
    // The window the daemon serves, for the integrations that declare
    // one: the local model's, the hosted model's, or a pair's larger
    // half, set by whichever arm below resolves the model. `None` when
    // no side of it is known. See `launch`.
    let context_window: Option<u64>;
    // The catalog's reply ceiling, for the arms that have a catalog.
    let mut max_output = None;
    let (model, api_key) = match provider {
        Some(provider) => {
            check_provider_supported(name)?;
            // The daemon first, before --provider is validated: the
            // catalog belongs to `llmman serve` (see cmd::providers), so
            // there is nothing to validate against until it runs. Nothing
            // to preload either — a provider-routed model has nothing
            // local to warm up — but it still has to be running, since it
            // is what forwards upstream.
            crate::daemon::ensure_server("")?;
            let per_request = !PROVIDER_NEEDS_DAEMON_KEY.contains(&name.to_lowercase().as_str());
            let hosted =
                resolve_provider_model(provider, args.model.as_deref(), name, per_request)?;
            thinking = hosted.thinking.map(Thinking::Listed);
            // Nothing local is loaded here, so the catalog's limits are
            // the only ones, and the ones the provider enforces.
            context_window = hosted.context_window;
            max_output = hosted.max_output;
            (hosted.reference, hosted.api_key)
        }
        None => {
            // resolve_ollama_api, not resolve: every integration this
            // launches talks to serve's Ollama/OpenAI/Anthropic-compat
            // surfaces, all of which resolve model names the same way
            // (see ensure_model in cmd::serve), so a bare name here must
            // match what the daemon resolves it to at request time.
            // Fallible: it validates the raw reference first (see
            // shortnames::validate_reference).
            let model = args
                .model
                .as_deref()
                .map(crate::shortnames::resolve_ollama_api)
                .transpose()?
                .unwrap_or_default();

            // Ensure serve is running (start it in background if needed),
            // preloading the requested model so the integration's first
            // request finds it warm.
            crate::daemon::ensure_server(&model)?;

            // serve's preload above is fire-and-forget and only fires on
            // a cold `serve` start (see run() in cmd/serve.rs) — if the
            // daemon was already running from a previous invocation, a
            // missing model would otherwise only surface as an opaque
            // failure once the integration made its first request. Mirror
            // `llmman run`'s behavior and pull it here instead,
            // synchronously and with progress, before ever handing off to
            // the integration.
            if !model.is_empty() {
                let info = crate::daemon::ensure_model_pulled(&model)?;
                thinking = info.thinking_controls().map(Thinking::Template);
                vision = info.vision();
                context_length = info.context_length();
            }
            // Resolved once, and only for an integration that has
            // somewhere to put it: reading the live window loads the
            // model (see `local_context_window`), which every other
            // launch would wait for and never use. That includes claude
            // given another model, which drops the window.
            let local_window = (declares_context_window(name)
                && !model.is_empty()
                && (!name.eq_ignore_ascii_case("claude")
                    || claude::runs_model(&model, &args.extra_args)))
            .then(|| local_context_window(&model, context_length))
            .flatten();
            match overflow {
                // The hosted half is validated and keyed exactly as a
                // bare --provider model would be, then paired with the
                // local model just pulled.
                Some((provider, hosted)) => {
                    check_provider_supported(name)?;
                    let per_request =
                        !PROVIDER_NEEDS_DAEMON_KEY.contains(&name.to_lowercase().as_str());
                    // The local half, which serves by default, keeps
                    // its thinking choices.
                    let remote = resolve_provider_model(provider, Some(hosted), name, per_request)?;
                    context_window = pair_context_window(local_window, remote.context_window);
                    // The pair's ceiling is the hosted half's: a local
                    // backend caps no reply of its own.
                    max_output = remote.max_output;
                    (
                        crate::hybrid::pair_with_local(&model, &remote.reference)?,
                        remote.api_key,
                    )
                }
                None => {
                    // A `--provider` model is served by someone else, so
                    // this is the only arm whose window is the local one
                    // alone.
                    context_window = local_window;
                    (model, integration_key())
                }
            }
        }
    };

    let launched = launch(
        name,
        &model,
        &api_key,
        thinking.as_ref(),
        variant,
        vision,
        context_window,
        max_output,
        &args.extra_args,
        args.optimizer,
    );
    hdrm::stop();
    launched
}

/// `args` with `[launch.<name>]` from llmman.conf filled in for the flags
/// left out, and those fills as the flags they stand for.
///
/// Typing `--provider`, `--model` or an overflow flag chooses a model, so
/// none of the configured provider, model or variant applies: the
/// provider would route the typed model to the wrong host, and the
/// variant belongs to the old one. `--variant` alone keeps the configured
/// model. A `--model` after `--` wins over the configured one, so the
/// configured variant is dropped too. `load` runs only when needed.
fn apply_defaults(
    args: &LaunchArgs,
    name: &str,
    load: impl FnOnce() -> Result<crate::config::LaunchConf, &'static str>,
) -> anyhow::Result<(LaunchArgs, Vec<String>)> {
    let mut effective = args.clone();
    let mut applied = Vec::new();
    if args.provider.is_some()
        || args.model.is_some()
        || args.overflow_provider.is_some()
        || args.overflow_model.is_some()
    {
        return Ok((effective, applied));
    }
    let mut defaults = load().map_err(|e| anyhow::anyhow!("cannot read launch defaults: {e}"))?;
    if has_forwarded_model_flag(name, &args.extra_args) {
        defaults.variant = None;
    }
    for (slot, configured, flag) in [
        (&mut effective.provider, defaults.provider, "--provider"),
        (&mut effective.model, defaults.model, "--model"),
        (&mut effective.variant, defaults.variant, "--variant"),
    ] {
        if let (None, Some(value)) = (&*slot, configured) {
            applied.push(format!("{flag} {value}"));
            *slot = Some(value);
        }
    }
    Ok((effective, applied))
}

/// What an integration authenticates with when no provider key travels:
/// the daemon's key when this shell has one, else the placeholder that
/// tells serve the header is not a credential.
fn integration_key() -> String {
    crate::auth::client_key().unwrap_or_else(|| providers::PLACEHOLDER_API_KEY.to_string())
}

// ---------------------------------------------------------------------------
// Pre-flight
// ---------------------------------------------------------------------------

/// Integrations that cannot be launched without `--model`. Qwen Code has
/// no notion of a missing model and sends its own built-in default
/// (`qwen3.7-max` in 0.22.3), which the daemon would then try to pull.
/// AGY needs an explicit model for its Gemini routing URL.
/// dsh has no default of its own either — an empty `--model` would
/// otherwise land a literal `"default"` in `agent-default-model.model`,
/// which the first request then tries to resolve as a real model id.
/// goose instead refuses with "Run 'goose configure' first", advice that
/// does not apply to a launch llmman configures through the environment.
/// goose-desktop keeps the same environment, so llmman still owns the
/// provider and endpoint; only the model name would fall back to
/// `goose configure`'s, and the daemon would be asked for that one.
/// It alone may skip `--model`, for dictation: see `check_model_flag`.
/// Grok Build and Cline have hosted defaults of their own; without an
/// explicit local model they would send those ids to llmman's endpoint
/// instead.
/// docker-agent's "auto" selection takes the first cloud provider with a
/// credential, then a local Docker Model Runner model, so without an
/// explicit model the request never reaches llmman at all.
/// OMP needs a model so the launcher can select the matching entry its
/// Ollama discovery reads from llmman's `/api/tags`.
/// Copilot's BYOK mode also requires an explicit model.
/// Droid needs one to create and select its custom-model entry.
/// Checked before `ensure_server`, so the refusal costs no daemon start.
const MODEL_REQUIRED: &[&str] = &[
    "qwen",
    "dsh",
    "agy",
    "goose",
    "goose-desktop",
    "grok",
    "cline",
    "pi",
    "omp",
    "copilot",
    "copilot-cli",
    "droid",
    "docker-agent",
];

/// Integrations whose launcher yields to a `--model` after `--`, and so
/// warrant the warning below. qwen: `qwen::qwen_args` drops its own `--model`
/// when the caller spelled one. goose: its model is `GOOSE_MODEL` in the
/// environment, which goose's own `--model` documents itself as
/// overriding. grok: its argument builder likewise drops the generated
/// `--model`. Cline receives no generated arguments, so its own `--model`
/// also wins over the provider selected in its settings.
/// OMP's argument builder drops the generated Ollama model when the caller
/// supplies one.
/// Copilot's argument builder likewise yields to its own `--model`.
/// Droid's `exec` argument builder does the same for its custom model id;
/// interactive Droid reads the generated session default instead.
/// claude: `claude::claude_args` likewise drops the generated `--model`.
/// Not docker-agent: its `--model` replaces the whole model entry
/// `docker_agent::docker_agent_document` wrote, `base_url` included, so
/// a forwarded one reaches api.openai.com.
/// `docker_agent::check_docker_agent_args` refuses it rather than
/// letting it "win".
/// Not dsh: `dsh::dsh_args` does not yield, and dsh takes no
/// `--model` flag at all (its model is the one `dsh::write_dsh_settings`
/// records), so telling a dsh user theirs "wins" would be false, and dsh
/// rejects the unknown flag on its own.
/// Not goose-desktop: the `--model` goose documents is `goose run`/
/// `goose session`'s, and the desktop app has no documented equivalent,
/// so promising the caller's wins would be false. Move it here if it
/// turns out to take one.
const MODEL_FLAG_FORWARDED: &[&str] = &[
    "claude",
    "qwen",
    "goose",
    "grok",
    "cline",
    "omp",
    "copilot",
    "copilot-cli",
    "droid",
];

/// Refuses a launch of one of `MODEL_REQUIRED` without a model, under
/// `--provider` too. A second `--model` after `--` is the caller's to
/// win for the integrations in `MODEL_FLAG_FORWARDED`, but `run`
/// resolves the top-level one and, locally, preloads it, so that gets
/// said.
///
/// goose-desktop alone may skip `--model` when `speech_model`
/// (`OPENAI_TRANSCRIPTION_MODEL`) is set: it then launches for dictation
/// only. `--provider` still needs `--model`.
fn check_model_flag(
    integration: &str,
    model: Option<&str>,
    provider: Option<&str>,
    speech_model: Option<&str>,
    extra_args: &[String],
) -> anyhow::Result<()> {
    let name = integration.to_lowercase();
    let model = model.map(str::trim).filter(|m| !m.is_empty());
    let Some(model) = model else {
        if MODEL_REQUIRED.contains(&name.as_str()) {
            let dictation = name == "goose-desktop" && provider.is_none();
            if dictation && speech_model.is_some_and(|s| !s.trim().is_empty()) {
                return Ok(());
            }
            let with_provider = provider.map_or(String::new(), |p| format!(" --provider {p}"));
            let or_dictate = if dictation {
                format!(
                    ", or set {}=<speech model> for dictation only",
                    goose_desktop::TRANSCRIPTION_MODEL_ENV
                )
            } else {
                String::new()
            };
            anyhow::bail!(
                "{name} needs a model: llmman launch {name}{with_provider} --model <model>{or_dictate}"
            );
        }
        // No resolved model for a forwarded one to win over.
        return Ok(());
    };
    if has_forwarded_model_flag(&name, extra_args) {
        eprintln!(
            "[llmman] {name}: the --model after -- wins over --model {model}, the one llmman resolved"
        );
    }
    Ok(())
}

/// Whether this integration accepts a forwarded model flag. Copilot and
/// claude spell it only `--model`; the other launchers also accept `-m`.
fn has_forwarded_model_flag(integration: &str, extra_args: &[String]) -> bool {
    let name = integration.to_lowercase();
    let short = (!matches!(name.as_str(), "copilot" | "copilot-cli" | "claude")).then_some("-m");
    MODEL_FLAG_FORWARDED.contains(&name.as_str())
        && has_flag(before_terminator(extra_args), "--model", short)
}

/// `extra_args` up to a forwarded `--`. The launched CLI takes everything
/// after one as arguments, so a prompt there that reads like a flag is a
/// message, not a flag.
fn before_terminator(extra_args: &[String]) -> &[String] {
    let end = extra_args
        .iter()
        .position(|arg| arg == "--")
        .unwrap_or(extra_args.len());
    &extra_args[..end]
}

/// Whether `extra_args` spells `long` or `short`, as a word or `=`-joined.
fn has_flag(extra_args: &[String], long: &str, short: Option<&str>) -> bool {
    extra_args.iter().any(|a| {
        a == long
            || a.starts_with(&format!("{long}="))
            || short.is_some_and(|s| a == s || a.starts_with(&format!("{s}=")))
    })
}

// ---------------------------------------------------------------------------
// Providers
// ---------------------------------------------------------------------------

/// Integrations `--provider` cannot drive, and why.
///
/// Refusing is the same call the catalog filter makes — a
/// combination llmman cannot actually drive is absent, not offered and
/// then broken at the first request.
const PROVIDER_UNSUPPORTED: &[(&str, &str)] = &[
    (
        "kimi",
        "llmman has not verified it with a provider-routed model",
    ),
    // Its key variable feeds a native Google client, and llmman has not
    // verified that GEMINI_BASE_URL still redirects it here. Getting that
    // wrong sends someone's OpenRouter key to Google, which is a worse
    // outcome than `--provider gemini` not working — the placeholder this
    // used to pass was harmless either way, a real key is not.
    (
        "gemini",
        "llmman cannot confirm it would send the key here rather than to Google",
    ),
    // openclaw::launch_openclaw passes --custom-model-id only through
    // onboarding, which runs once. Every later launch reuses whatever
    // openclaw.json already names, so the provider reference would never
    // reach the daemon and the session would quietly run on the old model.
    (
        "openclaw",
        "it only takes a model during first-run onboarding",
    ),
    // Grok Build refuses `-m` values absent from the custom endpoint's
    // `/v1/models` catalog. llmman's endpoint lists stored local models,
    // not the synthetic provider or hybrid routing refs, so one of those
    // launches would fail before making its first request.
    (
        "grok",
        "its model catalog cannot represent llmman's provider or hybrid routing reference",
    ),
    // OMP discovers the Ollama catalog from /api/tags. That endpoint lists
    // stored models, not the synthetic provider or hybrid routing refs the
    // daemon accepts at request time, and OMP rejects a model absent from the
    // catalog before it sends a request.
    (
        "omp",
        "its Ollama catalog cannot represent llmman's provider or hybrid routing reference",
    ),
];

/// Integrations llmman configures through a file on disk. They take a
/// model on every launch, so `--provider` works, but they cannot carry
/// the key: writing a real one into `~/.hermes/config.yaml` would persist
/// a credential, which this feature promises not to do. They rely on
/// `llmman serve` having the variable itself — which it only uses for a
/// daemon nobody else can reach (see `reachable_only_locally`).
const PROVIDER_NEEDS_DAEMON_KEY: &[&str] = &["hermes", "cline", "pi"];

/// Integrations that declare a context window, and so are worth
/// loading the model to read the real one back for. Keep in step with
/// [`launch`]'s own dispatch: an integration missing here is simply
/// told no window, not told a wrong one.
fn declares_context_window(integration: &str) -> bool {
    matches!(
        integration.to_lowercase().as_str(),
        "claude" | "opencode" | "codex" | "pi" | "omp" | "kimi" | "openclaw" | "vibe"
    )
}

fn check_provider_supported(integration: &str) -> anyhow::Result<()> {
    let name = integration.to_lowercase();
    if let Some((_, why)) = PROVIDER_UNSUPPORTED.iter().find(|(id, _)| *id == name) {
        anyhow::bail!(
            "--provider does not work with {name}: {why}\n\
             Run it against a locally served model, or use another integration."
        );
    }
    // The key would go to the integration in cleartext, and from there
    // over plain http to a daemon somewhere else on the network. llmman
    // controls neither hop, so it does not start the handoff. A wildcard
    // bind is fine here — that hop is still loopback — and so is TLS.
    if !crate::daemon::connects_securely() {
        anyhow::bail!(
            "--provider needs a local llmman serve, or one over TLS: LLMMAN_HOST points at {}, \
             and the provider key would cross the network in cleartext.\n\
             Export the key where that daemon runs instead.",
            crate::daemon::server()
        );
    }
    // These reach the daemon over loopback, so the check above passes,
    // but they send the placeholder key and the daemon will not fall back
    // to its own on a bind anyone can reach — unless it authenticates
    // callers, in which case they send its key and it will. Say so here
    // rather than let it surface as a 401 from inside the integration.
    if PROVIDER_NEEDS_DAEMON_KEY.contains(&name.as_str())
        && !crate::daemon::reachable_only_locally()
        && crate::auth::client_key().is_none()
    {
        anyhow::bail!(
            "--provider does not work with {name} while llmman serve is bound to {}: \
             {name} is configured through a file, so it cannot send the key per request, \
             and a daemon reachable from the network will not spend its own.\n\
             Bind llmman serve to loopback, or use an integration that carries the key.",
            crate::daemon::bind_addr()
        );
    }
    Ok(())
}

/// What [`resolve_provider_model`] resolves. Named fields rather than a
/// tuple: three of these are `Option`s, which a caller can drop by
/// destructuring past them without the compiler saying so.
struct ResolvedProvider {
    /// The hosted model's full reference, as the daemon routes on it.
    reference: String,
    /// The key the integration authenticates with.
    api_key: String,
    /// The thinking levels the catalog lists (see [`Thinking::Listed`]).
    thinking: Option<Vec<String>>,
    /// The window the catalog says this model holds.
    context_window: Option<u64>,
    /// The most the catalog says it will emit in one reply (see
    /// [`crate::daemon::ProviderDetail::max_output`]).
    max_output: Option<u32>,
}

/// Validates `--provider`/`--model` against the running daemon's catalog
/// (see [`crate::daemon::provider`]), resolving the reference the daemon
/// routes on, the key `integration` authenticates with, and what the
/// catalog says about the model — see [`ResolvedProvider`].
///
/// `key_travels_per_request` is false for the integrations in
/// [`PROVIDER_NEEDS_DAEMON_KEY`], which get the placeholder because they
/// cannot carry a real key — so this shell having one is beside the
/// point, and demanding it would reject a perfectly good daemon that has
/// it while this shell does not.
///
/// Every check here is one the daemon would otherwise make at first
/// request, by which point the integration has already taken over the
/// terminal and reports whatever it makes of an HTTP error. Failing in
/// llmman's own output, before the handoff, is the difference between a
/// named missing environment variable and an opaque "connection error"
/// inside someone else's TUI.
fn resolve_provider_model(
    provider: &str,
    model: Option<&str>,
    integration: &str,
    key_travels_per_request: bool,
) -> anyhow::Result<ResolvedProvider> {
    // Asked of the daemon, not models.dev: it routes the request, so it
    // is the authority on whether this provider exists — and on whether
    // *it* has the key, which this shell cannot see.
    let model = model.map(str::trim).filter(|m| !m.is_empty());
    // Sent so a catalog that predates the model is refreshed, not warned about.
    let entry = daemon::provider(provider, model)?;

    let model = model.ok_or_else(|| {
        anyhow::anyhow!(
            "--provider {provider} also needs --model\n\n{}",
            providers::example_models(&entry.name, &entry.model_ids())
        )
    })?;

    entry.warn_unlisted(model);

    // Read here, not left to the daemon, so a missing key names the
    // variable to set in llmman's own output. It travels per request in
    // the integration's own Authorization header (see client_api_key in
    // cmd::serve), never to disk or a command line. The placeholder goes
    // instead whenever the daemon's key is the one that matters — an
    // integration that cannot carry one, or a shell without one where
    // the daemon has it — or when the provider takes none at all
    // (`key_optional`): it is what tells serve the header is not a
    // credential.
    //
    // A daemon requiring a key takes that header for it, so the provider
    // key cannot travel: the daemon's is the only one, and an
    // authenticated caller may spend it.
    let daemon_authenticates = crate::auth::client_key().is_some();
    let key = if key_travels_per_request && !daemon_authenticates {
        entry.client_key()
    } else {
        None
    };
    let key = match (key, key_travels_per_request) {
        (Some(key), true) => key,
        (_, false) => {
            // Fatal, not a warning: this integration cannot carry a key,
            // so the daemon's is the only one its first request can use,
            // and `key_usable` is the daemon's own word on whether it
            // would spend it. Warning and handing off would surface as a
            // 401 inside someone else's TUI.
            anyhow::ensure!(
                entry.key_usable || entry.key_optional,
                "{integration} is configured through a file, so it cannot send an API key: \
                 llmman serve needs a key of its own, and must be bound to loopback (or \
                 require an API key) to spend it.\n\
                 Where the daemon runs, {}, then restart it.",
                entry.key_hint()
            );
            integration_key()
        }
        (None, true) if entry.daemon_key_usable() => {
            if !daemon_authenticates {
                eprintln!(
                    "[llmman] warning: no API key for {} here; using the key llmman serve has",
                    entry.name
                );
            }
            integration_key()
        }
        (None, true) if entry.key_optional => integration_key(),
        (None, true) if daemon_authenticates => anyhow::bail!(
            "llmman serve requires an API key, so {integration} sends that one and cannot \
             also carry a key for {}: llmman serve needs a key of its own.\n\
             Where the daemon runs, {}, then restart it.",
            entry.name,
            entry.key_hint()
        ),
        (None, true) => anyhow::bail!("no API key for {} — {}", entry.name, entry.key_hint()),
    };

    Ok(ResolvedProvider {
        reference: providers::format_remote_ref(provider, model),
        api_key: key,
        thinking: entry.thinking_levels(model),
        context_window: entry.context_window(model),
        max_output: entry.max_output(model),
    })
}

// ---------------------------------------------------------------------------
// Integration registry
// ---------------------------------------------------------------------------

struct Integration {
    name: &'static str,
    description: &'static str,
    binary: &'static str,
}

const INTEGRATIONS: &[Integration] = &[
    Integration {
        name: "claude",
        description: "Claude Code",
        binary: "claude",
    },
    Integration {
        name: "opencode",
        description: "OpenCode",
        binary: "opencode",
    },
    Integration {
        name: "codex",
        description: "OpenAI Codex CLI",
        binary: "codex",
    },
    Integration {
        name: "pi",
        description: "Pi coding agent",
        binary: "pi",
    },
    Integration {
        name: "omp",
        description: "OMP coding agent",
        binary: "omp",
    },
    Integration {
        name: "cline",
        description: "Cline",
        binary: "cline",
    },
    Integration {
        name: "aider",
        description: "Aider AI pair programmer",
        binary: "aider",
    },
    Integration {
        name: "copilot",
        description: "GitHub Copilot CLI",
        binary: "copilot",
    },
    Integration {
        name: "droid",
        description: "Factory Droid CLI",
        binary: "droid",
    },
    Integration {
        name: "kimi",
        description: "Kimi Code CLI",
        binary: "kimi",
    },
    Integration {
        name: "gemini",
        description: "Gemini CLI",
        binary: "gemini",
    },
    Integration {
        name: "agy",
        description: "Google Antigravity CLI",
        binary: "agy",
    },
    Integration {
        name: "hermes",
        description: "Hermes Agent",
        binary: "hermes",
    },
    Integration {
        name: "openclaw",
        description: "OpenClaw",
        binary: "openclaw",
    },
    Integration {
        name: "qwen",
        description: "Qwen Code",
        binary: "qwen",
    },
    Integration {
        name: "dsh",
        description: "DeepSeek Harness",
        binary: "dsh",
    },
    Integration {
        name: "goose",
        description: "Block goose",
        binary: "goose",
    },
    Integration {
        name: "goose-desktop",
        description: "Block goose Desktop",
        binary: "goose-desktop",
    },
    Integration {
        name: "grok",
        description: "Grok Build",
        binary: "grok",
    },
    Integration {
        name: "docker-agent",
        description: "Docker Agent",
        binary: "docker-agent",
    },
    Integration {
        name: "vibe",
        description: "Mistral Vibe CLI",
        binary: "vibe",
    },
];

/// The registered name for a launch name (any case, or an alias): what
/// `[launch.<name>]` in llmman.conf is matched on.
pub(crate) fn canonical_integration(name: &str) -> Option<&'static str> {
    integration(name).map(|i| i.name)
}

/// The registry entry for a launch name, after resolving supported aliases.
fn integration(name: &str) -> Option<&'static Integration> {
    let name = name.to_ascii_lowercase();
    let canonical = match name.as_str() {
        "copilot-cli" => "copilot",
        other => other,
    };
    INTEGRATIONS.iter().find(|i| i.name == canonical)
}

fn print_integrations() {
    println!("Available integrations:\n");
    for i in INTEGRATIONS {
        // dsh resolves to npx when it isn't installed (see `dsh::find_dsh`),
        // and that launch downloads the package before running it.
        let how = match find_integration_binary(i) {
            Some(bin) if bin.file_stem().is_some_and(|s| s == "npx") => " (via npx)",
            Some(_) => "",
            None => " (not installed)",
        };
        println!("  {:<14} {}{}", i.name, i.description, how);
    }
    println!(
        "\nUsage: llmman launch <integration> [--model <model>] [--provider <provider>] [--sandbox [sandbox]]"
    );
    println!("       llmman providers   (the providers --provider accepts)");
}

/// Extensions to try, in order, when resolving a bare command name on
/// Windows — where, unlike everywhere else, a name on `PATH` almost never
/// exists as a bare file: it's always some extension's worth of shim/
/// executable, and which one varies by how it got installed. `.exe` is a
/// real native binary; `.cmd`/`.bat` is what `npm install -g` always
/// generates for a JS-based CLI's bin entry (every integration this
/// module launches — claude, opencode, codex — is installed exactly that
/// way), alongside a `.ps1` this intentionally skips: unlike `.exe`/
/// `.cmd`/`.bat`, Windows' `CreateProcess` (and so `std::process::Command`
/// under it) can't launch a `.ps1` directly at all without an explicit
/// `powershell -File` wrapper, and every npm install already writes a
/// `.cmd` alongside it, so there's no case where only the `.ps1` exists.
const WINDOWS_PATH_EXTS: &[&str] = &["exe", "cmd", "bat"];

/// Under an image-based `--sandbox` the image's copy runs, found on the
/// image's `PATH`, so this machine need not have one.
fn find_on_path(binary: &str) -> Option<PathBuf> {
    find_on_path_unless(binary, |_| false)
}

/// [`find_on_path`], skipping matches `reject` returns true for and
/// carrying on down `PATH` rather than stopping at the first. No
/// platform tells goose's two binaries apart by name — the desktop zip's
/// is `Goose.exe` on Windows, `goose` elsewhere — so each has to refuse
/// the other, and the one wanted may sit in a later directory.
fn find_on_path_unless(binary: &str, reject: impl Fn(&Path) -> bool) -> Option<PathBuf> {
    if sandbox::runs_from_image() {
        return Some(PathBuf::from(binary));
    }
    let path_var = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path_var) {
        if cfg!(windows) {
            for ext in WINDOWS_PATH_EXTS {
                let candidate = dir.join(format!("{binary}.{ext}"));
                if candidate.is_file() && !reject(&candidate) {
                    return Some(candidate);
                }
            }
        } else {
            let candidate = dir.join(binary);
            if candidate.is_file() && !reject(&candidate) {
                return Some(candidate);
            }
        }
    }
    None
}

/// `key` as a path, treating an empty value as unset like the tools do.
fn env_dir(key: &str) -> Option<PathBuf> {
    crate::env_dir_from(|var| std::env::var_os(var), key)
}

#[cfg(test)]
fn test_temp_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "llmman-{tag}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// The binary `launch` will run for `i`, so the listing does not report
/// as missing what the launcher would find: `PATH`, then what the
/// launcher knows.
fn find_integration_binary(i: &Integration) -> Option<PathBuf> {
    match i.name {
        "copilot" => copilot::find_copilot(),
        "droid" => droid::find_droid(),
        "opencode" => opencode::find_opencode(),
        "omp" => omp::find_omp(),
        "qwen" => qwen::find_qwen(),
        "dsh" => dsh::find_dsh().map(|(bin, _)| bin),
        "goose" => goose::find_goose(),
        "goose-desktop" => goose_desktop::find_goose_desktop(),
        "grok" => grok::find_grok(),
        "docker-agent" => docker_agent::find_docker_agent(),
        _ => find_on_path(i.binary),
    }
}

/// Fails, before anything is started or pulled, when `name`'s program is
/// not there to run, looked up as its launcher will look for it (so an
/// image-based `--sandbox`, prepared just before, counts as there). Cline
/// installs itself on demand, and an unknown name is left to the
/// launcher's own error.
fn check_installed(name: &str) -> anyhow::Result<()> {
    let Some(i) = integration(name) else {
        return Ok(());
    };
    if i.name == "cline" || find_integration_binary(i).is_some() {
        return Ok(());
    }
    match i.name {
        "dsh" => anyhow::bail!("{}", dsh::DSH_MISSING),
        "docker-agent" => Err(docker_agent::docker_agent_missing()),
        "goose-desktop" => Err(goose_desktop::goose_desktop_missing()),
        _ => anyhow::bail!("{} is not installed", i.binary),
    }
}

/// A yes to an interactive `[y/N]`. Shared by the Cline install
/// prompt and goose-desktop's offer to quit.
fn accepts_prompt(answer: &str) -> bool {
    matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes")
}

// ---------------------------------------------------------------------------
// Variants
// ---------------------------------------------------------------------------

/// Integrations that cannot carry a `--variant` to llmman, and why.
const VARIANT_UNSUPPORTED: &[(&str, &str)] = &[
    ("kimi", "llmman does not set its thinking yet"),
    ("openclaw", "it only takes settings at onboarding"),
    (
        "droid",
        "Droid does not support reasoning effort for custom models",
    ),
    ("gemini", "llmman's Gemini API ignores thinkingConfig"),
    ("agy", "llmman's Gemini API ignores thinkingConfig"),
    ("goose", "it sends effort only for OpenAI's models"),
    ("goose-desktop", "it sends effort only for OpenAI's models"),
    ("docker-agent", "it ignores thinking_budget here"),
    ("vibe", "llmman does not set its thinking yet"),
];

const LOW_TO_MAX: &[&str] = &["low", "medium", "high", "xhigh", "max"];

/// Each other integration's word for thinking off, if any, and the
/// levels it takes for an unknown model without clamping (pi, omp and
/// Cline clamp the rest). opencode takes llmman's own variants.
const VARIANT_SPELLINGS: &[(&str, Option<&str>, &[&str])] = &[
    ("claude", None, LOW_TO_MAX),
    ("codex", Some("none"), EFFORT_LEVELS),
    ("pi", Some("off"), &["minimal", "low", "medium", "high"]),
    (
        "omp",
        Some("off"),
        &["minimal", "low", "medium", "high", "xhigh"],
    ),
    ("cline", Some("none"), &["low", "medium", "high"]),
    ("aider", Some("none"), EFFORT_LEVELS),
    ("copilot", None, LOW_TO_MAX),
    ("copilot-cli", None, LOW_TO_MAX),
    ("hermes", Some("none"), EFFORT_LEVELS),
    ("qwen", Some("none"), LOW_TO_MAX),
    ("grok", Some("none"), EFFORT_LEVELS),
    ("dsh", Some("off"), EFFORT_LEVELS),
];

/// `variant` as `integration` spells it. `thinking` (a switch-only
/// template's on) goes as `medium`, which llmman serves as thinking on.
fn spell_variant<'a>(integration: &str, variant: &'a str) -> anyhow::Result<&'a str> {
    let name = integration.to_lowercase();
    if name == "opencode" {
        return Ok(variant);
    }
    if let Some((_, why)) = VARIANT_UNSUPPORTED.iter().find(|(id, _)| *id == name) {
        anyhow::bail!("--variant does not work with {name}: {why}");
    }
    let Some(&(_, off, levels)) = VARIANT_SPELLINGS.iter().find(|(id, ..)| *id == name) else {
        return Err(unknown_integration(&name));
    };
    let spelled = match variant {
        "none" => off,
        "thinking" => Some("medium"),
        level => levels.iter().copied().find(|l| *l == level),
    };
    spelled.ok_or_else(|| {
        let takes: Vec<&str> = off
            .map(|_| "none")
            .into_iter()
            .chain(levels.iter().copied())
            .chain(Some("thinking"))
            .collect();
        anyhow::anyhow!(
            "{name} cannot start at --variant {variant}; it takes {}",
            takes.join(", ")
        )
    })
}

/// A `--variant` and the model's other levels, as the integration spells
/// them, for those configured with the list.
struct Effort<'a> {
    default: &'a str,
    levels: Vec<&'a str>,
}

/// The flags that start `integration` at `effort`; opencode, qwen and
/// dsh take it in their configuration instead.
fn effort_args(integration: &str, effort: &str) -> Vec<String> {
    let flags: &[&str] = match integration {
        "claude" | "grok" => &["--effort"],
        "copilot" | "copilot-cli" => &["--reasoning-effort"],
        "pi" | "omp" | "cline" => &["--thinking"],
        "hermes" => &["--reasoning"],
        // Else aider drops the effort for a model it does not know.
        "aider" => &["--no-check-model-accepts-settings", "--reasoning-effort"],
        "codex" => return vec!["-c".into(), format!("model_reasoning_effort={effort}")],
        _ => return Vec::new(),
    };
    flags
        .iter()
        .copied()
        .chain(Some(effort))
        .map(String::from)
        .collect()
}

// ---------------------------------------------------------------------------
// Launch dispatcher
// ---------------------------------------------------------------------------

/// `api_key` is what the integration is told to authenticate with:
/// [`providers::PLACEHOLDER_API_KEY`] for a locally-served model, or the
/// real provider key under `--provider`.
///
/// Passing the real one is what makes `--provider` work against a daemon
/// that is *already running* — `ensure_server` reuses one, so a daemon
/// started before the key was exported would otherwise never see it (see
/// `client_api_key` in cmd::serve). Only the launchers that pass a key in
/// the integration's environment can do this; the ones that go through a
/// config file on disk keep the placeholder rather than persist a
/// credential, and need the key in the daemon's own environment.
///
/// `context_window` is the window a request may fill, and the only
/// context figure an integration is told: a local model's loaded one
/// (see [`local_context_window`]), a `--provider` model's catalog one,
/// or a pair's larger half (see [`pair_context_window`]); `None` when
/// none of them is known. codex and kimi substitute a fallback for a
/// `None`.
#[allow(clippy::too_many_arguments)]
fn launch(
    name: &str,
    model: &str,
    api_key: &str,
    thinking: Option<&Thinking>,
    variant: Option<&str>,
    vision: bool,
    context_window: Option<u64>,
    max_output: Option<u32>,
    extra_args: &[String],
    optimizer: Optimizer,
) -> anyhow::Result<()> {
    let name = name.to_lowercase();
    // Unknown levels are a guess: add the one asked for, don't refuse it.
    let widened = variant
        .filter(|_| thinking.is_none())
        .map(unknown_levels_with);
    let thinking = widened.as_ref().or(thinking);
    if let Some(variant) = variant {
        check_variant(model, thinking, variant)?;
    }
    let effort = variant.map(|v| spell_variant(&name, v)).transpose()?;
    let listed = effort.map(|default| Effort {
        default,
        levels: thinking_choices(thinking)
            .into_iter()
            .filter_map(|c| spell_variant(&name, c).ok())
            .collect(),
    });
    let extra_args = &[
        effort.map_or_else(Vec::new, |e| effort_args(&name, e)),
        extra_args.to_vec(),
    ]
    .concat();
    // pi and omp force thinking off for a model not marked as reasoning;
    // a model with the variant reasons.
    let reasons = variant.is_some()
        || thinking
            .and_then(Thinking::template)
            .is_some_and(|t| t.thinks);
    match name.as_str() {
        "claude" => claude::launch_claude(model, api_key, context_window, extra_args, optimizer),
        "opencode" => opencode::launch_opencode(
            model,
            api_key,
            thinking,
            variant,
            vision,
            context_window,
            max_output,
            extra_args,
            optimizer,
        ),
        "codex" => codex::launch_codex(
            model,
            api_key,
            vision,
            context_window,
            extra_args,
            optimizer,
        ),
        "pi" => pi::launch_pi(model, reasons, vision, context_window, extra_args),
        "omp" => omp::launch_omp(model, reasons, vision, context_window, extra_args),
        "cline" => cline::launch_cline(model, extra_args),
        "aider" => aider::launch_aider(model, api_key, extra_args),
        "copilot" | "copilot-cli" => copilot::launch_copilot(model, api_key, extra_args),
        "droid" => droid::launch_droid(model, api_key, vision, max_output, extra_args),
        "kimi" => kimi::launch_kimi(model, api_key, context_window, extra_args),
        "gemini" => gemini::launch_gemini(model, api_key, extra_args),
        "agy" => agy::launch_agy(model, api_key, extra_args),
        "hermes" => hermes::launch_hermes(model, vision, extra_args),
        "openclaw" => openclaw::launch_openclaw(model, context_window, extra_args),
        "qwen" => qwen::launch_qwen(model, api_key, vision, listed.as_ref(), extra_args),
        "dsh" => dsh::launch_dsh(model, api_key, vision, listed.as_ref(), extra_args),
        "goose" => goose::launch_goose(model, api_key, extra_args),
        "goose-desktop" => goose_desktop::launch_goose_desktop(model, api_key, extra_args),
        "grok" => grok::launch_grok(model, api_key, listed.as_ref(), extra_args),
        "docker-agent" => docker_agent::launch_docker_agent(model, api_key, extra_args),
        "vibe" => vibe::launch_vibe(model, api_key, context_window, extra_args),
        other => Err(unknown_integration(other)),
    }
}

/// Refuses a `--variant` the model is known not to have.
fn check_variant(model: &str, thinking: Option<&Thinking>, variant: &str) -> anyhow::Result<()> {
    let choices = thinking_choices(thinking);
    let shown = if model.is_empty() { "the model" } else { model };
    anyhow::ensure!(!choices.is_empty(), "{shown} does not think");
    anyhow::ensure!(
        choices.contains(&variant),
        "{shown} has no variant {variant}; it has {}",
        choices.join(", ")
    );
    Ok(())
}

fn unknown_integration(name: &str) -> anyhow::Error {
    anyhow::anyhow!(
        "unknown integration {name:?}\nRun 'llmman launch' without arguments to list supported integrations."
    )
}

/// Integrations whose launcher writes their configuration to files
/// under the home directory, rather than handing it over in the
/// environment and arguments alone.
const CONFIGURED_BY_FILE: &[&str] = &[
    "agy",
    "cline",
    "codex",
    "docker-agent",
    "dsh",
    "droid",
    "grok",
    "hermes",
    "omp",
    "openclaw",
    "pi",
    "qwen",
];

/// What `name` keeps on this machine, which `--sandbox` lets it write:
/// the directories its launcher writes config into, and its own
/// settings and session state, found the way the integration finds them.
fn sandbox_state(name: &str) -> anyhow::Result<Vec<sandbox::State>> {
    use sandbox::State::{Dir, Files};
    let home = dirs::home_dir().context("no home directory")?;
    let xdg = |var: &str, default: &str| xdg_dir(&home, var, default);
    let config = xdg("XDG_CONFIG_HOME", ".config");
    let data = xdg("XDG_DATA_HOME", ".local/share");
    let state = xdg("XDG_STATE_HOME", ".local/state");
    Ok(match name {
        "claude" => match env_dir("CLAUDE_CONFIG_DIR") {
            Some(dir) => vec![Dir(dir)],
            None => vec![Dir(home.join(".claude")), Files(home.join(".claude.json"))],
        },
        "opencode" => vec![
            Dir(config.join("opencode")),
            Dir(data.join("opencode")),
            Dir(opencode::opencode_state_dir()?),
            Dir(xdg("XDG_CACHE_HOME", ".cache").join("opencode")),
        ],
        "codex" => vec![Dir(codex::codex_dir()?)],
        "pi" => vec![Dir(pi::pi_agent_dir()?)],
        "omp" => vec![Dir(omp::omp_agent_dir()?)],
        "cline" => vec![Dir(cline::cline_dir()?)],
        "aider" => vec![Dir(home.join(".aider"))],
        "copilot" | "copilot-cli" => vec![
            Dir(env_dir("COPILOT_HOME").unwrap_or_else(|| home.join(".copilot"))),
            Dir(env_dir("GH_CONFIG_DIR").unwrap_or_else(|| config.join("gh"))),
        ],
        "droid" => vec![Dir(droid::droid_home()?)],
        "kimi" => vec![Dir(home.join(".kimi"))],
        "vibe" => vec![Dir(
            env_dir("VIBE_HOME").unwrap_or_else(|| home.join(".vibe"))
        )],
        "gemini" => vec![Dir(home.join(".gemini"))],
        "agy" => vec![Dir(agy::agy_settings_dir()?)],
        "hermes" => vec![Dir(hermes::hermes_home()?)],
        // `openclaw::launch_openclaw` takes the legacy config as onboarded too.
        "openclaw" => std::iter::once(home.join(".openclaw"))
            .chain(Some(home.join(".clawdbot")).filter(|d| d.is_dir()))
            .map(Dir)
            .collect(),
        "qwen" => vec![Dir(qwen::qwen_home()?)],
        "dsh" => vec![Dir(dsh::dsh_config_dir()?)],
        // The desktop app drives the same goose, so it reads the same
        // tree, and keeps Electron `userData` beside it under the
        // bundle's name — a separate directory wherever the filesystem
        // tells `Goose` and `goose` apart.
        "goose" | "goose-desktop" => {
            let mut dirs = vec![
                Dir(config.join("goose")),
                Dir(data.join("goose")),
                Dir(state.join("goose")),
            ];
            if name == "goose-desktop" {
                dirs.push(Dir(goose_desktop::goose_desktop_user_data(&home, &config)));
                // macOS keeps the app's preferences outside `userData`,
                // under the bundle id `Goose.app` 1.52.0 declares.
                if cfg!(target_os = "macos") {
                    dirs.push(Files(
                        home.join("Library")
                            .join("Preferences")
                            .join("com.electron.goose.plist"),
                    ));
                }
            }
            dirs
        }
        "grok" => vec![Dir(grok::grok_home()?)],
        "docker-agent" => vec![
            Dir(docker_agent::docker_agent_config_dir()?),
            Dir(home.join(".cagent")),
        ],
        other => return Err(unknown_integration(other)),
    })
}

// ---------------------------------------------------------------------------
// Per-integration launchers
// ---------------------------------------------------------------------------

/// `%USERPROFILE%` on Windows, where node's `os.homedir()` reads it and
/// `dirs::home_dir` does not. `None` elsewhere, where `os.homedir()`
/// reads `$HOME` and `dirs` agrees.
fn node_user_profile() -> Option<PathBuf> {
    cfg!(windows).then(|| env_dir("USERPROFILE")).flatten()
}

/// `$var`, else `default` under `home`.
fn xdg_dir(home: &Path, var: &str, default: &str) -> PathBuf {
    env_dir(var).unwrap_or_else(|| home.join(default))
}

/// The thinking choices a model offers an integration, in cycle order.
enum Thinking {
    /// A local model's, read off its chat template.
    Template(ThinkingControls),
    /// A provider's model's, from the catalog (see
    /// `crate::providers::Model::thinking`).
    Listed(Vec<String>),
}

impl Thinking {
    fn choices(&self) -> Vec<&str> {
        match self {
            Thinking::Template(controls) => controls.choices(),
            Thinking::Listed(levels) => levels.iter().map(String::as_str).collect(),
        }
    }

    fn template(&self) -> Option<&ThinkingControls> {
        match self {
            Thinking::Template(controls) => Some(controls),
            Thinking::Listed(_) => None,
        }
    }
}

/// The choices offered when neither a template nor the catalog says:
/// thinking off, then the levels every wire accepts
/// (`anthropic::portable_efforts`).
const PORTABLE_THINKING_LEVELS: &[&str] = &["none", "low", "medium", "high"];

/// The model's variants in cycle order, [`PORTABLE_THINKING_LEVELS`] if
/// unknown.
fn thinking_choices(thinking: Option<&Thinking>) -> Vec<&str> {
    match thinking {
        Some(thinking) => thinking.choices(),
        None => PORTABLE_THINKING_LEVELS.to_vec(),
    }
}

/// The guessed levels plus `variant`, for a model whose levels are
/// unknown: a provider can serve a level before models.dev lists it, and
/// an integration must find its starting level among those it is given.
/// `thinking` is a template switch, not a level, so it stays refused.
fn unknown_levels_with(variant: &str) -> Thinking {
    let levels = std::iter::once("none")
        .chain(EFFORT_LEVELS.iter().copied())
        .filter(|l| PORTABLE_THINKING_LEVELS.contains(l) || *l == variant)
        .map(String::from)
        .collect();
    Thinking::Listed(levels)
}

/// The window the daemon serves for `model`, read back from the loaded
/// runner rather than predicted: an OOM retry halves `--ctx-size` during
/// the load, and a reused daemon keeps whatever it was started with.
/// [`served_context_window`] is the fallback for a backend that reports
/// none of its own, vLLM and MLX among them.
fn local_context_window(model: &str, trained: Option<u64>) -> Option<u64> {
    crate::daemon::loaded_context_length(model)
        .or_else(|| served_context_window(super::serve::context_length_from_env(), trained))
}

/// A hybrid pair's window: whichever half holds more.
///
/// Requests above the local budget are routed to the hosted half, so the
/// pair can hold the larger of the two whichever way round they are —
/// declaring only the local window makes the agent compact before a
/// request is ever big enough to route, and declaring only the hosted
/// one understates a local half that is larger.
///
/// `hosted` is `None` for a provider defined in `llmman.conf`, which has
/// no catalog entry; the local window then stands on its own.
fn pair_context_window(local: Option<u64>, hosted: Option<u64>) -> Option<u64> {
    match (local, hosted) {
        (Some(local), Some(hosted)) => Some(local.max(hosted)),
        (window, None) | (None, window) => window,
    }
}

/// The window the daemon would serve, predicted rather than read,
/// mirroring `initial_ctx_size`:
///
/// * `LLMMAN_CONTEXT_LENGTH` set and positive — forwarded as `--ctx-size`
///   uncapped, so it is what gets served.
/// * Set to `0` — `--ctx-size 0`, which llama.cpp reads as the model's
///   own `trained` context, also uncapped.
/// * Unset — the default, clamped *down* to `trained`, so a model
///   trained past [`super::serve::DEFAULT_CTX_SIZE`] is still served
///   only the default.
///
/// `None` when the window is unknown: every caller but codex passes that
/// through as "say nothing", leaving the integration its own default.
///
/// Blind to the daemon's `LLMMAN_NUM_PARALLEL`: llama-server splits
/// `--ctx-size 0`'s trained context across that many slots, so this
/// overstates a request's window by that factor. One more reason
/// [`local_context_window`] reads the live one first.
fn served_context_window(env: Option<u32>, trained: Option<u64>) -> Option<u64> {
    match env {
        Some(0) => trained,
        Some(explicit) => Some(u64::from(explicit)),
        None => trained.map(|trained| trained.min(u64::from(super::serve::DEFAULT_CTX_SIZE))),
    }
}

// ---------------------------------------------------------------------------
// Process execution helper
// ---------------------------------------------------------------------------

fn exec_with_env(bin: &Path, args: &[String], extra_env: &[(&str, &str)]) -> anyhow::Result<()> {
    exec_with_env_removing(bin, args, extra_env, &[])
}

/// [`exec_with_env`] for a computed environment.
fn exec_with_owned_env(bin: &Path, args: &[String], env: &[(&str, String)]) -> anyhow::Result<()> {
    let env: Vec<(&str, &str)> = env.iter().map(|(k, v)| (*k, v.as_str())).collect();
    exec_with_env(bin, args, &env)
}

fn exec_with_env_removing(
    bin: &Path,
    args: &[String],
    extra_env: &[(&str, &str)],
    remove_env: &[&str],
) -> anyhow::Result<()> {
    let code = run_with_env_removing(bin, args, extra_env, remove_env);
    hdrm::stop();
    std::process::exit(code?);
}

/// Runs the integration — in the `--sandbox`, when there is one — and
/// returns its exit code.
fn run_with_env(bin: &Path, args: &[String], extra_env: &[(&str, &str)]) -> anyhow::Result<i32> {
    run_with_env_removing(bin, args, extra_env, &[])
}

fn run_with_env_removing(
    bin: &Path,
    args: &[String],
    extra_env: &[(&str, &str)],
    remove_env: &[&str],
) -> anyhow::Result<i32> {
    // The inherited environment, overlaid with OLLAMA_HOST and the
    // integration's variables, later ones winning.
    let mut overlay = vec![("OLLAMA_HOST".to_string(), server())];
    overlay.extend(
        extra_env
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string())),
    );
    if sandbox::active() {
        return sandbox::run(bin, args, &overlay, remove_env);
    }

    let mut cmd = Command::new(bin);
    cmd.args(args);
    cmd.stdin(std::process::Stdio::inherit());
    cmd.stdout(std::process::Stdio::inherit());
    cmd.stderr(std::process::Stdio::inherit());
    cmd.envs(overlay);
    for name in remove_env {
        cmd.env_remove(name);
    }

    let status = cmd
        .status()
        .with_context(|| format!("failed to run {}", bin.display()))?;
    Ok(status.code().unwrap_or(1))
}

/// The daemon's URL as the integration reaches it: `daemon::server()`,
/// or the `--sandbox`'s name for this machine.
fn server() -> String {
    sandbox::agent_server()
}

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

    /// Cline installs itself, and a name launch does not know is left to
    /// its own error, so neither is refused here whatever is on PATH.
    #[test]
    fn check_installed_leaves_cline_and_unknown_names_alone() {
        assert!(check_installed("cline").is_ok());
        assert!(check_installed("not-an-integration").is_ok());
    }

    #[test]
    fn agy_is_listed_as_an_integration() {
        let agy = INTEGRATIONS.iter().find(|i| i.name == "agy").unwrap();
        assert_eq!(agy.binary, "agy");
    }

    #[test]
    fn copilot_is_the_standalone_model_required_byok_integration() {
        let copilot = INTEGRATIONS.iter().find(|i| i.name == "copilot").unwrap();
        assert_eq!(copilot.binary, "copilot");
        for id in ["copilot", "copilot-cli"] {
            assert!(MODEL_REQUIRED.contains(&id));
            assert!(MODEL_FLAG_FORWARDED.contains(&id));
            assert!(!PROVIDER_UNSUPPORTED.iter().any(|(name, _)| *name == id));
            assert_eq!(effort_args(id, "high"), ["--reasoning-effort", "high"]);
        }
    }

    #[test]
    fn droid_is_a_file_configured_model_required_integration() {
        let droid = INTEGRATIONS.iter().find(|i| i.name == "droid").unwrap();
        assert_eq!(droid.binary, "droid");
        assert!(MODEL_REQUIRED.contains(&"droid"));
        assert!(MODEL_FLAG_FORWARDED.contains(&"droid"));
        assert!(CONFIGURED_BY_FILE.contains(&"droid"));
        assert!(check_provider_supported("droid").is_ok());
        assert!(VARIANT_UNSUPPORTED.iter().any(|(name, _)| *name == "droid"));
    }

    /// Every integration `--provider` refuses must be one `launch`
    /// actually dispatches, or the refusal is for a name nobody can type
    /// and the real one is still silently broken.
    #[test]
    fn every_provider_unsupported_integration_is_a_real_one() {
        for (id, why) in PROVIDER_UNSUPPORTED {
            assert!(integration(id).is_some(), "{id} is not an integration");
            assert!(!why.is_empty(), "{id} has no reason");
            assert!(check_provider_supported(id).is_err(), "{id} was accepted");
            // Case-insensitively, the way `launch` dispatches.
            assert!(check_provider_supported(&id.to_uppercase()).is_err());
        }
        // Same for the ones that depend on the daemon holding the key:
        // a name nobody can type protects nobody.
        for id in PROVIDER_NEEDS_DAEMON_KEY {
            assert!(
                INTEGRATIONS.iter().any(|i| i.name == *id),
                "{id} is not an integration"
            );
            assert!(
                !PROVIDER_UNSUPPORTED.iter().any(|(u, _)| u == id),
                "{id} is both refused outright and expected to work"
            );
        }
    }

    /// `--sandbox` must know what every integration writes, or the first
    /// one it misses fails on a read-only home directory, not up front.
    #[test]
    fn every_integration_has_sandbox_state() {
        for i in INTEGRATIONS {
            let state = sandbox_state(i.name).unwrap();
            assert!(!state.is_empty(), "{} has no sandbox state", i.name);
        }
        assert!(sandbox_state("copilot-cli").is_ok());
        assert!(sandbox_state("nope").is_err());
        for id in CONFIGURED_BY_FILE {
            assert!(
                INTEGRATIONS.iter().any(|i| i.name == *id),
                "{id} is not an integration"
            );
        }
    }

    /// The directory a file-configured launcher writes into must be one
    /// the sandbox mounts, or the integration never sees its config.
    #[test]
    fn sandbox_state_covers_where_the_launchers_write() {
        let covers = |name: &str, written: PathBuf| {
            let state = sandbox_state(name).unwrap();
            assert!(
                state.iter().any(|s| matches!(
                    s,
                    sandbox::State::Dir(dir) if written.starts_with(dir)
                )),
                "{name}'s {} is outside its sandbox state {state:?}",
                written.display()
            );
        };
        covers("codex", codex::codex_dir().unwrap());
        covers(
            "opencode",
            opencode::opencode_state_dir().unwrap().join("model.json"),
        );
        covers("pi", pi::pi_agent_dir().unwrap());
        covers("omp", omp::omp_agent_dir().unwrap());
        covers("cline", cline::cline_data_dir().unwrap());
        covers("agy", agy::agy_settings_dir().unwrap());
        covers("hermes", hermes::hermes_home().unwrap());
        covers("qwen", qwen::qwen_home().unwrap());
        covers("dsh", dsh::dsh_config_dir().unwrap());
        covers("grok", grok::grok_home().unwrap().join("llmman"));
        covers(
            "docker-agent",
            docker_agent::docker_agent_config_dir().unwrap(),
        );
        covers(
            "openclaw",
            dirs::home_dir()
                .unwrap()
                .join(".openclaw")
                .join("openclaw.json"),
        );
    }

    /// A provider-routed `--model` must come out under
    /// `providers::REMOTE_PREFIX`, which is the only thing that stops the
    /// daemon resolving it as a HuggingFace or registry reference — and
    /// must keep an `<vendor>/<model>` id (openrouter's shape) intact.
    #[test]
    fn provider_models_are_encoded_under_the_remote_prefix() {
        assert_eq!(
            providers::format_remote_ref("openrouter", "qwen/qwen3-coder"),
            "llmman.provider/openrouter/qwen/qwen3-coder"
        );
        assert_eq!(
            providers::split_remote_ref(&providers::format_remote_ref("groq", "llama-3.3-70b")),
            Some(("groq", "llama-3.3-70b"))
        );
    }

    /// The default path must be untouched by provider support: no
    /// `--provider` means the same shortname resolution, and so the same
    /// daemon behavior, as before it existed.
    #[test]
    fn local_models_are_unaffected_by_the_remote_prefix() {
        for local in ["qwen3.5:0.8b", "hf.co/unsloth/Qwen3.5-0.8B-GGUF"] {
            let resolved = crate::shortnames::resolve_ollama_api(local).unwrap();
            assert!(
                !providers::is_remote_ref(&resolved),
                "{local} resolved to a provider-routed reference: {resolved}"
            );
        }
    }

    /// Every integration `check_model_flag` holds to a model must be one
    /// `launch` dispatches; it is refused without one, under `--provider`
    /// too, and a `--model` after `--` is let through.
    #[test]
    fn model_required_integrations_are_refused_without_a_model() {
        let none: Vec<String> = vec![];
        for id in MODEL_REQUIRED {
            assert!(integration(id).is_some(), "{id} is not an integration");
            assert!(check_model_flag(id, None, None, None, &none).is_err());
            assert!(check_model_flag(id, Some(" "), None, None, &none).is_err());
            assert!(check_model_flag(&id.to_uppercase(), None, None, None, &none).is_err());
            let err = check_model_flag(id, None, Some("openrouter"), None, &none).unwrap_err();
            assert!(
                err.to_string().contains("--provider openrouter --model"),
                "{err}"
            );
            assert!(check_model_flag(id, Some("m"), None, None, &none).is_ok());
            let forwarded = vec!["--model".to_string(), "theirs".to_string()];
            assert!(check_model_flag(id, Some("m"), None, None, &forwarded).is_ok());
        }
        assert!(check_model_flag("claude", None, None, None, &none).is_ok());
        // The "yours wins" warning is only claimed for launchers that
        // actually yield to it; dsh has no `--model` flag to yield to.
        for id in MODEL_FLAG_FORWARDED {
            assert!(integration(id).is_some(), "{id} is not an integration");
        }
        assert!(!MODEL_FLAG_FORWARDED.contains(&"dsh"));
        // claude yields without requiring a model, so the warning must not
        // depend on `MODEL_REQUIRED`.
        assert!(MODEL_FLAG_FORWARDED.contains(&"claude"));
        assert!(!MODEL_REQUIRED.contains(&"claude"));
    }

    /// goose carries the key in its own environment, so `--provider`
    /// needs neither a refusal nor the daemon holding the key.
    #[test]
    fn goose_carries_its_own_key_so_provider_works() {
        assert!(INTEGRATIONS.iter().any(|i| i.name == "goose"));
        assert!(check_provider_supported("goose").is_ok());
        assert!(!PROVIDER_NEEDS_DAEMON_KEY.contains(&"goose"));
    }

    /// The predicate has to be able to veto, or `find_goose`'s refusal of
    /// the desktop app is decoration. A rejected match must also leave
    /// `find_on_path` as it was: it passes `|_| false`, so every other
    /// integration still resolves exactly as before this predicate
    /// existed.
    #[test]
    fn find_on_path_unless_consults_its_predicate() {
        // Any binary this machine really has, so there is a match for
        // the predicate to veto.
        let present = ["cargo", "sh", "cmd"]
            .into_iter()
            .find(|b| find_on_path(b).is_some());
        let Some(present) = present else {
            eprintln!("skipping: no known binary on PATH to test against");
            return;
        };
        let found = find_on_path(present).unwrap();
        assert!(found.is_file());
        // Vetoing everything finds nothing, whatever is on PATH.
        assert_eq!(find_on_path_unless(present, |_| true), None);
        // Vetoing only what was not found leaves that result standing.
        assert_eq!(
            find_on_path_unless(present, |p| p != found),
            Some(found.clone())
        );
        // A name nothing answers to stays unfound, predicate or not.
        assert_eq!(find_on_path("llmman-no-such-binary-xyz"), None);
        assert_eq!(
            find_on_path_unless("llmman-no-such-binary-xyz", |_| false),
            None
        );
    }

    /// The desktop app keeps Electron state the CLI has none of, and
    /// `--sandbox` has to let it write there: `userData`, named for the
    /// bundle (`Goose`) rather than the CLI. Without it a sandboxed run
    /// starts blank every time.
    #[test]
    fn goose_desktop_sandbox_state_covers_the_electron_user_data() {
        let desktop = sandbox_state("goose-desktop").unwrap();
        let cli = sandbox_state("goose").unwrap();
        // Everything the CLI gets, and then what only the app keeps.
        assert!(desktop.len() > cli.len(), "{desktop:?}");
        assert!(
            desktop.iter().any(|s| matches!(
                s,
                sandbox::State::Dir(d) if d.file_name() == Some(std::ffi::OsStr::new("Goose"))
            )),
            "no userData directory in {desktop:?}"
        );
        // macOS keeps preferences outside `userData`, as a file.
        if cfg!(target_os = "macos") {
            assert!(
                desktop.iter().any(|s| matches!(
                    s,
                    sandbox::State::Files(f) if f.extension() == Some(std::ffi::OsStr::new("plist"))
                )),
                "no preferences file in {desktop:?}"
            );
        }
    }

    /// Like the CLI, the desktop app carries the key per request and
    /// writes nothing, so it must be absent from every list that would
    /// say otherwise, and `--provider` must work.
    #[test]
    fn goose_desktop_carries_its_own_key_so_provider_works() {
        assert!(INTEGRATIONS.iter().any(|i| i.name == "goose-desktop"));
        assert!(check_provider_supported("goose-desktop").is_ok());
        assert!(!PROVIDER_NEEDS_DAEMON_KEY.contains(&"goose-desktop"));
        assert!(!CONFIGURED_BY_FILE.contains(&"goose-desktop"));
        // It has no --model of its own to yield to.
        assert!(!MODEL_FLAG_FORWARDED.contains(&"goose-desktop"));
        // Needs a model, bar the dictation-only launch below.
        assert!(MODEL_REQUIRED.contains(&"goose-desktop"));
    }

    #[test]
    fn goose_desktop_launches_without_a_model_only_for_dictation() {
        let none: Vec<String> = vec![];
        let speech = Some("hf.co/ggml-org/Voxtral-Mini-3B-2507-GGUF:Q4_K_M");
        for id in ["goose-desktop", "GOOSE-DESKTOP"] {
            assert!(check_model_flag(id, None, None, speech, &none).is_ok());
            assert!(check_model_flag(id, Some("m"), None, speech, &none).is_ok());
            for blank in [None, Some(""), Some(" \t\n")] {
                let err = check_model_flag(id, None, None, blank, &none).unwrap_err();
                assert!(err
                    .to_string()
                    .contains("--model <model>, or set OPENAI_TRANSCRIPTION_MODEL="));
            }
            let err = check_model_flag(id, None, Some("openrouter"), speech, &none).unwrap_err();
            assert!(err
                .to_string()
                .contains("--provider openrouter --model <model>"));
            assert!(!err.to_string().contains("OPENAI_TRANSCRIPTION_MODEL"));
        }
        // No other integration, the goose CLI included, is let off.
        for id in MODEL_REQUIRED.iter().filter(|id| **id != "goose-desktop") {
            assert!(check_model_flag(id, None, None, speech, &none).is_err());
        }
    }

    #[test]
    fn cline_is_model_required_and_supports_provider_routes() {
        assert!(INTEGRATIONS.iter().any(|i| i.name == "cline"));
        assert!(MODEL_REQUIRED.contains(&"cline"));
        assert!(MODEL_FLAG_FORWARDED.contains(&"cline"));
        assert!(!PROVIDER_UNSUPPORTED.iter().any(|(id, _)| *id == "cline"));
        assert!(PROVIDER_NEEDS_DAEMON_KEY.contains(&"cline"));
        assert!(check_provider_supported("cline").is_ok());
    }

    #[test]
    fn grok_is_model_required_and_refuses_unrepresentable_provider_routes() {
        assert!(INTEGRATIONS.iter().any(|i| i.name == "grok"));
        assert!(MODEL_REQUIRED.contains(&"grok"));
        assert!(MODEL_FLAG_FORWARDED.contains(&"grok"));
        let error = check_provider_supported("grok").unwrap_err().to_string();
        assert!(error.contains("model catalog"), "{error}");
        assert!(!PROVIDER_NEEDS_DAEMON_KEY.contains(&"grok"));
    }

    /// A word or `=`-joined, and nothing looser: `-sm` is not `-m`.
    #[test]
    fn has_flag_takes_the_exact_and_joined_forms_only() {
        let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        assert!(has_flag(&args(&["--model", "x"]), "--model", Some("-m")));
        assert!(has_flag(&args(&["--model=x"]), "--model", Some("-m")));
        assert!(has_flag(&args(&["-m", "x"]), "--model", Some("-m")));
        assert!(has_flag(&args(&["-m=x"]), "--model", Some("-m")));
        assert!(!has_flag(&args(&["-sm", "x"]), "--model", Some("-m")));
        assert!(!has_flag(
            &args(&["--model-context", "x"]),
            "--model",
            Some("-m")
        ));
    }

    /// The `--variant` guard in `run` relies on this to refuse a forwarded
    /// `--model`.
    #[test]
    fn forwarded_model_flag_is_seen_for_each_integration_that_yields() {
        let args = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        let model = args(&["--model", "other/model"]);
        for name in MODEL_FLAG_FORWARDED {
            assert!(has_forwarded_model_flag(name, &model), "{name}");
        }
        // docker-agent refuses a forwarded model; dsh takes none.
        assert!(!has_forwarded_model_flag("docker-agent", &model));
        assert!(!has_forwarded_model_flag("dsh", &model));
        // copilot and claude have no `-m`; grok does.
        let short = args(&["-m", "other/model"]);
        assert!(!has_forwarded_model_flag("claude", &short));
        assert!(!has_forwarded_model_flag("copilot", &short));
        assert!(has_forwarded_model_flag("grok", &short));
        // After a second `--` it is a message, not a flag.
        let message = args(&["--", "--model", "other/model"]);
        assert!(!has_forwarded_model_flag("claude", &message));
        assert!(has_forwarded_model_flag(
            "claude",
            &args(&["--model", "other/model", "--", "hi"])
        ));
    }

    /// Only the integrations that put a window somewhere pay for the
    /// load that reads the live one back. Every launcher in [`launch`]
    /// that takes `context_window` must be listed, or it silently gets
    /// none.
    #[test]
    fn the_integrations_that_declare_a_window_are_the_ones_that_take_one() {
        assert!(declares_context_window("opencode"));
        assert!(declares_context_window("codex"));
        assert!(declares_context_window("OpenCode"), "matched case-blind");
        // pi and omp write a window into their own model entries too,
        // so they read the live one back rather than predict it from
        // the trained context.
        assert!(declares_context_window("pi"));
        assert!(declares_context_window("omp"));
        assert!(declares_context_window("kimi"));
        assert!(declares_context_window("openclaw"));
        // claude is told it through CLAUDE_CODE_MAX_CONTEXT_TOKENS
        assert!(declares_context_window("claude"));
        assert!(!declares_context_window("aider"));
    }

    /// Requests above the local budget route to the hosted half, so the
    /// pair holds the larger window whichever way round the two are.
    /// Declaring the local one alone has the agent compact before a
    /// request is ever big enough to route — the bug this fixes.
    #[test]
    fn pair_context_window_takes_whichever_half_holds_more() {
        // The usual pairing: a small local model overflowing to a large
        // hosted one.
        assert_eq!(
            pair_context_window(Some(262_144), Some(1 << 20)),
            Some(1 << 20)
        );
        // Reversed — paired for quality, not capacity. The hosted window
        // must not shrink what the local half can already hold.
        assert_eq!(
            pair_context_window(Some(1 << 20), Some(200_000)),
            Some(1 << 20)
        );
        // A provider from llmman.conf names no window, so the local one
        // stands alone rather than being discarded.
        assert_eq!(pair_context_window(Some(262_144), None), Some(262_144));
        assert_eq!(pair_context_window(None, Some(200_000)), Some(200_000));
        assert_eq!(pair_context_window(None, None), None);
    }

    /// The same precedence, minus codex's guess: the launchers that may
    /// omit the key see `None` instead of a fallback, so an integration's
    /// own default stands rather than a number llmman invented.
    #[test]
    fn served_context_window_prefers_the_environment_then_the_trained_context() {
        let cases = [
            (Some(16384), Some(32768), Some(16384)),
            (Some(65536), Some(32768), Some(65536)),
            (Some(16384), None, Some(16384)),
            (None, Some(32768), Some(32768)),
            // `--ctx-size 0` is the model's own context, so it is served
            // uncapped like any other explicit value — the one case the
            // default-clamping branch below would get wrong.
            (Some(0), Some(32768), Some(32768)),
            (Some(0), Some(1 << 20), Some(1 << 20)),
            // Clamped down to the trained context, never up to it.
            (
                None,
                Some(1 << 20),
                Some(u64::from(super::super::serve::DEFAULT_CTX_SIZE)),
            ),
            // An explicit value is forwarded uncapped, so it stands.
            (Some(1 << 20), Some(1 << 20), Some(1 << 20)),
            // Neither: say nothing, where codex would guess. A `0` with
            // no trained context to name has nothing to forward either.
            (Some(0), None, None),
            (None, None, None),
        ];
        for (env, trained, want) in cases {
            assert_eq!(
                served_context_window(env, trained),
                want,
                "env={env:?} trained={trained:?}"
            );
        }
    }

    /// dsh carries a real key per launch, unlike hermes, so it belongs
    /// on neither `--provider` refusal list.
    #[test]
    fn dsh_is_a_real_integration_and_not_on_a_provider_refusal_list() {
        assert!(INTEGRATIONS.iter().any(|i| i.name == "dsh"));
        assert!(!PROVIDER_UNSUPPORTED.iter().any(|(id, _)| *id == "dsh"));
        assert!(!PROVIDER_NEEDS_DAEMON_KEY.contains(&"dsh"));
    }

    // -----------------------------------------------------------------
    // docker-agent
    // -----------------------------------------------------------------

    /// Needs a model, but carries its key in the environment, so it is on
    /// neither `PROVIDER_UNSUPPORTED` nor `PROVIDER_NEEDS_DAEMON_KEY` —
    /// and not on `MODEL_FLAG_FORWARDED` either, since a forwarded
    /// `--model` is refused rather than obeyed.
    #[test]
    fn docker_agent_is_a_real_model_required_integration_provider_can_drive() {
        let entry = INTEGRATIONS
            .iter()
            .find(|i| i.name == "docker-agent")
            .expect("docker-agent is not an integration");
        assert_eq!(entry.binary, "docker-agent");
        assert!(MODEL_REQUIRED.contains(&"docker-agent"));
        assert!(!MODEL_FLAG_FORWARDED.contains(&"docker-agent"));
        assert!(check_provider_supported("docker-agent").is_ok());
        assert!(!PROVIDER_NEEDS_DAEMON_KEY.contains(&"docker-agent"));
    }

    /// `print_integrations` pads each name into a 14-wide column and
    /// prints a space after it, so every description starts at the same
    /// place — including "goose-desktop", the longest at 13. A longer
    /// name would push its own description right instead.
    #[test]
    fn every_integration_name_fits_the_listings_column() {
        let longest = INTEGRATIONS.iter().map(|i| i.name.len()).max().unwrap();
        assert!(
            longest <= 14,
            "{longest}-character name overflows the `{{:<14}}` column in print_integrations"
        );
    }

    // -- --variant ---------------------------------------------------------

    /// A model with unknown levels (`None`) takes any effort level, and
    /// opencode finds it among its variants; one with known levels still
    /// refuses what it lacks.
    #[test]
    fn a_variant_is_refused_only_for_a_model_with_known_levels() {
        let check = |thinking: Option<&Thinking>, variant: &str| {
            check_variant("m", thinking, variant).map_err(|e| e.to_string())
        };
        assert!(check(None, "xhigh").is_err(), "the guess refuses");
        for variant in ["none", "minimal", "low", "high", "xhigh", "max"] {
            let widened = unknown_levels_with(variant);
            check(Some(&widened), variant).unwrap();
        }
        let thinking = unknown_levels_with("thinking");
        assert!(check(Some(&thinking), "thinking").is_err());

        let widened = unknown_levels_with("xhigh");
        let variants = opencode::opencode_variants(Some(&widened));
        let names: Vec<_> = variants.iter().map(|(name, _)| *name).collect();
        assert_eq!(names, ["none", "low", "medium", "high", "xhigh"]);
        assert_eq!(
            variants[4].1,
            serde_json::json!({ "reasoningEffort": "xhigh" })
        );

        let listed = Thinking::Listed(["low", "high"].map(String::from).to_vec());
        assert!(check(Some(&listed), "high").is_ok());
        let err = check(Some(&listed), "xhigh").unwrap_err();
        assert!(
            err.contains("m has no variant xhigh; it has low, high"),
            "{err}"
        );
        let none = Thinking::Listed(Vec::new());
        assert!(check(Some(&none), "low")
            .unwrap_err()
            .contains("does not think"));
    }

    #[test]
    fn spell_variant_uses_each_integrations_own_words() {
        let ok = |name, variant| spell_variant(name, variant).unwrap();
        assert_eq!(ok("opencode", "thinking"), "thinking");
        assert_eq!(ok("codex", "none"), "none");
        assert_eq!(ok("pi", "none"), "off");
        assert_eq!(ok("dsh", "none"), "off");
        assert_eq!(ok("claude", "max"), "max");
        // A switch-only template's on is a level llmman serves as on.
        assert_eq!(ok("cline", "thinking"), "medium");
        assert_eq!(ok("Claude", "high"), "high");
    }

    #[test]
    fn spell_variant_refuses_what_the_integration_cannot_start_at() {
        let err = |name, variant| spell_variant(name, variant).unwrap_err().to_string();
        let claude = err("claude", "none");
        assert!(
            claude.contains("low, medium, high, xhigh, max, thinking"),
            "{claude}"
        );
        assert!(!claude.contains("none,"), "{claude}");
        // Would clamp to high rather than start where asked.
        assert!(err("pi", "xhigh").contains("none, minimal"));
        assert!(err("cline", "minimal").contains("cline cannot start"));
        assert!(err("kimi", "high").contains("--variant does not work with kimi"));
        assert!(err("nope", "high").contains("unknown integration"));
    }

    /// Every integration is either spelled or refused, never silently
    /// launched without its variant.
    #[test]
    fn every_integration_takes_or_refuses_a_variant() {
        for i in INTEGRATIONS {
            let spelled = spell_variant(i.name, "thinking").is_ok();
            let refused = VARIANT_UNSUPPORTED.iter().any(|(id, _)| *id == i.name);
            let configured = matches!(i.name, "opencode" | "qwen" | "dsh");
            assert!(spelled != refused, "{}", i.name);
            assert!(
                refused || configured || !effort_args(i.name, "medium").is_empty(),
                "{} is spelled but never told",
                i.name
            );
        }
    }

    #[test]
    fn effort_args_lead_with_each_integrations_flag() {
        let args = |name| effort_args(name, "high");
        assert_eq!(args("claude"), ["--effort", "high"]);
        assert_eq!(args("pi"), ["--thinking", "high"]);
        assert_eq!(args("hermes"), ["--reasoning", "high"]);
        assert_eq!(args("codex"), ["-c", "model_reasoning_effort=high"]);
        assert_eq!(
            args("aider"),
            [
                "--no-check-model-accepts-settings",
                "--reasoning-effort",
                "high"
            ]
        );
        assert!(args("qwen").is_empty());
    }

    #[test]
    fn launch_variant_is_one_of_llmmans() {
        #[derive(clap::Parser)]
        struct Cli {
            #[command(flatten)]
            args: LaunchArgs,
        }
        let parse = |v: &str| <Cli as clap::Parser>::try_parse_from(["l", "pi", "--variant", v]);
        assert_eq!(
            parse("xhigh").unwrap().args.variant.as_deref(),
            Some("xhigh")
        );
        assert!(parse("turbo").is_err());

        // Refused before the daemon starts: the variant is checked
        // against llmman's --model, not the one Qwen Code would use.
        let args = <Cli as clap::Parser>::try_parse_from([
            "l",
            "qwen",
            "-m",
            "a",
            "--variant",
            "high",
            "--",
            "--model",
            "b",
        ])
        .unwrap()
        .args;
        let err = run(&args).unwrap_err().to_string();
        assert!(err.contains("not after --"), "{err}");
    }

    /// OpenShell gets none of the host's files, including the state a
    /// `--variant` is written to.
    #[cfg(not(windows))]
    #[test]
    fn opencode_variant_is_refused_where_the_sandbox_gets_no_files() {
        #[derive(clap::Parser)]
        struct Cli {
            #[command(flatten)]
            args: LaunchArgs,
        }
        let args = <Cli as clap::Parser>::try_parse_from([
            "l",
            "opencode",
            "--sandbox",
            "openshell",
            "--variant",
            "high",
        ])
        .unwrap()
        .args;
        let err = run(&args).unwrap_err().to_string();
        assert!(err.contains("openshell cannot run opencode"), "{err}");
    }

    /// A bare `--sandbox` is `--sandbox sbx`, and a named one is kept.
    #[test]
    fn a_bare_sandbox_is_sbx() {
        assert_eq!(launch_args(&["claude"]).sandbox, None);
        assert_eq!(
            launch_args(&["claude", "--sandbox"]).sandbox,
            Some(Sandbox::Sbx)
        );
        assert_eq!(
            launch_args(&["claude", "--sandbox", "docker"]).sandbox,
            Some(Sandbox::Docker)
        );
        // Another flag after a bare one is not taken as its value.
        let args = launch_args(&["claude", "--sandbox", "--model", "qwen3.8", "--", "hi"]);
        assert_eq!(args.sandbox, Some(Sandbox::Sbx));
        assert_eq!(args.model.as_deref(), Some("qwen3.8"));
        assert_eq!(args.extra_args, ["hi"]);
    }

    /// `--optimizer` defaults to none, and takes none, tk, cvmn or hdrm.
    #[test]
    fn the_optimizer_defaults_to_none_and_takes_none_tk_cvmn_or_hdrm() {
        assert_eq!(launch_args(&["claude"]).optimizer, Optimizer::None);
        assert_eq!(
            launch_args(&["claude", "--optimizer", "none"]).optimizer,
            Optimizer::None
        );
        assert_eq!(
            launch_args(&["claude", "--optimizer", "tk"]).optimizer,
            Optimizer::Tk
        );
        assert_eq!(
            launch_args(&["claude", "--optimizer=tk", "--", "-p", "hi"]).optimizer,
            Optimizer::Tk
        );
        assert_eq!(
            launch_args(&["claude", "--optimizer", "cvmn"]).optimizer,
            Optimizer::Cvmn
        );
        assert_eq!(
            launch_args(&["claude", "--optimizer", "hdrm"]).optimizer,
            Optimizer::Hdrm
        );
        // The flag is not an alias for anything after `--`.
        let args = launch_args(&["claude", "--", "--optimizer", "tk"]);
        assert_eq!(args.optimizer, Optimizer::None);
        assert_eq!(args.extra_args, ["--optimizer", "tk"]);
    }

    #[test]
    fn a_bad_optimizer_is_rejected_by_the_parser() {
        #[derive(clap::Parser, Debug)]
        struct Cli {
            #[command(flatten)]
            args: LaunchArgs,
        }
        for bad in ["TK", "turbo", "", "none,tk"] {
            let err = <Cli as clap::Parser>::try_parse_from(["l", "claude", "--optimizer", bad])
                .unwrap_err()
                .to_string();
            assert!(err.contains("--optimizer"), "{bad}: {err}");
        }
        // The help says what each choice does.
        let help = <Cli as clap::CommandFactory>::command()
            .render_long_help()
            .to_string();
        assert!(help.contains("--optimizer <OPTIMIZER>"), "{help}");
        assert!(help.contains("none"), "{help}");
        assert!(help.contains("tk"), "{help}");
        assert!(help.contains("cvmn"), "{help}");
        assert!(help.contains("hdrm"), "{help}");
        assert!(help.contains("[default: none]"), "{help}");
    }

    /// The refusals are `run`'s own errors, so the daemon is never started
    /// and no model is pulled: a launch that got past them would try both.
    #[test]
    fn tk_is_refused_for_other_integrations_before_anything_starts() {
        for name in [
            "codex",
            "aider",
            "goose",
            "cline",
            "gemini",
            "docker-agent",
            "copilot-cli",
        ] {
            let args = launch_args(&[name, "--model", "m", "--optimizer", "tk"]);
            let err = run(&args).unwrap_err().to_string();
            let canonical = canonical_integration(name).unwrap();
            assert!(
                err.contains(&format!("does not work with {canonical}")),
                "{name}: {err}"
            );
            assert!(
                err.contains("only claude, opencode can use it"),
                "{name}: {err}"
            );
        }
    }

    #[test]
    fn cvmn_is_refused_for_other_integrations_before_anything_starts() {
        let args = launch_args(&["aider", "--model", "m", "--optimizer", "cvmn"]);
        let err = run(&args).unwrap_err().to_string();
        assert!(err.contains("does not work with aider"), "{err}");
        assert!(
            err.contains("only claude, codex, opencode can use it"),
            "{err}"
        );
    }

    #[test]
    fn tk_is_refused_with_a_sandbox_before_sbx_is_reached() {
        for sandbox in [
            &["--sandbox"][..],
            &["--sandbox", "sbx"],
            &["--sandbox", "docker"],
        ] {
            let mut argv = vec!["claude", "--model", "m", "--optimizer", "tk"];
            argv.extend_from_slice(sandbox);
            let err = run(&launch_args(&argv)).unwrap_err().to_string();
            assert!(
                err.contains("--optimizer tk does not work with --sandbox"),
                "{err}"
            );
        }
    }

    #[test]
    fn tk_is_refused_beside_claude_flags_it_cannot_honor() {
        let args = launch_args(&[
            "claude",
            "--model",
            "m",
            "--optimizer",
            "tk",
            "--",
            "--settings",
            "mine.json",
        ]);
        let err = run(&args).unwrap_err().to_string();
        assert!(err.contains("--settings"), "{err}");
    }

    /// The optimizer is not one of the flags `[launch.<integration>]` fills.
    #[test]
    fn configured_defaults_leave_the_optimizer_alone() {
        let (args, _) = applied(&["claude", "--optimizer", "tk"], &sonnet());
        assert_eq!(args.optimizer, Optimizer::Tk);
        let (args, _) = applied(&["claude"], &sonnet());
        assert_eq!(args.optimizer, Optimizer::None);
    }

    /// `llmman launch <argv...>` as the command line parses it.
    fn launch_args(argv: &[&str]) -> LaunchArgs {
        #[derive(clap::Parser)]
        struct Cli {
            #[command(flatten)]
            args: LaunchArgs,
        }
        <Cli as clap::Parser>::try_parse_from(["l"].into_iter().chain(argv.iter().copied()))
            .unwrap()
            .args
    }

    /// `apply_defaults` for `argv` against `[launch.<integration>]`.
    fn applied(argv: &[&str], conf: &crate::config::LaunchConf) -> (LaunchArgs, Vec<String>) {
        let args = launch_args(argv);
        let name = args.integration.clone().unwrap();
        apply_defaults(&args, &name, || Ok(conf.clone())).unwrap()
    }

    fn sonnet() -> crate::config::LaunchConf {
        crate::config::launch_conf(Some("anthropic"), Some("claude-sonnet-5-5"), Some("xhigh"))
    }

    /// `launch opencode` alone is the full invocation, and says which
    /// flags it stands for.
    #[test]
    fn configured_defaults_fill_every_flag_the_command_line_left_out() {
        let (args, applied) = applied(&["opencode"], &sonnet());
        assert_eq!(args.provider.as_deref(), Some("anthropic"));
        assert_eq!(args.model.as_deref(), Some("claude-sonnet-5-5"));
        assert_eq!(args.variant.as_deref(), Some("xhigh"));
        assert_eq!(
            applied,
            [
                "--provider anthropic",
                "--model claude-sonnet-5-5",
                "--variant xhigh"
            ]
        );

        // Arguments for the integration itself are not a choice of model.
        let (args, _) = self::applied(
            &["opencode", "--sandbox", "docker", "--", "run", "hi"],
            &sonnet(),
        );
        assert_eq!(args.model.as_deref(), Some("claude-sonnet-5-5"));
        assert_eq!(args.extra_args, ["run", "hi"]);
    }

    #[test]
    fn nothing_configured_changes_nothing() {
        let none = crate::config::LaunchConf::default();
        let (args, applied) = applied(&["opencode", "-m", "qwen3.8", "--variant", "low"], &none);
        assert!(applied.is_empty());
        assert_eq!(args.model.as_deref(), Some("qwen3.8"));
        assert_eq!(args.variant.as_deref(), Some("low"));
        assert_eq!(args.provider, None);
    }

    /// `--variant` is the one flag that keeps the configured model.
    #[test]
    fn a_typed_variant_replaces_only_the_configured_variant() {
        let (args, applied) = applied(&["opencode", "--variant", "low"], &sonnet());
        assert_eq!(args.provider.as_deref(), Some("anthropic"));
        assert_eq!(args.model.as_deref(), Some("claude-sonnet-5-5"));
        assert_eq!(args.variant.as_deref(), Some("low"));
        assert_eq!(
            applied,
            ["--provider anthropic", "--model claude-sonnet-5-5"]
        );
    }

    #[test]
    fn a_typed_model_choice_turns_the_configured_set_off() {
        for argv in [
            &["opencode", "--model", "qwen3.8"][..],
            &["opencode", "-m", "qwen3.8"],
            &["opencode", "--provider", "openai"],
            &["opencode", "-p", "openai", "--model", "gpt-5"],
            &["opencode", "--overflow-provider", "openai"],
            &["opencode", "--overflow-model", "gpt-5"],
        ] {
            let typed = launch_args(argv);
            let (args, applied) = applied(argv, &sonnet());
            assert!(applied.is_empty(), "{argv:?}: {applied:?}");
            assert_eq!(args.provider, typed.provider, "{argv:?}");
            assert_eq!(args.model, typed.model, "{argv:?}");
            assert_eq!(args.variant, None, "{argv:?}");
        }
    }

    /// A `--model` after `--` wins over the configured one, as it does
    /// over a typed one, but the configured model is still there for the
    /// integrations that need one; its variant is not.
    #[test]
    fn a_forwarded_model_keeps_the_configured_model_but_not_its_variant() {
        let (args, applied) = applied(&["qwen", "--", "--model", "b"], &sonnet());
        assert_eq!(args.model.as_deref(), Some("claude-sonnet-5-5"));
        assert_eq!(args.variant, None);
        assert_eq!(
            applied,
            ["--provider anthropic", "--model claude-sonnet-5-5"]
        );

        // opencode has no such flag, so there it is a prompt's word.
        let (args, _) = self::applied(&["opencode", "--", "--model", "x"], &sonnet());
        assert_eq!(args.variant.as_deref(), Some("xhigh"));
    }

    /// Defaults that cannot be read are an error, but only where they
    /// would have been used.
    #[test]
    fn unreadable_defaults_fail_only_a_launch_that_needs_them() {
        let args = launch_args(&["opencode"]);
        let err = apply_defaults(&args, "opencode", || Err("bad file")).unwrap_err();
        assert!(err.to_string().contains("bad file"), "{err}");
        for argv in [&["opencode", "-m", "x"][..], &["opencode", "-p", "x"]] {
            let args = launch_args(argv);
            apply_defaults(&args, "opencode", || Err("bad file")).expect("not consulted");
        }
    }

    #[test]
    fn canonical_integration_resolves_case_and_aliases() {
        assert_eq!(canonical_integration("OpenCode"), Some("opencode"));
        assert_eq!(canonical_integration("copilot-cli"), Some("copilot"));
        assert_eq!(canonical_integration("nonesuch"), None);
        for i in INTEGRATIONS {
            assert_eq!(canonical_integration(i.name), Some(i.name));
        }
    }
}