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
//! `llmman.conf` — one config file, read once, shared by every consumer.
//!
//! Two locations, later overriding earlier:
//!
//!   1. `/etc/llmman/llmman.conf`        system-wide
//!   2. `~/.config/llmman/llmman.conf`   per-user
//!
//! The same paths on every platform, with no llmman-specific variable
//! to move them: one documented answer to "where does this go". (`~` is
//! `$HOME`, or `%USERPROFILE%` on Windows.)
//!
//! ```toml
//! [aliases]                            # crate::shortnames
//! gemma4 = "docker.io/ai/gemma4"
//!
//! [providers.openrouter]               # crate::providers
//! api_key = "sk-or-..."
//!
//! [providers.inferencebox]                   # a provider models.dev does not list
//! base_url = "http://inferencebox:8000/v1"
//! wire     = "openai"                  # default; or "anthropic"
//!
//! [verify]                             # crate::verify
//! default = "off"
//! layers  = "off"                      # hash stored layers on a cold load
//!
//! [[verify.trust]]
//! pattern = "docker.io/myorg/**"
//! keys    = ["keys/myorg.pub"]         # relative to this file's directory
//! mode    = "enforce"
//!
//! [[verify.trust]]
//! pattern = "mirror.corp/**"           # pulled reference selects the rule
//! claims  = "docker.io/myorg/**"       # repository the signature may name
//! keys    = ["keys/myorg.pub"]
//! mode    = "enforce"
//!
//! [aggregation]                        # cmd::serve::aggregation
//! peers = "asahi,spark:17434"
//! api_key = "..."                      # presented to peers; defaults to the first auth key
//!
//! [auth]                               # cmd::serve::auth
//! api_keys = "k1,k2"                   # what a request to this daemon must present
//!
//! [registries."docker.io"]             # crate::ffi, go-shim
//! mirrors = "https://mirror.gcr.io,registry-mirror.corp:5000"
//!
//! [launch.opencode]                    # cmd::launch: flags to use when none are typed
//! provider = "anthropic"
//! model    = "claude-sonnet-5-5"
//! variant  = "xhigh"
//! ```
//!
//! Parsing happens once, in [`files`]; what a *failed* parse means is
//! left to each consumer, because it differs. [`crate::verify`] refuses
//! to run, since a trust policy it cannot read must not quietly become
//! `off`; aliases and keys degrade to none. The error is reported once,
//! centrally, so one typo is not announced three times.

use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::sync::{OnceLock, PoisonError, RwLock};

use serde::Deserialize;

const FILE: &str = "llmman.conf";

/// Everything `llmman.conf` configures.
///
/// `deny_unknown_fields` throughout: a misspelling that parsed happily
/// would be a policy, alias or credential that silently never takes
/// effect, surfacing somewhere else entirely — an unsigned pull that
/// passes, a 401 inside someone else's TUI.
#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct Conf {
    #[serde(default)]
    pub core: CoreConf,
    #[serde(default)]
    pub pager: PagerConf,
    /// Short-name aliases — see [`crate::shortnames`].
    #[serde(default)]
    pub aliases: HashMap<String, String>,
    /// Private: a key leaves this module only through
    /// [`provider_api_key`], which gates it on the file's mode.
    #[serde(default)]
    providers: HashMap<String, ProviderConf>,
    /// Signature trust policy — see [`crate::verify`].
    #[serde(default)]
    pub verify: VerifyConf,
    /// Peer daemons — see `cmd::serve::aggregation`.
    #[serde(default)]
    pub aggregation: AggregationConf,
    /// Private, like `providers`: reached through [`auth_api_keys`] only.
    #[serde(default)]
    auth: AuthConf,
    /// Native OAuth forwarding on the daemon's TLS listener.
    #[serde(default)]
    pub managed: ManagedConf,
    /// `[registries."<host>"]` tables, keyed by the registry host a
    /// reference names — see [`registry_mirrors`].
    #[serde(default)]
    pub registries: HashMap<String, RegistryConf>,
    /// `[launch.<integration>]` tables, see [`launch_defaults`].
    #[serde(default)]
    pub launch: HashMap<String, LaunchConf>,
}

#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct CoreConf {
    #[serde(default)]
    pub pager: Option<String>,
}

#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct PagerConf {
    #[serde(default)]
    pub log: Option<String>,
    #[serde(default)]
    pub providers: Option<String>,
    #[serde(default)]
    pub list: Option<String>,
    #[serde(default)]
    pub search: Option<String>,
    #[serde(default)]
    pub ps: Option<String>,
}

pub(crate) fn pager(command: &str) -> (Option<String>, Option<String>) {
    pager_from_files(files().unwrap_or_default(), command)
}

fn pager_from_files(files: &[File], command: &str) -> (Option<String>, Option<String>) {
    let mut core = None;
    let mut specific = None;
    for file in files {
        if let Some(value) = &file.conf.core.pager {
            core = Some(value.clone());
        }
        let value = match command {
            "log" => &file.conf.pager.log,
            "providers" => &file.conf.pager.providers,
            "list" => &file.conf.pager.list,
            "search" => &file.conf.pager.search,
            "ps" => &file.conf.pager.ps,
            _ => continue,
        };
        if let Some(value) = value {
            specific = Some(value.clone());
        }
    }
    (core, specific)
}

/// The `[aggregation]` section.
#[derive(Deserialize, Default, Clone)]
#[serde(deny_unknown_fields)]
pub struct AggregationConf {
    /// Comma-separated `[scheme://]host[:port]`, as `LLMMAN_PEERS` — a
    /// string so `llmman config set aggregation.peers a,b` can write it.
    #[serde(default)]
    pub peers: Option<String>,
    /// Presented to peers, as `LLMMAN_PEER_API_KEY`; see [`peer_api_key`].
    #[serde(default)]
    api_key: Option<String>,
}

impl std::fmt::Debug for AggregationConf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AggregationConf")
            .field("peers", &self.peers)
            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
            .finish()
    }
}

/// The `[auth]` section — see `cmd::serve::auth`.
#[derive(Deserialize, Default, Clone)]
#[serde(deny_unknown_fields)]
struct AuthConf {
    /// Comma-separated keys a request must present, as `LLMMAN_API_KEYS`
    /// — a string so `llmman config set auth.api_keys a,b` can write it.
    #[serde(default)]
    api_keys: Option<String>,
}

/// The `[managed]` section. Strings work with `llmman config set`.
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct ManagedConf {
    /// Explicit opt-in; absent means disabled.
    #[serde(default)]
    pub enabled: Option<String>,
    /// Optional upstream idle-read limit; absent or zero means no limit.
    #[serde(default)]
    pub read_timeout_seconds: Option<String>,
}

/// Validated forwarding settings. No provider credentials are stored here.
#[derive(Default, Debug, Clone)]
pub struct Managed {
    /// Whether the daemon mounts the forwarding routes.
    pub enabled: bool,
    /// Maximum wait for each upstream read; no total request deadline.
    pub read_timeout: Option<std::time::Duration>,
}

/// Loads managed settings using the normal configuration precedence.
pub fn managed() -> Result<Managed, String> {
    managed_from_files(files()?)
}

fn managed_from_files(files: &[File]) -> Result<Managed, String> {
    let mut managed = Managed::default();
    for file in files {
        managed.apply(&file.conf.managed)?;
    }
    Ok(managed)
}

impl Managed {
    /// Shared by parse-time validation and field-by-field configuration merging.
    fn apply(&mut self, conf: &ManagedConf) -> Result<(), String> {
        if let Some(enabled) = &conf.enabled {
            self.enabled = enabled
                .parse()
                .map_err(|_| "managed.enabled must be \"true\" or \"false\"".to_string())?;
        }
        if let Some(seconds) = &conf.read_timeout_seconds {
            let seconds: u32 = seconds.parse().map_err(|_| {
                "managed.read_timeout_seconds must be an unsigned 32-bit integer".to_string()
            })?;
            self.read_timeout =
                (seconds != 0).then(|| std::time::Duration::from_secs(seconds.into()));
        }
        Ok(())
    }
}

impl std::fmt::Debug for AuthConf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AuthConf")
            .field("api_keys", &self.api_keys.as_ref().map(|_| "<redacted>"))
            .finish()
    }
}

/// One `[registries."<host>"]` table.
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct RegistryConf {
    /// Comma-separated `[scheme://]host[:port][/path]` mirrors, tried in
    /// order before the registry for pulls. A string, like `peers`, so
    /// `llmman config set` can write it.
    #[serde(default)]
    pub mirrors: Option<String>,
}

/// One `[launch.<integration>]` table: the flags `llmman launch` uses when
/// none are typed. A blank value clears one that an earlier file set.
#[derive(Deserialize, Default, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LaunchConf {
    /// As `--provider`.
    #[serde(default)]
    pub provider: Option<String>,
    /// As `--model`.
    #[serde(default)]
    pub model: Option<String>,
    /// As `--variant`.
    #[serde(default)]
    pub variant: Option<String>,
}

