llmman 0.1.366

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
//! `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` as usual.)
//!
//! ```toml
//! [aliases]                            # crate::shortnames
//! gemma4 = "docker.io/ai/gemma4"
//!
//! [providers.openrouter]               # crate::providers
//! api_key = "sk-or-..."
//!
//! [providers.gpubox]                   # a provider models.dev does not list
//! base_url = "http://gpubox:8000/v1"
//! wire     = "openai"                  # default; or "anthropic"
//!
//! [verify]                             # crate::verify
//! default = "off"
//!
//! [[verify.trust]]
//! pattern = "docker.io/myorg/**"
//! keys    = ["keys/myorg.pub"]         # relative to this file's directory
//! mode    = "enforce"
//!
//! [aggregation]                        # cmd::serve::aggregation
//! peers = "asahi,spark:17434"
//! ```
//!
//! 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::HashMap;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

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 {
    /// 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,
}

/// The `[aggregation]` section.
#[derive(Deserialize, Default, Debug, 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>,
}

/// 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>,
    /// `[[verify.trust]]` entries, in the order they appear.
    #[serde(default)]
    pub trust: Vec<TrustConf>,
}

/// 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>,
}

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. [`load_provider_keys`] drops blanks
    /// after merging, since sending `Authorization: Bearer ` upstream
    /// would turn a clear "no API key" into someone else's 401.
    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 `$HOME` as usual.
fn user_dir() -> Option<PathBuf> {
    dirs::home_dir().map(|h| h.join(".config").join("llmman"))
}

/// 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)
}

/// [`user_path`] for printing, falling back to the bare file name.
pub fn user_path_display() -> String {
    user_path()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| FILE.to_string())
}

// ---------------------------------------------------------------------------
// 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());
    }

    let mut files = Vec::new();
    for path in search_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> {
    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(())
}

/// Normalizes a configured `base_url`: an absolute `http`/`https` URL
/// with no query, fragment or userinfo, lowercased scheme and host, and
/// no trailing slash. Userinfo is refused because the URL is reported by
/// the daemon's API and printed in warnings; `api_key` is where a
/// credential goes.
fn base_url_of(url: &str) -> Result<String, String> {
    let url = url.trim();
    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:?}: a base URL cannot carry a username or password; use api_key"
        ));
    }
    if parsed.query().is_some() || parsed.fragment().is_some() {
        return Err(format!("{url:?}: a base URL has no query or fragment"));
    }
    Ok(parsed.as_str().trim_end_matches('/').to_string())
}

// ---------------------------------------------------------------------------
// 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.
///
/// Borrowed from a process-lifetime cache, so a daemon serving requests
/// is not stat'ing `/etc` per token.
pub fn provider_api_key(provider_id: &str) -> Option<&'static str> {
    static CACHE: OnceLock<HashMap<String, String>> = OnceLock::new();
    CACHE
        .get_or_init(load_provider_keys)
        .get(provider_id)
        .map(String::as_str)
}

fn load_provider_keys() -> HashMap<String, String> {
    let mut accepted = Vec::new();
    for file in files().unwrap_or_default() {
        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> {
    env.or_else(|| {
        files
            .iter()
            .rev()
            .find_map(|f| f.conf.aggregation.peers.as_deref())
    })
    .unwrap_or_default()
    .split(',')
    .map(str::trim)
    .filter(|s| !s.is_empty())
    .map(str::to_string)
    .collect()
}

/// 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::*;

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

    /// 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"

            [[verify.trust]]
            pattern = "docker.io/myorg/**"
            keys    = ["keys/myorg.pub"]
            mode    = "enforce"
            "#,
        );
        assert_eq!(
            c.aliases.get("gemma4").map(String::as_str),
            Some("docker.io/ai/gemma4")
        );
        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.trust.len(), 1);
        assert_eq!(c.verify.trust[0].mode.as_deref(), Some("enforce"));
    }

    /// 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.trust]]\npattern = \"a/b\"\nkyes = []").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.gpubox]
            base_url = "http://gpubox: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: "gpubox".into(),
                    name: "gpubox".into(),
                    // Trailing slash gone: a route appends its own.
                    base_url: "http://gpubox: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.gpubox]\nbase_url = \"http://gpubox:8000/v1\"\nname = \"Shared box\"\n\
             api_key_env = \"SHARED_KEY\"",
        );
        let user = file(
            "[providers.gpubox]\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.gpubox]\n{body}")).expect_err(body);

        assert!(bad("base_url = \"gpubox:8000\"").contains("base_url"));
        assert!(bad("base_url = \"gpubox:8000/v1\"").contains("base_url"));
        assert!(bad("base_url = \"ftp://gpubox/v1\"").contains("http or https"));
        assert!(bad("base_url = \"http://\"").contains("base_url"));
        assert!(bad("base_url = \"http://gpubox/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@gpubox/v1\"").contains("api_key"));
        assert!(bad("base_url = \"https://user@gpubox/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://gpubox:8000/v1", "http://gpubox:8000/v1"),
            ("http://gpubox", "http://gpubox"),
            ("HTTP://GPUBox:8000/v1/", "http://gpubox: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.gpubox]\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.gpubox]\nbase_url = \"http://gpubox:8000/v1\"\napi_key = \"sk-secret-value\"",
        );
        let rendered = format!("{c:?}");
        assert!(rendered.contains("gpubox:8000"), "{rendered}");
        assert!(!rendered.contains("sk-secret-value"), "{rendered}");
    }

    // -- 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());
    }

    // -- 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();
    }
}