/// One `[providers.<id>]` table. Without `base_url` it keys a catalog
/// provider; with it, it *defines* one — an inference server at a host
/// models.dev does not list — and shadows any catalog entry with its id.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ProviderConf {
    #[serde(default)]
    api_key: Option<String>,
    /// Base URL the wire's route is appended to. Plain `http://` is
    /// allowed: the user typed it into their own owner-only file.
    #[serde(default)]
    base_url: Option<String>,
    /// What is spoken at `base_url`; `openai` by default.
    #[serde(default)]
    wire: Option<crate::providers::Wire>,
    /// Variable holding the key. With neither this nor `api_key`, no
    /// credential is sent — most local servers take none.
    #[serde(default)]
    api_key_env: Option<String>,
    /// Display name for listings; the id when absent.
    #[serde(default)]
    name: Option<String>,
}

/// Hand-written so the key cannot reach a log through the derived
/// `Debug` of the public [`Conf`] and [`File`]. Same reasoning as
/// `RemoteTarget` in `cmd::serve`.
impl std::fmt::Debug for ProviderConf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProviderConf")
            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
            .field("base_url", &self.base_url)
            .field("wire", &self.wire)
            .field("api_key_env", &self.api_key_env)
            .field("name", &self.name)
            .finish()
    }
}

impl ProviderConf {
    /// Whether this table defines a provider rather than only keying one.
    fn defines(&self) -> bool {
        self.base_url.is_some()
    }

    /// Fields that only mean something on a definition (see [`validate`]).
    fn definition_only_fields(&self) -> Vec<&'static str> {
        let mut set = Vec::new();
        if self.wire.is_some() {
            set.push("wire");
        }
        if self.api_key_env.is_some() {
            set.push("api_key_env");
        }
        if self.name.is_some() {
            set.push("name");
        }
        set
    }
}

/// A provider `llmman.conf` defines, merged across every file that
/// mentions it. What `crate::providers` turns into a `Provider`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConfiguredProvider {
    pub id: String,
    /// `name` from the file, else the id.
    pub name: String,
    /// Without its trailing slash, as `Provider::base_url` is kept.
    pub base_url: String,
    pub wire: crate::providers::Wire,
    /// `api_key_env`, when set. The `api_key` itself is not here: it is
    /// reached through [`provider_api_key`], behind the file-mode gate.
    pub key_env: Option<String>,
}

/// The `[verify]` section. [`crate::verify`] owns what these strings
/// mean; this module only spells out their shape.
#[derive(Deserialize, Default, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct VerifyConf {
    /// Mode for references no rule matches.
    #[serde(default)]
    pub default: Option<String>,
    /// Mode for hashing the stored manifest and layer blobs on a cold
    /// load. Independent of `default` and the trust rules: those speak
    /// about signatures, this about the bytes already on disk.
    #[serde(default)]
    pub layers: Option<String>,
    /// `[[verify.trust]]` entries, in the order they appear.
    #[serde(default)]
    pub trust: Vec<TrustConf>,
    /// Sigstore TrustedRoot JSON (Fulcio roots and Rekor log keys) that
    /// keyless rules, those with `issuer` or `subject`, are checked
    /// against. Relative to this file's directory, like `keys`.
    #[serde(default)]
    pub trusted_root: Option<String>,
}

/// One `[[verify.trust]]` entry.
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct TrustConf {
    pub pattern: String,
    #[serde(default)]
    pub keys: Vec<String>,
    #[serde(default)]
    pub mode: Option<String>,
    /// Glob the signature's claimed repository must match, in place of
    /// equality with the pulled one. Same grammar as `pattern`.
    #[serde(default)]
    pub claims: Option<String>,
    /// Keyless rule: the OIDC issuer the signing certificate must name.
    /// Exclusive with `keys`.
    #[serde(default)]
    pub issuer: Option<String>,
    /// Keyless rule: the identity (a certificate SAN) that must have
    /// signed. Exclusive with `keys`.
    #[serde(default)]
    pub subject: Option<String>,
}

impl Conf {
    /// Every entry that sets `api_key` at all, trimmed, by provider id.
    ///
    /// A blank value is kept here rather than dropped, so a user file can
    /// blank out a key `/etc` set — otherwise there is no way to opt out
    /// of a system-wide credential. [`provider_keys_in`] drops blanks
    /// after merging, since sending `Authorization: Bearer ` upstream
    /// would turn a clear "no API key" into someone else's 401.
    pub(crate) fn provider_keys(&self) -> HashMap<String, String> {
        self.providers
            .iter()
            .filter_map(|(id, p)| Some((id.clone(), p.api_key.as_deref()?.trim().to_string())))
            .collect()
    }
}

/// One `llmman.conf` that exists on disk.
#[derive(Debug)]
pub struct File {
    /// Where it was read from.
    pub path: PathBuf,
    /// Its own directory — what relative paths inside it resolve
    /// against, so a trust policy can ship alongside its keys.
    pub dir: PathBuf,
    pub conf: Conf,
}

// ---------------------------------------------------------------------------
// Search paths
// ---------------------------------------------------------------------------

/// Every place [`FILE`] may live, in ascending priority order.
///
/// Not the `/usr/share/llmman/`, `<binary>/../share/llmman/` and
/// `<binary-dir>/` tiers earlier versions searched: nothing ever shipped
/// a file to them, and they are package-managed and world-readable,
/// which `llmman.conf` cannot be.
pub fn search_paths() -> Vec<PathBuf> {
    let mut paths = vec![system_dir().join(FILE)];
    paths.extend(user_dir().map(|d| d.join(FILE)));
    paths
}

/// `/etc/llmman`.
#[cfg(not(windows))]
fn system_dir() -> PathBuf {
    PathBuf::from("/etc/llmman")
}

/// `/etc/llmman` anchored to the system drive.
///
/// A leading `/` is drive-*relative* on Windows, so a bare `/etc/llmman`
/// would mean `D:\etc\llmman` for a process launched from `D:` — the
/// system-wide location would move with the working directory.
#[cfg(windows)]
fn system_dir() -> PathBuf {
    let drive = std::env::var("SystemDrive").unwrap_or_else(|_| "C:".to_string());
    PathBuf::from(format!("{drive}\\etc\\llmman"))
}

/// `~/.config/llmman`, on every platform. No llmman-specific override;
/// `~` is the home directory below.
fn user_dir() -> Option<PathBuf> {
    home_dir().map(|h| h.join(".config").join("llmman"))
}

/// The home directory `~` names, here and for the integrations
/// `cmd::launch` configures. The environment comes first because
/// `dirs::home_dir` reads the Windows known-folder API, which ignores
/// the home a session set for every other program in it — node's
/// `os.homedir()` included, which is what Cline and pi resolve through.
pub(crate) fn home_dir() -> Option<PathBuf> {
    env_home().or_else(dirs::home_dir)
}

/// The variables that name the home directory. `%USERPROFILE%` is what
/// a Windows session sets and what node's `os.homedir()` reads; `HOME`
/// is not consulted there, because a POSIX shell exports it in a form
/// Windows reads as drive-relative.
const HOME_KEYS: &[&str] = if cfg!(windows) {
    &["USERPROFILE"]
} else {
    &["HOME"]
};

/// The first of [`HOME_KEYS`] that names an absolute path. A relative
/// or empty one would anchor the config on the current drive rather
/// than at a home directory.
fn env_home() -> Option<PathBuf> {
    env_home_from(HOME_KEYS, |key| std::env::var_os(key))
}

/// Split from [`env_home`] so the rule can be asserted without changing
/// the process environment.
fn env_home_from(
    keys: &[&str],
    lookup: impl Fn(&str) -> Option<std::ffi::OsString>,
) -> Option<PathBuf> {
    keys.iter()
        .filter_map(|key| lookup(key))
        .map(PathBuf::from)
        .find(|value| value.is_absolute())
}

/// Where a user's own config belongs, for a message that has to name
/// it or a [`crate::cmd::config`] that has to write it. `None` only when
/// there is no home directory.
pub fn user_path() -> Option<PathBuf> {
    user_dir().map(|d| d.join(FILE))
}

/// Where the system-wide config belongs. Always known, unlike
/// [`user_path`]: it does not depend on there being a home directory.
pub fn system_path() -> PathBuf {
    system_dir().join(FILE)
}

/// `key` as one part of a dotted TOML key, quoted unless it is bare: an
/// id like `wafer.ai` would otherwise name two nested tables.
pub fn toml_key(key: &str) -> String {
    toml_edit::Key::new(key).display_repr().into_owned()
}

// ---------------------------------------------------------------------------
// Loading
// ---------------------------------------------------------------------------

/// Every `llmman.conf` that exists, lowest priority first, parsed once
/// for the process.
///
/// `Err` when one is there but unreadable or malformed — see the module
/// docs for why what to do about that is the caller's call.
pub fn files() -> Result<&'static [File], &'static str> {
    cache().as_deref().map_err(String::as_str)
}

fn cache() -> &'static Result<Vec<File>, String> {
    static CACHE: OnceLock<Result<Vec<File>, String>> = OnceLock::new();
    CACHE.get_or_init(|| {
        let loaded = load();
        // Once, here, rather than by each of the three consumers.
        if let Err(e) = &loaded {
            eprintln!("[llmman] warning: {e}");
        }
        loaded
    })
}

fn load() -> Result<Vec<File>, String> {
    // No unit test may depend on whoever runs it having an llmman.conf:
    // their aliases would redirect a fixture reference, and a provider
    // called `openai` would resolve their real key, flipping the
    // assertions in `cmd::serve` and `cmd::providers` that prove no key
    // leaks. `parse`, `provider_keys` and `Policy::parse` are covered
    // directly instead. The binary the e2e tests spawn is built without
    // `cfg(test)`, so it reads the real files.
    if cfg!(test) {
        return Ok(Vec::new());
    }
    load_from(search_paths())
}

/// The files among `paths` that exist, parsed. Split out from [`load`]
/// so a test can point it at a temporary directory.
fn load_from(paths: Vec<PathBuf>) -> Result<Vec<File>, String> {
    let mut files = Vec::new();
    for path in paths {
        let text = match std::fs::read_to_string(&path) {
            Ok(text) => text,
            // Absence is the common case: most machines never have one.
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => return Err(format!("ignoring {}: {e}", path.display())),
        };
        let conf = parse(&text).map_err(|e| format!("ignoring {}: {e}", path.display()))?;
        let dir = path.parent().unwrap_or(Path::new(".")).to_path_buf();
        files.push(File { path, dir, conf });
    }
    Ok(files)
}

/// Parse one `llmman.conf`. Split out from [`load`] so the format is
/// testable without a file, a home directory, or a particular umask.
pub(crate) fn parse(text: &str) -> Result<Conf, String> {
    let conf: Conf = toml::from_str(text).map_err(|e| e.to_string())?;
    validate(&conf)?;
    Ok(conf)
}

/// What the TOML shape alone cannot say, checked at parse time so
/// `llmman config set` refuses it on the spot.
fn validate(conf: &Conf) -> Result<(), String> {
    Managed::default().apply(&conf.managed)?;
    let mut seen: HashMap<String, &str> = HashMap::new();
    for (host, r) in &conf.registries {
        let canonical = canonical_registry_host(host).ok_or_else(|| {
            format!(
                "[registries.{host:?}]: expected a host[:port], as in [registries.\"docker.io\"]"
            )
        })?;
        if let Some(other) = seen.insert(canonical, host) {
            return Err(format!(
                "[registries.{host:?}] and [registries.{other:?}] name the same registry"
            ));
        }
        for mirror in split_list(r.mirrors.as_deref()) {
            mirror_url_of(&mirror).map_err(|e| format!("[registries.{host:?}] mirrors: {e}"))?;
        }
    }
    validate_launch(&conf.launch)?;
    for (id, p) in &conf.providers {
        // `<provider>/<model>` is how a reference travels (see
        // `crate::providers::split_remote_ref`), so a slash in the id
        // could never be routed.
        if id.is_empty() || id.contains('/') {
            return Err(format!(
                "[providers.{id:?}]: a provider id cannot be empty or contain '/'"
            ));
        }
        match &p.base_url {
            Some(url) => {
                base_url_of(url).map_err(|e| format!("[providers.{id}] base_url: {e}"))?;
            }
            None => {
                let only_on_definition = p.definition_only_fields();
                if !only_on_definition.is_empty() {
                    return Err(format!(
                        "[providers.{id}] sets {} but no base_url, so it only keys the catalog \
                         provider {id:?}",
                        only_on_definition.join(", ")
                    ));
                }
            }
        }
    }
    Ok(())
}

/// Each table must name an integration (once) and hold a variant that
/// `--variant` takes; a misspelling would silently never apply.
fn validate_launch(launch: &HashMap<String, LaunchConf>) -> Result<(), String> {
    let mut seen: HashMap<&str, &str> = HashMap::new();
    for (name, l) in launch {
        let canonical = crate::cmd::launch::canonical_integration(name).ok_or_else(|| {
            format!(
                "[launch.{name:?}]: not an integration `llmman launch` knows; \
                 run `llmman launch` to list them"
            )
        })?;
        // `copilot` and its alias `copilot-cli` are one integration.
        if let Some(other) = seen.insert(canonical, name) {
            return Err(format!(
                "[launch.{name:?}] and [launch.{other:?}] name the same integration"
            ));
        }
        if let Some(variant) = set_value(l.variant.as_deref()) {
            if !crate::cmd::run::variants().any(|v| v == variant) {
                return Err(format!(
                    "[launch.{name}] variant: {variant:?} is not one of {}",
                    crate::cmd::run::variants().collect::<Vec<_>>().join(", ")
                ));
            }
        }
    }
    Ok(())
}

/// `value` trimmed, `None` when absent or blank.
fn set_value(value: Option<&str>) -> Option<&str> {
    value.map(str::trim).filter(|v| !v.is_empty())
}

/// Parses an `http`/`https` URL with a host and no userinfo, query or
/// fragment. Userinfo is refused because these URLs are reported and
/// logged; `credential` names where a credential goes instead.
fn plain_http_url(url: &str, credential: &str) -> Result<reqwest::Url, String> {
    let parsed = reqwest::Url::parse(url).map_err(|e| format!("{url:?}: {e}"))?;
    match parsed.scheme() {
        "http" | "https" => {}
        other => {
            return Err(format!(
                "{url:?}: scheme must be http or https, not {other}"
            ))
        }
    }
    if parsed.host_str().is_none() {
        return Err(format!("{url:?}: no host"));
    }
    if !parsed.username().is_empty() || parsed.password().is_some() {
        return Err(format!(
            "{url:?}: cannot carry a username or password; use {credential}"
        ));
    }
    if parsed.query().is_some() || parsed.fragment().is_some() {
        return Err(format!("{url:?}: no query or fragment allowed"));
    }
    Ok(parsed)
}

/// Normalizes a configured `base_url`: lowercased scheme and host, no
/// trailing slash.
fn base_url_of(url: &str) -> Result<String, String> {
    let parsed = plain_http_url(url.trim(), "api_key")?;
    Ok(parsed.as_str().trim_end_matches('/').to_string())
}

/// Normalizes a mirror, `[scheme://]host[:port][/path]`, `https` when no
/// scheme is given. A path is kept (a mirror served under a prefix);
/// `llmman login <mirror-host>` is where a mirror's credential goes.
fn mirror_url_of(mirror: &str) -> Result<String, String> {
    let mirror = mirror.trim();
    if mirror.is_empty() {
        return Err("an empty entry".to_string());
    }
    let with_scheme = if mirror.contains("://") {
        mirror.to_string()
    } else {
        format!("https://{mirror}")
    };
    let parsed = plain_http_url(&with_scheme, "`llmman login`")?;
    Ok(parsed.as_str().trim_end_matches('/').to_string())
}

/// The `host[:port]` a `[registries."<host>"]` key names, as the Go shim
/// looks it up: lowercased, Docker Hub's several names folded onto
/// `docker.io`. `None` for anything that is not a bare authority.
fn canonical_registry_host(host: &str) -> Option<String> {
    let host = host.trim();
    // `*` is a wildcard in registries.conf, which the url crate lets through.
    if host.is_empty() || host.contains("://") || host.contains(['/', '*']) {
        return None;
    }
    let parsed = plain_http_url(&format!("https://{host}"), "").ok()?;
    if parsed.path() != "/" {
        return None;
    }
    let mut canonical = parsed.host_str()?.to_string();
    if let Some(port) = parsed.port() {
        canonical = format!("{canonical}:{port}");
    }
    Some(match canonical.as_str() {
        "index.docker.io" | "registry-1.docker.io" => "docker.io".to_string(),
        _ => canonical,
    })
}

/// Splits a comma-separated setting into its trimmed, non-empty entries.
fn split_list(value: Option<&str>) -> Vec<String> {
    value
        .unwrap_or_default()
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

// ---------------------------------------------------------------------------
// Registry mirrors
// ---------------------------------------------------------------------------

/// Mirrors per registry host, normalized and in order: for Docker Hub
/// `LLMMAN_REGISTRY_MIRRORS` when set (even empty; Hub-only, like
/// dockerd's `--registry-mirror`), else the last file that sets the
/// host's `mirrors`. Replaced per host, not merged, so `mirrors = ""`
/// opts a host out. What a mirror does is the Go shim's business (see
/// go-shim/registry_mirrors.go).
pub fn registry_mirrors() -> BTreeMap<String, Vec<String>> {
    registry_mirrors_from(
        std::env::var("LLMMAN_REGISTRY_MIRRORS").ok().as_deref(),
        files().unwrap_or_default(),
    )
}

/// Whether `host`, as a reference spells it, has mirrors configured.
/// Cached for the process; `crate::hf` asks per reference classified.
pub fn has_registry_mirrors(host: &str) -> bool {
    static CACHE: OnceLock<BTreeMap<String, Vec<String>>> = OnceLock::new();
    canonical_registry_host(host)
        .is_some_and(|host| CACHE.get_or_init(registry_mirrors).contains_key(&host))
}

fn registry_mirrors_from(env: Option<&str>, files: &[File]) -> BTreeMap<String, Vec<String>> {
    let mut by_host: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for file in files {
        for (host, r) in &file.conf.registries {
            let Some(mirrors) = r.mirrors.as_deref() else {
                continue;
            };
            let host = canonical_registry_host(host).expect("validated at parse");
            let mirrors = split_list(Some(mirrors))
                .iter()
                .map(|m| mirror_url_of(m).expect("validated at parse"))
                .collect();
            by_host.insert(host, mirrors);
        }
    }
    if let Some(env) = env {
        let mut mirrors = Vec::new();
        for m in split_list(Some(env)) {
            match mirror_url_of(&m) {
                Ok(m) => mirrors.push(m),
                Err(e) => eprintln!("[llmman] warning: ignoring LLMMAN_REGISTRY_MIRRORS entry {e}"),
            }
        }
        by_host.insert("docker.io".to_string(), mirrors);
    }
    by_host.retain(|_, mirrors| !mirrors.is_empty());
    by_host
}

// ---------------------------------------------------------------------------
// Launch defaults
// ---------------------------------------------------------------------------

/// The `[launch.<integration>]` defaults for `integration` (any case, or
/// an alias), merged one key at a time with later files overriding.
///
/// `Err` when a file does not parse: the defaults cannot be known, and
/// running without them would silently launch another model.
pub fn launch_defaults(integration: &str) -> Result<LaunchConf, &'static str> {
    Ok(launch_defaults_from(files()?, integration))
}

fn launch_defaults_from(files: &[File], integration: &str) -> LaunchConf {
    let wanted = crate::cmd::launch::canonical_integration(integration);
    let mut merged = LaunchConf::default();
    for conf in files.iter().filter_map(|file| {
        // At most one table per integration: see `validate_launch`.
        file.conf
            .launch
            .iter()
            .find(|(name, _)| crate::cmd::launch::canonical_integration(name) == wanted)
            .map(|(_, conf)| conf)
    }) {
        for (field, value) in [
            (&mut merged.provider, &conf.provider),
            (&mut merged.model, &conf.model),
            (&mut merged.variant, &conf.variant),
        ] {
            if value.is_some() {
                *field = set_value(value.as_deref()).map(str::to_string);
            }
        }
    }
    merged
}

#[cfg(test)]
pub(crate) fn launch_conf(
    provider: Option<&str>,
    model: Option<&str>,
    variant: Option<&str>,
) -> LaunchConf {
    let own = |v: Option<&str>| v.map(str::to_string);
    LaunchConf {
        provider: own(provider),
        model: own(model),
        variant: own(variant),
    }
}

// ---------------------------------------------------------------------------
// Configured providers
// ---------------------------------------------------------------------------

/// Every provider `llmman.conf` defines, sorted by id. Read once for the
/// process, like the keys.
pub fn configured_providers() -> &'static [ConfiguredProvider] {
    static CACHE: OnceLock<Vec<ConfiguredProvider>> = OnceLock::new();
    CACHE.get_or_init(|| configured_from(files().unwrap_or_default()))
}

/// Merged field by field, later files overriding earlier, as keys do.
/// Split out to be testable without a filesystem.
fn configured_from(files: &[File]) -> Vec<ConfiguredProvider> {
    let mut merged: HashMap<&str, ConfiguredProvider> = HashMap::new();
    for file in files {
        for (id, p) in file.conf.providers.iter().filter(|(_, p)| p.defines()) {
            let base_url = p
                .base_url
                .as_deref()
                .and_then(|u| base_url_of(u).ok())
                .expect("validated at parse");
            let entry = merged.entry(id).or_insert_with(|| ConfiguredProvider {
                id: id.clone(),
                name: id.clone(),
                base_url: String::new(),
                wire: crate::providers::Wire::OpenAi,
                key_env: None,
            });
            entry.base_url = base_url;
            if let Some(wire) = p.wire {
                entry.wire = wire;
            }
            // Blank clears a variable an earlier file named, as `api_key = ""`
            // clears a key.
            if let Some(var) = p.api_key_env.as_deref().map(str::trim) {
                entry.key_env = (!var.is_empty()).then(|| var.to_string());
            }
            if let Some(name) = p.name.as_deref().map(str::trim).filter(|n| !n.is_empty()) {
                entry.name = name.to_string();
            }
        }
    }
    let mut out: Vec<ConfiguredProvider> = merged.into_values().collect();
    out.sort_by(|a, b| a.id.cmp(&b.id));
    out
}

// ---------------------------------------------------------------------------
// Provider API keys
// ---------------------------------------------------------------------------

/// The key `llmman.conf` configures for `provider_id`, or `None`. Keyed
/// by the id `llmman providers` prints, not by the environment variable,
/// which is models.dev's naming rather than llmman's.
///
/// From a process-lifetime cache, so a daemon serving requests is not
/// stat'ing `/etc` per token; [`reload_provider_keys`] refreshes it after
/// `PUT /llmman/providers/:id/key` writes one.
pub fn provider_api_key(provider_id: &str) -> Option<String> {
    if let Some(keys) = PROVIDER_KEYS
        .read()
        .unwrap_or_else(PoisonError::into_inner)
        .as_ref()
    {
        return keys.get(provider_id).cloned();
    }
    PROVIDER_KEYS
        .write()
        .unwrap_or_else(PoisonError::into_inner)
        .get_or_insert_with(|| provider_keys_in(files().unwrap_or_default()))
        .get(provider_id)
        .cloned()
}

static PROVIDER_KEYS: RwLock<Option<HashMap<String, String>>> = RwLock::new(None);

/// Re-reads the provider keys of every `llmman.conf`, so a key the daemon
/// just wrote is spent from the next request on, without a restart. The
/// other settings (aliases, providers, policy) keep their startup values.
///
/// Only `PUT|DELETE /llmman/providers/:id/key` call this: a key written
/// by `llmman config set` or a hand edit reaches a running daemon at its
/// next start, as every other setting does.
///
/// `Err` leaves the keys in force: a file broken by a hand edit must not
/// switch every provider off. The message can quote the file, keys and
/// all, so it is for the daemon's log, not a client.
pub fn reload_provider_keys() -> Result<(), String> {
    reload_provider_keys_from(provider_key_paths())
}

#[cfg(not(test))]
fn provider_key_paths() -> Vec<PathBuf> {
    search_paths()
}

/// Tests read no real config: only the files a test names here.
#[cfg(test)]
fn provider_key_paths() -> Vec<PathBuf> {
    TEST_PROVIDER_FILES
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .clone()
}

/// The file `PUT|DELETE /llmman/providers/:id/key` writes: the per-user
/// llmman.conf, as `llmman config set` does.
#[cfg(not(test))]
pub fn provider_key_file() -> Option<PathBuf> {
    user_path()
}

#[cfg(test)]
pub fn provider_key_file() -> Option<PathBuf> {
    provider_key_paths().last().cloned()
}

/// The config files a test reads provider keys from, and writes them to.
#[cfg(test)]
pub(crate) static TEST_PROVIDER_FILES: std::sync::Mutex<Vec<PathBuf>> =
    std::sync::Mutex::new(Vec::new());

/// Held by every test that reloads or reads the provider-key cache:
/// it is one process-wide value, and a reload in one test would
/// otherwise change what another reads mid-assertion.
#[cfg(test)]
pub(crate) static PROVIDER_KEYS_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn reload_provider_keys_from(paths: Vec<PathBuf>) -> Result<(), String> {
    let files = load_from(paths)?;
    *PROVIDER_KEYS
        .write()
        .unwrap_or_else(PoisonError::into_inner) = Some(provider_keys_in(&files));
    Ok(())
}

fn provider_keys_in(files: &[File]) -> HashMap<String, String> {
    let mut accepted = Vec::new();
    for file in files {
        let keys = file.conf.provider_keys();
        // The mode gate is for a file that holds a secret. One that only
        // blanks a key out does not.
        if !keys.values().all(String::is_empty) {
            if let Err(e) = owner_readable_only(&file.path) {
                eprintln!(
                    "[llmman] warning: ignoring the API keys in {}: {e}",
                    file.path.display()
                );
                continue;
            }
        }
        accepted.push(keys);
    }
    merge_keys(accepted)
}

/// Merge per-file key tables, lowest priority first, then drop blanks.
///
/// Blanks survive the merge so a user file can shadow a key `/etc` set,
/// and are dropped only at the end so none is ever spent. Split out to
/// be testable without a filesystem.
fn merge_keys(per_file: Vec<HashMap<String, String>>) -> HashMap<String, String> {
    let mut merged: HashMap<String, String> = HashMap::new();
    for keys in per_file {
        merged.extend(keys);
    }
    merged.retain(|_, key| !key.is_empty());
    merged
}

// ---------------------------------------------------------------------------
// Aggregation peers
// ---------------------------------------------------------------------------

/// Peer daemons, as written: `LLMMAN_PEERS` when set (even empty), else
/// the last file that sets `aggregation.peers`. Replaced, not merged —
/// half of two aggregations is no one's intent — so `peers = ""` opts out.
pub fn peers() -> Vec<String> {
    peers_from(
        std::env::var("LLMMAN_PEERS").ok().as_deref(),
        files().unwrap_or_default(),
    )
}

fn peers_from(env: Option<&str>, files: &[File]) -> Vec<String> {
    split_list(env.or_else(|| {
        files
            .iter()
            .rev()
            .find_map(|f| f.conf.aggregation.peers.as_deref())
    }))
}

// ---------------------------------------------------------------------------
// Daemon API keys
// ---------------------------------------------------------------------------

/// The keys `llmman serve` requires: `LLMMAN_API_KEYS` when set (even
/// empty), else the last file setting `auth.api_keys` that passes the
/// mode gate. Replaced, not merged, like [`peers`].
pub fn auth_api_keys() -> Vec<String> {
    split_list(
        std::env::var("LLMMAN_API_KEYS")
            .ok()
            .or_else(|| {
                secret_from_files(files().unwrap_or_default(), "auth.api_keys", |f| {
                    f.conf.auth.api_keys.as_deref()
                })
            })
            .as_deref(),
    )
}

/// The key presented to peers: `LLMMAN_PEER_API_KEY`, else the last file
/// setting `aggregation.api_key` that passes the mode gate. `None` for blank.
pub fn peer_api_key() -> Option<String> {
    std::env::var("LLMMAN_PEER_API_KEY")
        .ok()
        .or_else(|| {
            secret_from_files(files().unwrap_or_default(), "aggregation.api_key", |f| {
                f.conf.aggregation.api_key.as_deref()
            })
        })
        .map(|k| k.trim().to_string())
        .filter(|k| !k.is_empty())
}

/// The last file that sets a secret field, behind [`provider_keys_in`]'s
/// mode gate: a loose file is skipped with a warning. A blank is returned
/// as such — it is how a user file clears a system-wide value.
fn secret_from_files<'a>(
    files: &'a [File],
    field: &str,
    pick: impl Fn(&'a File) -> Option<&'a str>,
) -> Option<String> {
    for file in files.iter().rev() {
        let Some(value) = pick(file) else { continue };
        if !value.trim().is_empty() {
            if let Err(e) = owner_readable_only(&file.path) {
                eprintln!(
                    "[llmman] warning: ignoring {field} in {}: {e}",
                    file.path.display()
                );
                continue;
            }
        }
        return Some(value.to_string());
    }
    None
}

/// Refuses a file that group or other can read, the way `ssh` refuses a
/// loose private key.
///
/// Gates the keys, not the read: `/etc/llmman/llmman.conf` is
/// legitimately world-readable for the aliases and trust policy it also
/// carries, so a loose file still supplies everything but the key.
#[cfg(unix)]
pub(crate) fn owner_readable_only(path: &Path) -> Result<(), String> {
    use std::os::unix::fs::PermissionsExt;

    let mode = std::fs::metadata(path)
        .map_err(|e| e.to_string())?
        .permissions()
        .mode();
    if mode & 0o077 != 0 {
        return Err(format!(
            "mode {:04o} lets other users read it. Run: chmod 600 {}",
            mode & 0o7777,
            path.display()
        ));
    }
    Ok(())
}

/// Unchecked on Windows: there are no mode bits, and reading an ACL
/// needs a `windows` dependency this crate does not have. Defaults are
/// owner-only under the user's profile, but a file with a widened ACL,
/// or one under the system directory, is accepted as-is.
#[cfg(not(unix))]
pub(crate) fn owner_readable_only(_path: &Path) -> Result<(), String> {
    Ok(())
}

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

    /// A key written while the daemon runs is spent from the next
    /// request on: the reload re-reads the files, behind the same mode
    /// gate as at startup.
    #[cfg(unix)]
    #[test]
    fn reloaded_provider_keys_take_effect_without_a_restart() {
        use std::os::unix::fs::PermissionsExt;

        let _one = PROVIDER_KEYS_TEST_LOCK
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        let id = "llmman-test-reload";
        let dir = std::env::temp_dir().join(format!("llmman-reload-{}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("temp dir");
        let path = dir.join(FILE);
        std::fs::write(&path, format!("[providers.{id}]\napi_key = \"sk-new\"\n")).expect("write");

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).expect("chmod");
        reload_provider_keys_from(vec![path.clone()]).expect("reload");
        assert_eq!(
            provider_api_key(id),
            None,
            "a world-readable file's key is ignored"
        );

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).expect("chmod");
        reload_provider_keys_from(vec![path.clone()]).expect("reload");
        assert_eq!(provider_api_key(id).as_deref(), Some("sk-new"));

        // A file that no longer parses: the error, and the keys in force stay.
        std::fs::write(&path, format!("[providers.{id}]\napi_key = \"sk-broken\n")).expect("write");
        assert!(reload_provider_keys_from(vec![path.clone()]).is_err());
        assert_eq!(provider_api_key(id).as_deref(), Some("sk-new"));

        reload_provider_keys_from(Vec::new()).expect("reload");
        assert_eq!(provider_api_key(id), None);
        std::fs::remove_dir_all(&dir).ok();
    }

    fn conf(text: &str) -> Conf {
        parse(text).expect("valid conf")
    }

    #[test]
    fn pager_configuration_merges_each_key_independently() {
        let system = file(
            r#"[core]
pager = "less -S"
[pager]
providers = "false"
log = "more""#,
        );
        let user = file(
            r#"[pager]
log = """#,
        );
        assert_eq!(
            pager_from_files(&[system, user], "log"),
            (Some("less -S".into()), Some("".into()))
        );
        let system = file(
            r#"[core]
pager = "less"
[pager]
providers = "false""#,
        );
        let user = file(
            r#"[core]
pager = "cat""#,
        );
        assert_eq!(
            pager_from_files(&[system, user], "providers"),
            (Some("cat".into()), Some("false".into()))
        );
        assert!(parse(
            r#"[core]
pagr = "less""#
        )
        .is_err());
        assert!(parse(
            r#"[pager]
provider = "false""#
        )
        .is_err());
    }

    #[test]
    fn pager_settings_support_model_listing_commands() {
        let settings = file(
            r#"[pager]
list = "yes"
search = "less -S"
ps = "off""#,
        );
        for (command, value) in [("list", "yes"), ("search", "less -S"), ("ps", "off")] {
            assert_eq!(
                pager_from_files(std::slice::from_ref(&settings), command),
                (None, Some(value.into()))
            );
        }
    }

    /// `$HOME` is what `dirs::home_dir` reads on Unix anyway, so this
    /// pins the Windows half: `%USERPROFILE%`, which it ignores.
    #[test]
    fn a_home_comes_from_the_environment_when_it_is_absolute() {
        let expected: &[&str] = if cfg!(windows) {
            &["USERPROFILE"]
        } else {
            &["HOME"]
        };
        assert_eq!(HOME_KEYS, expected);

        let take = |value: &str| {
            let value = value.to_string();
            env_home_from(HOME_KEYS, move |_| {
                Some(std::ffi::OsString::from(value.clone()))
            })
        };
        let home = if cfg!(windows) {
            "C:\\Users\\u"
        } else {
            "/home/u"
        };
        assert_eq!(take(home), Some(PathBuf::from(home)));
        assert_eq!(take(""), None);
        assert_eq!(take("relative/home"), None);
        // What a POSIX shell exports to a Windows binary: rooted, but
        // not absolute, so it would follow the current drive.
        #[cfg(windows)]
        assert_eq!(take("/c/Users/u"), None);
        assert_eq!(env_home_from(HOME_KEYS, |_| None), None);
    }

    /// A key must not reach a log through the derived `Debug` of the
    /// public `Conf`/`File`.
    #[test]
    fn debug_output_never_carries_a_key() {
        let c = conf("[providers.openai]\napi_key = \"sk-secret-value\"");
        let rendered = format!("{c:?}");
        assert!(!rendered.contains("sk-secret-value"), "{rendered}");
        assert!(rendered.contains("redacted"), "{rendered}");
    }

    /// The documented shape, all three sections in one file.
    #[test]
    fn parses_every_section_of_one_file() {
        let c = conf(
            r#"
            [aliases]
            gemma4 = "docker.io/ai/gemma4"

            [providers.openrouter]
            api_key = "sk-or-abc"

            [verify]
            default = "warn"
            trusted_root = "sigstore/trusted_root.json"

            [[verify.trust]]
            pattern = "docker.io/myorg/**"
            keys    = ["keys/myorg.pub"]
            mode    = "enforce"
            claims  = "docker.io/myorg/**"

            [[verify.trust]]
            pattern = "ghcr.io/myorg/**"
            issuer  = "https://token.actions.githubusercontent.com"
            subject = "https://github.com/myorg/models/.github/workflows/sign.yml@refs/heads/main"
            mode    = "enforce"

            [aggregation]
            peers = "asahi,spark:17434"

            [registries."docker.io"]
            mirrors = "https://mirror.gcr.io,registry-mirror.corp:5000"
            "#,
        );
        assert_eq!(
            c.aliases.get("gemma4").map(String::as_str),
            Some("docker.io/ai/gemma4")
        );
        assert_eq!(c.aggregation.peers.as_deref(), Some("asahi,spark:17434"));
        assert_eq!(
            c.registries["docker.io"].mirrors.as_deref(),
            Some("https://mirror.gcr.io,registry-mirror.corp:5000")
        );
        assert_eq!(
            c.provider_keys().get("openrouter").map(String::as_str),
            Some("sk-or-abc")
        );
        assert_eq!(c.verify.default.as_deref(), Some("warn"));
        assert_eq!(
            c.verify.trusted_root.as_deref(),
            Some("sigstore/trusted_root.json")
        );
        assert_eq!(c.verify.trust.len(), 2);
        assert_eq!(c.verify.trust[0].mode.as_deref(), Some("enforce"));
        assert_eq!(
            c.verify.trust[0].claims.as_deref(),
            Some("docker.io/myorg/**")
        );
        assert!(c.verify.trust[0].issuer.is_none());
        assert!(c.verify.trust[0].subject.is_none());
        assert!(c.verify.trust[1].keys.is_empty());
        assert_eq!(
            c.verify.trust[1].issuer.as_deref(),
            Some("https://token.actions.githubusercontent.com")
        );
        assert_eq!(
            c.verify.trust[1].subject.as_deref(),
            Some("https://github.com/myorg/models/.github/workflows/sign.yml@refs/heads/main")
        );
    }

    /// A file that sets one section must not need the other two.
    #[test]
    fn every_section_is_optional() {
        let c = conf("");
        assert!(c.aliases.is_empty());
        assert!(c.provider_keys().is_empty());
        assert!(c.verify.default.is_none());
        assert!(c.verify.trust.is_empty());

        assert!(!conf("[aliases]\nx = \"y\"").aliases.is_empty());
        assert!(conf("[verify]\ndefault = \"off\"").aliases.is_empty());
    }

    /// A key is trimmed; one that sets no `api_key` at all is absent.
    /// A blank *is* carried this far — see [`merge_keys`].
    #[test]
    fn a_key_is_trimmed_and_an_unset_one_is_absent() {
        let keys = conf(
            r#"
            [providers.openai]
            api_key = "  sk-padded  "

            [providers.groq]
            api_key = "   "

            [providers.mistral]
            "#,
        )
        .provider_keys();
        assert_eq!(keys.get("openai").map(String::as_str), Some("sk-padded"));
        assert_eq!(keys.get("groq").map(String::as_str), Some(""));
        assert_eq!(keys.get("mistral"), None);
    }

    /// A user file must be able to blank out a key `/etc` set, or there
    /// is no way to opt out of a system-wide credential. A blank never
    /// survives as an empty bearer token either.
    #[test]
    fn a_later_blank_shadows_an_earlier_key_and_is_never_spent() {
        let system = HashMap::from([
            ("openai".to_string(), "sk-system".to_string()),
            ("groq".to_string(), "sk-groq".to_string()),
        ]);
        let user = HashMap::from([("openai".to_string(), String::new())]);

        let merged = merge_keys(vec![system, user]);
        assert_eq!(merged.get("openai"), None, "blanked out by the user file");
        assert_eq!(merged.get("groq").map(String::as_str), Some("sk-groq"));

        // And the ordinary case: a later real key replaces an earlier one.
        let merged = merge_keys(vec![
            HashMap::from([("openai".to_string(), "sk-system".to_string())]),
            HashMap::from([("openai".to_string(), "sk-user".to_string())]),
        ]);
        assert_eq!(merged.get("openai").map(String::as_str), Some("sk-user"));
    }

    /// A misspelling that parsed happily would silently never take
    /// effect. Every section rejects one.
    #[test]
    fn a_misspelled_section_or_field_is_rejected_rather_than_ignored() {
        assert!(parse("[provider.openai]\napi_key = \"x\"").is_err());
        assert!(parse("[providers.openai]\napi_kye = \"x\"").is_err());
        assert!(parse("[alias]\nx = \"y\"").is_err());
        assert!(parse("[verify]\ndefualt = \"off\"").is_err());
        assert!(parse("[verify]\nlayres = \"off\"").is_err());
        assert!(parse("[[verify.trust]]\npattern = \"a/b\"\nkyes = []").is_err());
        assert!(parse("[[verify.trust]]\npattern = \"a/b\"\nclaim = \"c/d\"").is_err());
        assert!(parse("[registry.\"docker.io\"]\nmirrors = \"m\"").is_err());
        assert!(parse("[registries.\"docker.io\"]\nmirror = \"m\"").is_err());
        assert!(parse("api_key = \"x\"").is_err());
    }

    /// A mangled file is reported, not treated as empty.
    #[test]
    fn malformed_toml_is_an_error() {
        assert!(parse("[providers.openai").is_err());
    }

    // -- configured providers ------------------------------------------------

    /// A `[providers.<id>]` with a `base_url` defines a provider; every
    /// other field has a default. One without is only a key, as before.
    #[test]
    fn a_base_url_turns_a_provider_table_into_a_definition() {
        let files = [file(
            r#"
            [providers.inferencebox]
            base_url = "http://inferencebox:8000/v1/"

            [providers.relay]
            base_url    = "https://relay.example/v1"
            wire        = "anthropic"
            api_key_env = "RELAY_KEY"
            name        = "Claude relay"
            api_key     = "sk-relay"

            [providers.openrouter]
            api_key = "sk-or"
            "#,
        )];
        let configured = configured_from(&files);
        assert_eq!(
            configured,
            vec![
                ConfiguredProvider {
                    id: "inferencebox".into(),
                    name: "inferencebox".into(),
                    // Trailing slash gone: a route appends its own.
                    base_url: "http://inferencebox:8000/v1".into(),
                    wire: crate::providers::Wire::OpenAi,
                    key_env: None,
                },
                ConfiguredProvider {
                    id: "relay".into(),
                    name: "Claude relay".into(),
                    base_url: "https://relay.example/v1".into(),
                    wire: crate::providers::Wire::Anthropic,
                    key_env: Some("RELAY_KEY".into()),
                },
            ]
        );
        // The key of a defined provider is reached the same way as any.
        assert_eq!(
            files[0]
                .conf
                .provider_keys()
                .get("relay")
                .map(String::as_str),
            Some("sk-relay")
        );
        assert!(configured_from(&[]).is_empty());
    }

    /// A later file re-defining an id replaces the fields it sets and
    /// keeps the rest, as keys merge.
    #[test]
    fn a_later_definition_overrides_field_by_field() {
        let system = file(
            "[providers.inferencebox]\nbase_url = \"http://inferencebox:8000/v1\"\nname = \"Shared box\"\n\
             api_key_env = \"SHARED_KEY\"",
        );
        let user = file(
            "[providers.inferencebox]\nbase_url = \"http://10.0.0.5:8000/v1\"\nwire = \"anthropic\"\n\
             api_key_env = \"\"",
        );
        let merged = configured_from(&[system, user]);
        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].base_url, "http://10.0.0.5:8000/v1");
        assert_eq!(merged[0].name, "Shared box");
        assert_eq!(merged[0].wire, crate::providers::Wire::Anthropic);
        // A blank clears the inherited variable, so /etc's key does not
        // follow the user to another endpoint.
        assert_eq!(merged[0].key_env, None);
    }

    /// What `config set` has to refuse on the spot: a `base_url` that is
    /// not an absolute http(s) URL, a definition field on a table that
    /// defines nothing, a `wire` llmman does not speak.
    #[test]
    fn a_bad_provider_definition_is_a_parse_error() {
        let bad = |body: &str| parse(&format!("[providers.inferencebox]\n{body}")).expect_err(body);

        assert!(bad("base_url = \"inferencebox:8000\"").contains("base_url"));
        assert!(bad("base_url = \"inferencebox:8000/v1\"").contains("base_url"));
        assert!(bad("base_url = \"ftp://inferencebox/v1\"").contains("http or https"));
        assert!(bad("base_url = \"http://\"").contains("base_url"));
        assert!(bad("base_url = \"http://inferencebox/v1?x=1\"").contains("query"));
        assert!(bad("base_url = \"\"").contains("base_url"));
        // The URL is reported by the daemon's API; a secret does not go in it.
        assert!(bad("base_url = \"https://user:pw@inferencebox/v1\"").contains("api_key"));
        assert!(bad("base_url = \"https://user@inferencebox/v1\"").contains("api_key"));
        // A slash in the id would never survive `split_remote_ref`.
        assert!(
            parse("[providers.\"team/gpu\"]\nbase_url = \"http://g/v1\"")
                .expect_err("slash id")
                .contains("'/'")
        );
        assert!(parse("[providers.\"\"]\napi_key = \"x\"").is_err());

        let err = bad("wire = \"anthropic\"");
        assert!(err.contains("no base_url"), "{err}");
        assert!(err.contains("wire"), "{err}");
        let err = bad("api_key_env = \"X\"\nname = \"n\"");
        assert!(err.contains("api_key_env, name"), "{err}");

        assert!(bad("base_url = \"http://g/v1\"\nwire = \"ollama\"").contains("wire"));
        assert!(bad("base_url = \"http://g/v1\"\nbase_ulr = \"x\"").contains("base_ulr"));

        // The good shapes parse, and come out normalized: lowercase
        // scheme and host, no trailing slash.
        for (url, want) in [
            ("http://inferencebox:8000/v1", "http://inferencebox:8000/v1"),
            ("http://inferencebox", "http://inferencebox"),
            (
                "HTTP://InferenceBox:8000/v1/",
                "http://inferencebox:8000/v1",
            ),
            ("http://127.0.0.1:11434/v1", "http://127.0.0.1:11434/v1"),
            ("http://[::1]:8000/v1", "http://[::1]:8000/v1"),
            (
                "https://relay.example/api/v1",
                "https://relay.example/api/v1",
            ),
        ] {
            assert_eq!(base_url_of(url).expect(url), want);
            parse(&format!("[providers.inferencebox]\nbase_url = {url:?}")).expect(url);
        }
    }

    /// The definition fields show in `Debug`; the key still does not.
    #[test]
    fn debug_output_shows_a_definition_but_never_a_key() {
        let c = conf(
            "[providers.inferencebox]\nbase_url = \"http://inferencebox:8000/v1\"\napi_key = \"sk-secret-value\"",
        );
        let rendered = format!("{c:?}");
        assert!(rendered.contains("inferencebox:8000"), "{rendered}");
        assert!(!rendered.contains("sk-secret-value"), "{rendered}");
    }

    #[test]
    fn managed_configuration_is_opt_in_strict_and_uses_field_precedence() {
        assert!(!managed_from_files(&[]).unwrap().enabled);
        let base = file("[managed]\nenabled = \"true\"\nread_timeout_seconds = \"600\"");
        let override_file = file("[managed]\nread_timeout_seconds = \"0\"");
        let settings = managed_from_files(&[base, override_file]).unwrap();
        assert!(settings.enabled);
        assert!(settings.read_timeout.is_none());
        let settings =
            managed_from_files(&[file("[managed]\nread_timeout_seconds = \"300\"")]).unwrap();
        assert_eq!(
            settings.read_timeout,
            Some(std::time::Duration::from_secs(300))
        );
        assert!(!settings.enabled);
        for text in [
            "[managed]\nenabled = \"yes\"",
            "[managed]\nread_timeout_seconds = \"-1\"",
            "[managed]\nread_timeout_seconds = \"4294967296\"",
        ] {
            assert!(parse(text).is_err(), "{text}");
        }
        assert!(parse("[managed]\nenabled = true").is_err());
        assert!(parse("[managed]\nenabeld = \"true\"").is_err());
    }

    // -- aggregation peers ---------------------------------------------------

    fn file(text: &str) -> File {
        File {
            path: PathBuf::from("llmman.conf"),
            dir: PathBuf::from("."),
            conf: conf(text),
        }
    }

    #[test]
    fn peers_come_from_the_environment_then_the_last_file_that_sets_them() {
        let system = || file("[aggregation]\npeers = \"a, b\"");
        let user = file("[aggregation]\npeers = \"c\"");
        let silent = file("");
        let opted_out = file("[aggregation]\npeers = \"\"");

        assert_eq!(
            peers_from(Some(" x , ,y:1 "), &[]),
            vec!["x".to_string(), "y:1".to_string()]
        );
        assert!(peers_from(Some(""), &[system()]).is_empty());
        assert_eq!(
            peers_from(None, &[system(), silent]),
            vec!["a".to_string(), "b".to_string()]
        );
        assert_eq!(peers_from(None, &[system(), user]), vec!["c".to_string()]);
        assert!(peers_from(None, &[system(), opted_out]).is_empty());
        assert!(peers_from(None, &[]).is_empty());
        assert!(parse("[aggregation]\npeer = \"a\"").is_err());
    }

    /// The daemon's own keys and the peer key are secrets: redacted in
    /// `Debug`, and read from the last file that sets them — skipping,
    /// with a warning, one that other users can read.
    #[cfg(unix)]
    #[test]
    fn daemon_and_peer_keys_are_redacted_and_gated_on_the_file_mode() {
        use std::os::unix::fs::PermissionsExt;

        let c = conf("[auth]\napi_keys = \"k-secret,k2\"\n[aggregation]\napi_key = \"p-secret\"");
        let rendered = format!("{c:?}");
        assert!(!rendered.contains("secret"), "{rendered}");
        assert!(
            parse("[auth]\napi_key = \"k\"").is_err(),
            "the field is plural"
        );

        let dir = std::env::temp_dir().join(format!("llmman-auth-{}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("temp dir");
        let write = |name: &str, text: &str, mode: u32| {
            let path = dir.join(name);
            std::fs::write(&path, text).expect("write");
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).expect("chmod");
        };
        write(
            "system.conf",
            "[auth]\napi_keys = \"a, b\"\n[aggregation]\napi_key = \"pa\"",
            0o600,
        );
        write(
            "loose.conf",
            "[auth]\napi_keys = \"c\"\n[aggregation]\napi_key = \"pc\"",
            0o644,
        );
        write(
            "out.conf",
            "[auth]\napi_keys = \"\"\n[aggregation]\napi_key = \"\"",
            0o644,
        );
        let files = |names: &[&str]| -> Vec<File> {
            names
                .iter()
                .map(|n| {
                    let path = dir.join(n);
                    File {
                        dir: dir.clone(),
                        conf: parse(&std::fs::read_to_string(&path).unwrap()).unwrap(),
                        path,
                    }
                })
                .collect()
        };
        let keys = |files: &[File]| {
            split_list(
                secret_from_files(files, "auth.api_keys", |f| f.conf.auth.api_keys.as_deref())
                    .as_deref(),
            )
        };
        let peer = |files: &[File]| {
            secret_from_files(files, "aggregation.api_key", |f| {
                f.conf.aggregation.api_key.as_deref()
            })
        };
        let ab = vec!["a".to_string(), "b".to_string()];
        assert_eq!(keys(&files(&["system.conf"])), ab);
        assert_eq!(peer(&files(&["system.conf"])).as_deref(), Some("pa"));
        // The loose file's secret is skipped, and the system one used.
        assert_eq!(keys(&files(&["system.conf", "loose.conf"])), ab);
        assert_eq!(
            peer(&files(&["system.conf", "loose.conf"])).as_deref(),
            Some("pa")
        );
        // A blank needs no mode: it holds nothing, and clears the system one.
        assert!(keys(&files(&["system.conf", "out.conf"])).is_empty());
        assert_eq!(
            peer(&files(&["system.conf", "out.conf"])).as_deref(),
            Some("")
        );
        assert!(keys(&[]).is_empty());

        std::fs::remove_dir_all(&dir).ok();
    }

    // -- registry mirrors ----------------------------------------------------

    /// A mirror is spelled like a URL with the scheme optional, and
    /// comes out normalized the way the Go shim wants it.
    #[test]
    fn a_mirror_is_normalized_and_defaults_to_https() {
        assert_eq!(
            mirror_url_of("mirror.gcr.io").as_deref(),
            Ok("https://mirror.gcr.io")
        );
        assert_eq!(
            mirror_url_of(" HTTP://Mirror.Corp:5000/ ").as_deref(),
            Ok("http://mirror.corp:5000")
        );
        assert_eq!(
            mirror_url_of("https://proxy.corp/registry/").as_deref(),
            Ok("https://proxy.corp/registry")
        );
        for bad in [
            "",
            "ftp://mirror",
            "https://",
            "https://user:pw@mirror",
            "https://mirror?x=1",
            "https://mirror#frag",
        ] {
            assert!(mirror_url_of(bad).is_err(), "{bad:?}");
        }
    }

    /// The table key is a host. Hub's aliases fold onto `docker.io`,
    /// which is what a reference's `docker.io/ai/x` names and what the
    /// Go shim is asked for.
    #[test]
    fn a_registry_key_is_a_host_and_hub_has_one_name() {
        assert_eq!(
            canonical_registry_host("Index.Docker.io").as_deref(),
            Some("docker.io")
        );
        assert_eq!(
            canonical_registry_host("registry-1.docker.io").as_deref(),
            Some("docker.io")
        );
        assert_eq!(
            canonical_registry_host("localhost:5000").as_deref(),
            Some("localhost:5000")
        );
        assert_eq!(
            canonical_registry_host("ghcr.io").as_deref(),
            Some("ghcr.io")
        );
        assert_eq!(
            canonical_registry_host("[::1]:5000").as_deref(),
            Some("[::1]:5000")
        );
        assert_eq!(
            canonical_registry_host("ghcr.io:443").as_deref(),
            Some("ghcr.io")
        );
        for bad in [
            "",
            "https://ghcr.io",
            "ghcr.io/org",
            "*.example.com",
            "host:port",
            ":5000",
            "user@registry",
            "registry?q=1",
            "registry#f",
            "[not-an-ip]:5000",
            "a b",
        ] {
            assert_eq!(canonical_registry_host(bad), None, "{bad:?}");
        }
        assert!(parse("[registries.\"https://ghcr.io\"]\nmirrors = \"m\"").is_err());
        assert!(parse("[registries.\"ghcr.io\"]\nmirrors = \"ftp://m\"").is_err());
        assert!(parse("[registries.\"ghcr.io\"]\nmirrors = \"m\"").is_ok());
        // Two keys for one registry would leave which wins to HashMap order.
        assert!(parse(
            "[registries.\"docker.io\"]\nmirrors = \"a\"\n[registries.\"index.docker.io\"]\nmirrors = \"b\""
        )
        .is_err());
        assert!(parse(
            "[registries.\"ghcr.io\"]\nmirrors = \"a\"\n[registries.\"GHCR.io\"]\nmirrors = \"b\""
        )
        .is_err());
    }

    /// Per host, the environment (Hub only), then the last file that
    /// sets it; a blank opts a host out; hosts sort, mirrors keep order.
    #[test]
    fn mirrors_come_from_the_environment_then_the_last_file_that_sets_them() {
        let system = || {
            file(
                r#"
                [registries."docker.io"]
                mirrors = "https://a, b:5000"

                [registries."ghcr.io"]
                mirrors = "http://g"
                "#,
            )
        };
        let user = file("[registries.\"index.docker.io\"]\nmirrors = \"c\"");
        let opted_out = file("[registries.\"docker.io\"]\nmirrors = \"\"");
        let silent = file("[registries.\"docker.io\"]");

        let got = registry_mirrors_from(None, &[system(), silent]);
        assert_eq!(
            got.get("docker.io").map(Vec::as_slice),
            Some(&["https://a".to_string(), "https://b:5000".to_string()][..])
        );
        assert_eq!(
            got.get("ghcr.io").map(Vec::as_slice),
            Some(&["http://g".to_string()][..])
        );

        let got = registry_mirrors_from(None, &[system(), user]);
        assert_eq!(
            got.get("docker.io").map(Vec::as_slice),
            Some(&["https://c".to_string()][..]),
            "a Hub alias replaces docker.io's own entry"
        );

        let got = registry_mirrors_from(None, &[system(), opted_out]);
        assert!(!got.contains_key("docker.io"));
        assert!(
            got.contains_key("ghcr.io"),
            "opting Hub out leaves ghcr.io alone"
        );

        let got = registry_mirrors_from(Some(" x , ,http://y:1/ "), &[system()]);
        assert_eq!(
            got.get("docker.io").map(Vec::as_slice),
            Some(&["https://x".to_string(), "http://y:1".to_string()][..])
        );
        assert!(!registry_mirrors_from(Some(""), &[system()]).contains_key("docker.io"));
        assert!(registry_mirrors_from(Some("ftp://nope"), &[]).is_empty());
        assert!(registry_mirrors_from(None, &[]).is_empty());
    }

    // -- launch defaults -----------------------------------------------------

    #[test]
    fn launch_defaults_come_from_the_integrations_own_table() {
        let f = [file(
            "[launch.opencode]\nprovider = \"anthropic\"\nmodel = \"claude-sonnet-5-5\"\n\
             variant = \"xhigh\"\n[launch.claude]\nmodel = \"qwen3.8\"",
        )];
        assert_eq!(
            launch_defaults_from(&f, "opencode"),
            launch_conf(Some("anthropic"), Some("claude-sonnet-5-5"), Some("xhigh"))
        );
        assert_eq!(
            launch_defaults_from(&f, "claude"),
            launch_conf(None, Some("qwen3.8"), None)
        );
        for other in ["codex", "nonesuch"] {
            assert_eq!(
                launch_defaults_from(&f, other),
                LaunchConf::default(),
                "{other}"
            );
        }
        assert_eq!(launch_defaults_from(&[], "opencode"), LaunchConf::default());
    }

    /// `launch` takes any case, and `copilot-cli` for `copilot`.
    #[test]
    fn launch_defaults_are_found_through_case_and_alias() {
        let f = [file(
            "[launch.OpenCode]\nmodel = \"m\"\n[launch.copilot-cli]\nmodel = \"c\"",
        )];
        assert_eq!(
            launch_defaults_from(&f, "OPENCODE").model.as_deref(),
            Some("m")
        );
        for name in ["copilot", "copilot-cli", "Copilot"] {
            assert_eq!(
                launch_defaults_from(&f, name).model.as_deref(),
                Some("c"),
                "{name}"
            );
        }
    }

    /// One key at a time; a blank (or whitespace) value clears one.
    #[test]
    fn a_later_file_overrides_launch_defaults_one_key_at_a_time() {
        let system = file(
            "[launch.opencode]\nprovider = \"anthropic\"\nmodel = \"sys\"\nvariant = \"high\"",
        );
        let user = file("[launch.opencode]\nmodel = \"mine\"\nvariant = \"\"");
        assert_eq!(
            launch_defaults_from(&[system, user], "opencode"),
            launch_conf(Some("anthropic"), Some("mine"), None)
        );
        let blank = file("[launch.opencode]\nprovider = \"  \"\nmodel = \" m \"");
        assert_eq!(
            launch_defaults_from(&[blank], "opencode"),
            launch_conf(None, Some("m"), None)
        );
    }

    #[test]
    fn a_launch_table_must_name_an_integration_a_key_and_a_variant() {
        let err = |text: &str| parse(text).expect_err("must not parse");
        assert!(err("[launch.opencod]\nmodel = \"m\"").contains("opencod"));
        assert!(err("[launch.opencode]\nmodle = \"m\"").contains("modle"));
        assert!(err("[launch.opencode]\nvariant = \"turbo\"").contains("turbo"));
        assert!(err("[launch.opencode]\nvariant = 3").contains("string"));
        assert!(err("[launch]\nmodel = \"m\"").contains("model"));
        let both = "[launch.copilot]\nmodel = \"a\"\n[launch.copilot-cli]\nmodel = \"b\"";
        assert!(err(both).contains("same integration"));
        for variant in crate::cmd::run::variants().chain(["", "  "]) {
            parse(&format!("[launch.opencode]\nvariant = {variant:?}")).expect(variant);
        }
    }

    // -- search paths --------------------------------------------------------

    /// Two locations, system first so the user's file wins — and none
    /// of the package-managed ones earlier versions searched.
    #[test]
    fn the_search_path_is_etc_then_the_user_directory() {
        let paths = search_paths();
        assert_eq!(
            paths.first().expect("a system path"),
            &system_dir().join(FILE)
        );
        assert!(system_dir().ends_with("etc/llmman"), "{:?}", system_dir());
        // Rooted, so it cannot resolve against the working directory.
        assert!(system_dir().is_absolute(), "{:?}", system_dir());
        assert!(
            !paths.iter().any(|p| p.starts_with("/usr/share")),
            "{paths:?}"
        );
        if let Some(user) = user_path() {
            assert_eq!(paths.last(), Some(&user));
            assert!(user.ends_with("llmman/llmman.conf"), "{}", user.display());
        }
    }

    /// One per-user location on every platform, so there is exactly one
    /// place `user_path` can name in an error.
    #[test]
    fn there_is_one_user_directory_on_every_platform() {
        assert_eq!(search_paths().len(), user_dir().iter().count() + 1);
        if let Some(dir) = user_dir() {
            assert!(dir.ends_with(".config/llmman"), "{}", dir.display());
        }
    }

    /// A key any account on the box can read is not one worth spending.
    ///
    /// Everything here is inlined rather than sharing a helper: a helper
    /// used only by a `cfg(unix)` test is dead code on Windows, which
    /// `clippy --all-targets -D warnings` rejects.
    #[cfg(unix)]
    #[test]
    fn a_group_or_world_readable_file_is_refused() {
        use std::os::unix::fs::PermissionsExt;

        let dir = std::env::temp_dir().join(format!("llmman-conf-{}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("temp dir");
        let path = dir.join(FILE);
        std::fs::write(&path, "[providers.openai]\napi_key = \"sk-x\"\n").expect("write");

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).expect("chmod");
        let err = owner_readable_only(&path).expect_err("0644 is refused");
        assert!(err.contains("chmod 600"), "{err}");

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).expect("chmod");
        assert!(owner_readable_only(&path).is_ok());

        std::fs::remove_dir_all(&dir).ok();
    }
}