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
//! `llmman config` — read and write `llmman.conf` without an editor.
//!
//! `git config` verb for verb, with `--system`/`--user`/`--file`
//! picking one of [`crate::config`]'s locations. Reads see every file
//! merged, later overriding earlier; writes go to the per-user file.
//!
//! Keys are TOML dotted keys, quoted as the file quotes them:
//! `providers."wafer.ai".api_key`. Values are always written as
//! strings, which is every field the format has, and `set`/`unset`
//! cannot reach inside `[[verify.trust]]` — that is what `edit` is for.
//!
//! Every write goes through [`crate::config::parse`] first. That is the
//! point of the command: the format is `deny_unknown_fields`, so a
//! hand-edited `api_kye` is a credential that never takes effect and
//! surfaces later as someone else's 401.

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

use anyhow::{anyhow, bail, Context, Result};
use clap::{Args, Subcommand};
use toml_edit::{DocumentMut, Item, Key, Table, TableLike, Value};

#[derive(Args, Debug)]
pub struct ConfigArgs {
    #[command(subcommand)]
    pub command: ConfigCommand,

    /// Act on the system-wide file (/etc/llmman/llmman.conf)
    #[arg(long, global = true, conflicts_with_all = ["user", "file"])]
    pub system: bool,

    /// Act on the per-user file (~/.config/llmman/llmman.conf), which is
    /// already where `set`, `unset` and `edit` write
    #[arg(long, global = true, conflicts_with = "file")]
    pub user: bool,

    /// Act on this file instead of the configured locations
    #[arg(long, global = true, value_name = "PATH")]
    pub file: Option<PathBuf>,
}

#[derive(Subcommand, Debug)]
pub enum ConfigCommand {
    /// Print every key that is set, one `key=value` per line
    List(ListArgs),
    /// Print the value of one key, or exit non-zero if it is not set
    Get(GetArgs),
    /// Set one key to a string value
    Set(SetArgs),
    /// Remove one key
    Unset(UnsetArgs),
    /// Open the file in $VISUAL or $EDITOR, then check that it parses
    Edit,
}

#[derive(Args, Debug)]
pub struct ListArgs {
    /// Prefix each line with the file the key came from
    #[arg(long)]
    pub show_origin: bool,
}

#[derive(Args, Debug)]
pub struct GetArgs {
    /// A dotted key, exactly as `llmman config list` prints it
    #[arg(value_name = "KEY")]
    pub key: String,
}

#[derive(Args, Debug)]
pub struct SetArgs {
    /// A dotted key, e.g. `aliases.gemma4` or `providers.openai.api_key`
    #[arg(value_name = "KEY")]
    pub key: String,
    /// The value to store, always written as a TOML string
    #[arg(value_name = "VALUE")]
    pub value: String,
}

#[derive(Args, Debug)]
pub struct UnsetArgs {
    /// A dotted key, e.g. `providers.openai.api_key`
    #[arg(value_name = "KEY")]
    pub key: String,
}

pub fn run(args: &ConfigArgs) -> Result<()> {
    match &args.command {
        ConfigCommand::List(a) => list(args, a),
        ConfigCommand::Get(a) => get(args, a),
        ConfigCommand::Set(a) => set(args, a),
        ConfigCommand::Unset(a) => unset(args, a),
        ConfigCommand::Edit => edit(args),
    }
}

// ---------------------------------------------------------------------------
// Which file
// ---------------------------------------------------------------------------

impl ConfigArgs {
    /// The one file the flags name, or `None` for "all of them".
    fn selected(&self) -> Result<Option<PathBuf>> {
        if let Some(file) = &self.file {
            return Ok(Some(file.clone()));
        }
        if self.system {
            return Ok(Some(crate::config::system_path()));
        }
        if self.user {
            return Ok(Some(user_path()?));
        }
        Ok(None)
    }

    /// Ascending priority, so the last value printed wins.
    fn read_paths(&self) -> Result<Vec<PathBuf>> {
        Ok(match self.selected()? {
            Some(path) => vec![path],
            None => crate::config::search_paths(),
        })
    }

    /// The per-user file by default: `/etc` is admin-managed and needs
    /// an explicit `--system`.
    fn write_path(&self) -> Result<PathBuf> {
        match self.selected()? {
            Some(path) => Ok(path),
            None => user_path(),
        }
    }
}

fn user_path() -> Result<PathBuf> {
    crate::config::user_path()
        .ok_or_else(|| anyhow!("no home directory to hold llmman.conf; use --file"))
}

// ---------------------------------------------------------------------------
// The key space
// ---------------------------------------------------------------------------

/// Every scalar in `doc`, as the dotted key naming it and its value.
///
/// The whole key space: `get` looks its argument up in what `list`
/// prints, so the two cannot disagree about what a key is called.
/// Indices name array elements, making `[[verify.trust]]` readable
/// without inventing a notation.
fn entries(doc: &DocumentMut) -> Vec<(String, String)> {
    let mut out = Vec::new();
    walk_item(doc.as_item(), "", &mut out);
    out
}

fn walk_item(item: &Item, prefix: &str, out: &mut Vec<(String, String)>) {
    match item {
        Item::Table(table) => walk_table(table, prefix, out),
        Item::ArrayOfTables(tables) => {
            for (i, table) in tables.iter().enumerate() {
                walk_table(table, &format!("{prefix}[{i}]"), out);
            }
        }
        Item::Value(value) => walk_value(value, prefix, out),
        Item::None => {}
    }
}

fn walk_table(table: &Table, prefix: &str, out: &mut Vec<(String, String)>) {
    for (key, item) in table.iter() {
        walk_item(item, &join(prefix, key), out);
    }
}

fn walk_value(value: &Value, prefix: &str, out: &mut Vec<(String, String)>) {
    match value {
        Value::InlineTable(table) => {
            for (key, value) in table.iter() {
                walk_value(value, &join(prefix, key), out);
            }
        }
        Value::Array(array) => {
            for (i, value) in array.iter().enumerate() {
                walk_value(value, &format!("{prefix}[{i}]"), out);
            }
        }
        // Unquoted: the quotes are TOML's, not part of the value.
        Value::String(s) => out.push((prefix.to_string(), s.value().clone())),
        other => out.push((prefix.to_string(), other.to_string().trim().to_string())),
    }
}

/// Append one key to a dotted path, quoted if TOML would quote it.
fn join(prefix: &str, key: &str) -> String {
    let key = crate::config::toml_key(key);
    if prefix.is_empty() {
        key
    } else {
        format!("{prefix}.{key}")
    }
}

/// Split by TOML's rules, so `providers."wafer.ai"` is the two keys the
/// file spells that way rather than three. Rebuilt from the decoded
/// text: a parsed key carries its own whitespace, and `aliases.qwen`
/// would land as `qwen="…"` among lines reading `gemma4 = "…"`.
fn parse_key(name: &str) -> Result<Vec<Key>> {
    let keys = Key::parse(name).map_err(|e| anyhow!("invalid key `{name}`: {e}"))?;
    if keys.is_empty() {
        bail!("invalid key `{name}`: a key cannot be empty");
    }
    Ok(keys.iter().map(|key| Key::new(key.get())).collect())
}

fn is_credential(key: &str) -> bool {
    key == "api_key" || key.ends_with(".api_key") || key == "auth.api_keys"
}

/// Whether the document holds a secret — see [`write_atomic`].
fn carries_key(doc: &DocumentMut) -> bool {
    entries(doc)
        .iter()
        .any(|(key, value)| is_credential(key) && !value.trim().is_empty())
}

// ---------------------------------------------------------------------------
// Reading
// ---------------------------------------------------------------------------

/// Empty when the file is absent — the common case.
fn read(path: &Path) -> Result<DocumentMut> {
    match std::fs::read_to_string(path) {
        Ok(text) => text
            .parse::<DocumentMut>()
            .with_context(|| format!("parsing {}", path.display())),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(DocumentMut::new()),
        Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
    }
}

/// [`read`], warning where the file's keys would be dropped at load
/// time, so a read cannot report a value that will never be spent.
fn read_effective(path: &Path) -> Result<DocumentMut> {
    let doc = read(path)?;
    if carries_key(&doc) {
        if let Err(e) = crate::config::owner_readable_only(path) {
            eprintln!(
                "[llmman] warning: the API keys in {} are ignored: {e}",
                path.display()
            );
        }
    }
    Ok(doc)
}

/// A credential prints as `<redacted>`: this is the command whose
/// output ends up in a bug report. `get` names one key and prints it.
fn list(args: &ConfigArgs, opts: &ListArgs) -> Result<()> {
    for path in args.read_paths()? {
        for (key, value) in entries(&read_effective(&path)?) {
            let value = if is_credential(&key) {
                "<redacted>"
            } else {
                &value
            };
            if opts.show_origin {
                println!("{}\t{key}={value}", path.display());
            } else {
                println!("{key}={value}");
            }
        }
    }
    Ok(())
}

/// The last value set, across files and within a file — what llmman
/// would use. Except an array index: `[[verify.trust]]` is appended
/// across files rather than replaced, so `verify.trust[0]` names one
/// per file and `list --show-origin` tells them apart.
fn get(args: &ConfigArgs, opts: &GetArgs) -> Result<()> {
    let mut found = None;
    for path in args.read_paths()? {
        if let Some((_, value)) = entries(&read_effective(&path)?)
            .into_iter()
            .rev()
            .find(|(key, _)| *key == opts.key)
        {
            found = Some(value);
        }
    }
    match found {
        Some(value) => {
            println!("{value}");
            Ok(())
        }
        None => bail!("{}: not set", opts.key),
    }
}

// ---------------------------------------------------------------------------
// Writing
// ---------------------------------------------------------------------------

fn set(args: &ConfigArgs, opts: &SetArgs) -> Result<()> {
    let path = args.write_path()?;
    let mut doc = read(&path)?;
    set_in(&mut doc, &opts.key, &opts.value)?;
    save(&path, &doc)
}

fn unset(args: &ConfigArgs, opts: &UnsetArgs) -> Result<()> {
    let path = args.write_path()?;
    let mut doc = read(&path)?;
    unset_in(&mut doc, &opts.key)?;
    save(&path, &doc)
}

/// Sets (`Some`) or removes (`None`) `providers.<id>.api_key` in the
/// file at `path` — what `llmman config set|unset` do, for
/// `PUT|DELETE /llmman/providers/:id/key` — then reloads the daemon's
/// provider keys, also when there was nothing to remove, so `Ok` means
/// the keys in force are what the files now say. `Ok(false)` when there
/// was no key to remove. An `Err` can quote the file: log it, do not
/// return it to a client.
///
/// One write at a time: two requests interleaving read → edit → save
/// would each save the file without the other's key (and share the
/// temporary file's name).
pub(crate) fn write_provider_key(path: &Path, id: &str, key: Option<&str>) -> Result<bool> {
    static WRITING: std::sync::Mutex<()> = std::sync::Mutex::new(());
    let _one = WRITING
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let name = format!("providers.{}.api_key", crate::config::toml_key(id));
    let mut doc = read(path)?;
    let changed = match key {
        Some(key) => {
            set_in(&mut doc, &name, key)?;
            true
        }
        None => unset_in(&mut doc, &name).is_ok(),
    };
    if changed {
        save(path, &doc)?;
    }
    crate::config::reload_provider_keys().map_err(|e| anyhow!(e))?;
    Ok(changed)
}

/// Split out from [`set`] to be testable without a file, a home
/// directory, or a particular umask.
fn set_in(doc: &mut DocumentMut, name: &str, value: &str) -> Result<()> {
    let keys = parse_key(name)?;
    let (leaf, parents) = keys.split_last().expect("parse_key rejects an empty key");

    // `as_table_like_mut`: `aliases = { gemma4 = "…" }` is valid, and
    // a key `list` prints has to be one `set` can reach.
    let mut table: &mut dyn TableLike = doc.as_table_mut();
    for key in parents {
        table = table
            .entry_format(key)
            .or_insert_with(|| {
                let mut new = Table::new();
                // One `[providers.openai]` header, not an empty
                // `[providers]` above it.
                new.set_implicit(true);
                Item::Table(new)
            })
            .as_table_like_mut()
            .ok_or_else(|| anyhow!("{name}: {} is not a table", key.display_repr()))?;
    }

    let item = table.entry_format(leaf).or_insert(Item::None);
    let mut replacement = toml_edit::value(value);
    // Keep the old line's spacing and trailing comment.
    if let (Some(old), Some(new)) = (item.as_value(), replacement.as_value_mut()) {
        *new.decor_mut() = old.decor().clone();
    }
    *item = replacement;
    Ok(())
}

/// Split out from [`unset`] for the same reason as [`set_in`].
fn unset_in(doc: &mut DocumentMut, name: &str) -> Result<()> {
    let keys = parse_key(name)?;
    let (leaf, parents) = keys.split_last().expect("parse_key rejects an empty key");

    let mut table: &mut dyn TableLike = doc.as_table_mut();
    for key in parents {
        table = table
            .get_mut(key.get())
            .and_then(Item::as_table_like_mut)
            .ok_or_else(|| anyhow!("{name}: not set"))?;
    }
    if table.remove(leaf.get()).is_none() {
        bail!("{name}: not set");
    }
    Ok(())
}

/// Check, then replace atomically. Checked before the rename, never
/// after: a rejected edit has to leave the file as it was.
fn save(path: &Path, doc: &DocumentMut) -> Result<()> {
    let text = doc.to_string();
    crate::config::parse(&text)
        .map_err(|e| anyhow!("refusing to write {}: {e}", path.display()))?;
    write_atomic(path, &text, carries_key(doc))
}

/// Via a temporary file in the same directory, so an interrupted write
/// cannot leave half a config behind.
///
/// The mode is the existing file's, or 0644 for a new one:
/// `/etc/llmman/llmman.conf` has to stay readable for the aliases and
/// trust policy it carries. A file holding a key is tightened, since
/// [`crate::config`] would otherwise ignore the key just written.
fn write_atomic(path: &Path, text: &str, carries_key: bool) -> Result<()> {
    if let Some(dir) = path.parent() {
        std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
    }
    let tmp = path.with_extension(format!("{}.tmp", std::process::id()));

    #[cfg(unix)]
    {
        use std::io::Write as _;
        use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};

        let existing = std::fs::metadata(path)
            .ok()
            .map(|m| m.permissions().mode() & 0o7777);
        let mut mode = existing.unwrap_or(0o644);
        if carries_key && mode & 0o077 != 0 {
            // Silent for a new file: nobody chose that mode.
            if existing.is_some() {
                eprintln!(
                    "[llmman] tightening {} to 0600: it now holds an API key",
                    path.display()
                );
            }
            mode = 0o600;
        }
        // `mode` on create is masked by the umask, so set it again.
        std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .mode(mode)
            .open(&tmp)
            .and_then(|mut f| {
                f.set_permissions(std::fs::Permissions::from_mode(mode))?;
                f.write_all(text.as_bytes())
            })
            .with_context(|| format!("writing {}", tmp.display()))?;
    }
    #[cfg(not(unix))]
    {
        let _ = carries_key;
        std::fs::write(&tmp, text).with_context(|| format!("writing {}", tmp.display()))?;
    }

    std::fs::rename(&tmp, path)
        .inspect_err(|_| {
            let _ = std::fs::remove_file(&tmp);
        })
        .with_context(|| format!("replacing {}", path.display()))
}

// ---------------------------------------------------------------------------
// Editing
// ---------------------------------------------------------------------------

fn edit(args: &ConfigArgs) -> Result<()> {
    let path = args.write_path()?;
    prepare(&path)?;

    let editor = editor();
    let status = editor_command(&editor, &path)?
        .status()
        .with_context(|| format!("running {editor}"))?;
    if !status.success() {
        bail!("{editor} exited with {status}");
    }
    if !path.exists() {
        return Ok(()); // quit without saving
    }

    // Rewritten, not just checked, so a key typed into the editor gets
    // the owner-only mode a `set` would give it. Bytes unchanged. One
    // that does not parse is reported, not reverted: a file the user
    // can go back and fix beats one llmman threw away.
    let doc = read(&path)?;
    let text = doc.to_string();
    crate::config::parse(&text).map_err(|e| anyhow!("{} is not valid: {e}", path.display()))?;
    write_atomic(&path, &text, carries_key(&doc))
}

/// The directory only: an editor handed a path under a missing one
/// takes the edit and then cannot save it. Not the file, so an
/// abandoned edit leaves nothing behind.
fn prepare(path: &Path) -> Result<()> {
    match path.parent() {
        Some(dir) => {
            std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))
        }
        None => Ok(()),
    }
}

/// Through `sh`, as git does: `$EDITOR` is a command line, not a
/// program name, so `"/Applications/Sublime Text.app/…/subl" -w` has to
/// work. Windows has no such shell to assume.
fn editor_command(editor: &str, path: &Path) -> Result<std::process::Command> {
    if editor.trim().is_empty() {
        bail!("$VISUAL/$EDITOR is set but empty");
    }
    #[cfg(unix)]
    {
        let mut cmd = std::process::Command::new("sh");
        cmd.arg("-c")
            .arg(format!("{editor} \"$1\""))
            .arg("sh")
            .arg(path);
        Ok(cmd)
    }
    #[cfg(not(unix))]
    {
        let mut words = editor.split_whitespace();
        let program = words.next().expect("a non-empty editor has a program");
        let mut cmd = std::process::Command::new(program);
        cmd.args(words).arg(path);
        Ok(cmd)
    }
}

/// `$VISUAL`, then `$EDITOR`, then the platform fallback. Not a
/// `llmman.conf` setting: the editor belongs to the terminal, not to a
/// file shared across machines.
fn editor() -> String {
    for var in ["VISUAL", "EDITOR"] {
        match std::env::var(var) {
            Ok(value) if !value.trim().is_empty() => return value,
            _ => {}
        }
    }
    if cfg!(windows) {
        "notepad".to_string()
    } else {
        "vi".to_string()
    }
}

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

    fn doc(text: &str) -> DocumentMut {
        text.parse::<DocumentMut>().expect("valid toml")
    }

    fn keys(text: &str) -> Vec<String> {
        entries(&doc(text))
            .into_iter()
            .map(|(k, v)| format!("{k}={v}"))
            .collect()
    }

    fn scratch(label: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("llmman-{label}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("temp dir");
        dir
    }

    /// A trust policy `list` cannot show is one nobody can audit.
    #[test]
    fn every_scalar_is_addressable_including_one_in_an_array_of_tables() {
        let listed = keys(
            r#"
            [aliases]
            gemma4 = "docker.io/ai/gemma4"

            [verify]
            default = "warn"

            [[verify.trust]]
            pattern = "docker.io/myorg/**"
            keys    = ["keys/myorg.pub", "keys/old.pub"]
            "#,
        );
        assert_eq!(
            listed,
            vec![
                "aliases.gemma4=docker.io/ai/gemma4",
                "verify.default=warn",
                "verify.trust[0].pattern=docker.io/myorg/**",
                "verify.trust[0].keys[0]=keys/myorg.pub",
                "verify.trust[0].keys[1]=keys/old.pub",
            ],
            "{listed:?}"
        );
    }

    /// An id that is not a bare TOML key is one table, not two nested
    /// ones, and the key `list` prints has to be one `set` accepts.
    #[test]
    fn a_key_needing_quotes_is_printed_and_parsed_the_way_toml_spells_it() {
        let listed = keys("[providers.\"wafer.ai\"]\napi_key = \"sk-x\"");
        assert_eq!(
            listed,
            vec![r#"providers."wafer.ai".api_key=sk-x"#],
            "{listed:?}"
        );

        let mut d = doc("");
        set_in(&mut d, r#"providers."wafer.ai".api_key"#, "sk-y").expect("set");
        assert_eq!(
            entries(&d),
            vec![(
                r#"providers."wafer.ai".api_key"#.to_string(),
                "sk-y".to_string()
            )],
            "{d}"
        );
    }

    /// Comments and unrelated sections are why anyone hand-edits this
    /// file at all.
    #[test]
    fn set_preserves_the_comments_and_layout_of_everything_it_did_not_touch() {
        let mut d = doc("# my models\n[aliases]\ngemma4 = \"docker.io/ai/gemma4\"  # local\n");
        set_in(&mut d, "aliases.qwen", "docker.io/ai/qwen").expect("set");
        set_in(&mut d, "providers.openai.api_key", "sk-new").expect("set");

        let rendered = d.to_string();
        assert!(
            rendered.starts_with("# my models\n[aliases]\n"),
            "{rendered}"
        );
        assert!(
            rendered.contains("gemma4 = \"docker.io/ai/gemma4\"  # local"),
            "{rendered}"
        );
        assert!(
            rendered.contains("qwen = \"docker.io/ai/qwen\""),
            "{rendered}"
        );
        assert!(rendered.contains("[providers.openai]"), "{rendered}");
        assert!(!rendered.contains("[providers]\n"), "{rendered}");
    }

    /// TOML would refuse a duplicate key, and the comment on the line
    /// is not the thing that changed.
    #[test]
    fn set_replaces_a_value_in_place_keeping_its_comment() {
        let mut d = doc("[verify]\ndefault = \"off\"\n");
        set_in(&mut d, "verify.default", "enforce").expect("set");
        assert_eq!(
            entries(&d),
            vec![("verify.default".to_string(), "enforce".to_string())],
            "{d}"
        );
        crate::config::parse(&d.to_string()).expect("still valid");

        let mut d = doc("[aliases]\ngemma4 = \"old\"  # what I usually pull\n");
        set_in(&mut d, "aliases.gemma4", "new").expect("set");
        assert_eq!(
            d.to_string(),
            "[aliases]\ngemma4 = \"new\"  # what I usually pull\n",
            "{d}"
        );
    }

    /// An inline table is valid `llmman.conf`, so a key `list` prints
    /// from one has to be a key `set` and `unset` can reach.
    #[test]
    fn a_section_written_as_an_inline_table_is_still_writable() {
        let mut d = doc("aliases = { gemma4 = \"docker.io/ai/gemma4\" }\n");
        assert_eq!(
            keys("aliases = { gemma4 = \"docker.io/ai/gemma4\" }"),
            vec!["aliases.gemma4=docker.io/ai/gemma4"]
        );

        set_in(&mut d, "aliases.gemma4", "docker.io/ai/gemma5").expect("set");
        set_in(&mut d, "aliases.qwen", "docker.io/ai/qwen").expect("set");
        assert_eq!(
            entries(&d),
            vec![
                (
                    "aliases.gemma4".to_string(),
                    "docker.io/ai/gemma5".to_string()
                ),
                ("aliases.qwen".to_string(), "docker.io/ai/qwen".to_string()),
            ],
            "{d}"
        );

        unset_in(&mut d, "aliases.qwen").expect("unset");
        assert_eq!(entries(&d).len(), 1, "{d}");
        crate::config::parse(&d.to_string()).expect("still valid");
    }

    /// The whole reason the command exists: a misspelling TOML accepts
    /// is a setting that silently never takes effect.
    #[test]
    fn a_key_no_section_of_the_format_has_is_rejected_before_it_is_written() {
        let mut d = doc("");
        set_in(&mut d, "providers.openai.api_kye", "sk-x").expect("toml accepts it");
        let err = crate::config::parse(&d.to_string()).expect_err("llmman.conf must not");
        assert!(err.contains("api_kye"), "{err}");

        let mut d = doc("");
        set_in(&mut d, "alias.gemma4", "docker.io/ai/gemma4").expect("toml accepts it");
        assert!(crate::config::parse(&d.to_string()).is_err(), "{d}");
    }

    /// `launch.<integration>.<flag>` can be set; a typo is refused.
    #[test]
    fn launch_defaults_are_settable_and_checked_before_they_are_written() {
        let mut d = doc("");
        for (key, value) in [
            ("launch.opencode.provider", "anthropic"),
            ("launch.opencode.model", "claude-sonnet-5-5"),
            ("launch.opencode.variant", "xhigh"),
        ] {
            set_in(&mut d, key, value).unwrap();
        }
        crate::config::parse(&d.to_string()).expect("a valid launch table");
        assert_eq!(
            keys(&d.to_string()),
            [
                "launch.opencode.provider=anthropic",
                "launch.opencode.model=claude-sonnet-5-5",
                "launch.opencode.variant=xhigh"
            ]
        );

        for (key, value) in [
            ("launch.opencod.model", "m"),
            ("launch.opencode.variant", "turbo"),
            ("launch.opencode.modle", "m"),
        ] {
            let mut d = doc("");
            set_in(&mut d, key, value).unwrap();
            assert!(crate::config::parse(&d.to_string()).is_err(), "{key}");
        }
    }

    #[test]
    fn invalid_managed_values_never_replace_the_saved_configuration() {
        let dir = scratch("managed-validation");
        let path = dir.join("llmman.conf");
        let original = "[managed]\nenabled = \"true\"\nread_timeout_seconds = \"300\"\n";
        std::fs::write(&path, original).unwrap();
        for (key, value) in [
            ("managed.enabled", "yes"),
            ("managed.enabled", ""),
            ("managed.read_timeout_seconds", "-1"),
            ("managed.read_timeout_seconds", "1.5"),
            ("managed.read_timeout_seconds", "4294967296"),
        ] {
            let mut edited = read(&path).unwrap();
            set_in(&mut edited, key, value).unwrap();
            assert!(save(&path, &edited).is_err(), "{key}={value}");
            assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
        }
        let mut edited = read(&path).unwrap();
        set_in(&mut edited, "managed.enabled", "false").unwrap();
        set_in(&mut edited, "managed.read_timeout_seconds", "0").unwrap();
        save(&path, &edited).unwrap();
        crate::config::parse(&std::fs::read_to_string(&path).unwrap()).unwrap();
        std::fs::remove_dir_all(dir).unwrap();
    }

    /// A success that quietly did nothing reports a credential removed
    /// that is still there.
    #[test]
    fn unset_reports_a_key_that_was_never_set() {
        let mut d = doc("[providers.openai]\napi_key = \"sk-x\"\n");
        let err = unset_in(&mut d, "providers.groq.api_key").expect_err("absent section");
        assert!(err.to_string().contains("not set"), "{err}");
        let err = unset_in(&mut d, "providers.openai.token").expect_err("absent key");
        assert!(err.to_string().contains("not set"), "{err}");

        unset_in(&mut d, "providers.openai.api_key").expect("set, so removable");
        assert!(entries(&d).is_empty(), "{d}");
    }

    /// A key is a value asked for by name, not one to spray across a
    /// terminal that ends up in an issue.
    #[test]
    fn only_a_credential_is_treated_as_one() {
        assert!(carries_key(&doc("[providers.openai]\napi_key = \"sk-x\"")));
        assert!(!carries_key(&doc("[providers.openai]\napi_key = \"\"")));
        assert!(!carries_key(&doc("[aliases]\napi_keyring = \"x\"")));
        assert!(!carries_key(&doc("[verify]\ndefault = \"off\"")));
    }

    /// `config::owner_readable_only` ignores the keys in a file group
    /// or other can read, so a key llmman writes must be one it reads
    /// back. A config holding no key stays readable, since
    /// `/etc/llmman/llmman.conf` has to be.
    #[cfg(unix)]
    #[test]
    fn only_a_file_holding_a_key_is_made_owner_only() {
        use std::os::unix::fs::PermissionsExt;

        let dir = scratch("mode");
        let path = dir.join("llmman.conf");
        let mode = |p: &Path| std::fs::metadata(p).expect("metadata").permissions().mode() & 0o777;

        let mut d = doc("");
        set_in(&mut d, "aliases.gemma4", "docker.io/ai/gemma4").expect("set");
        save(&path, &d).expect("save");
        assert_eq!(mode(&path), 0o644, "a new file with no key in it");

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o640)).expect("chmod");
        let mut d = read(&path).expect("read");
        set_in(&mut d, "aliases.qwen", "docker.io/ai/qwen").expect("set");
        save(&path, &d).expect("save");
        assert_eq!(mode(&path), 0o640, "a mode an admin chose is kept");

        let mut d = read(&path).expect("read");
        set_in(&mut d, "providers.openai.api_key", "sk-x").expect("set");
        save(&path, &d).expect("save");
        assert_eq!(mode(&path), 0o600, "tightened once it holds a key");

        // A key is never on disk at a loose mode, not even briefly.
        std::fs::remove_file(&path).expect("rm");
        let mut d = doc("");
        set_in(&mut d, "providers.openai.api_key", "sk-x").expect("set");
        save(&path, &d).expect("save");
        assert_eq!(mode(&path), 0o600, "a new file holding a key");

        // The daemon's own keys and the peer key are credentials too.
        for key in ["auth.api_keys", "aggregation.api_key"] {
            std::fs::remove_file(&path).expect("rm");
            let mut d = doc("");
            set_in(&mut d, key, "k").expect("set");
            save(&path, &d).expect("save");
            assert_eq!(mode(&path), 0o600, "{key}");
        }

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

    /// Two `PUT /llmman/providers/:id/key` at once: each one's key is in
    /// the file afterwards, and the file still parses.
    #[test]
    fn concurrent_provider_key_writes_keep_both() {
        let _one = crate::config::PROVIDER_KEYS_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let dir = scratch("provider-keys-race");
        let path = dir.join("llmman.conf");
        std::fs::remove_file(&path).ok();
        let ids: Vec<String> = (0..8).map(|i| format!("p{i}")).collect();
        std::thread::scope(|s| {
            for id in &ids {
                let path = &path;
                s.spawn(move || {
                    write_provider_key(path, id, Some(&format!("key-{id}"))).expect("write")
                });
            }
        });
        let conf =
            crate::config::parse(&std::fs::read_to_string(&path).expect("read")).expect("parses");
        let keys = conf.provider_keys();
        for id in &ids {
            assert_eq!(
                keys.get(id).map(String::as_str),
                Some(format!("key-{id}").as_str()),
                "{id}"
            );
        }
        std::fs::remove_dir_all(&dir).ok();
    }

    /// The per-user file a first `PUT` creates is 0600 from the start; a
    /// stricter mode an existing file has is kept; `DELETE` removes only
    /// the key, and reports when there was none.
    #[cfg(unix)]
    #[test]
    fn provider_key_writes_keep_the_file_owner_only() {
        use std::os::unix::fs::PermissionsExt;

        let _one = crate::config::PROVIDER_KEYS_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let dir = scratch("provider-keys-mode");
        let path = dir.join("llmman.conf");
        let mode = |p: &Path| std::fs::metadata(p).expect("metadata").permissions().mode() & 0o777;
        std::fs::remove_file(&path).ok();

        assert!(write_provider_key(&path, "anthropic", Some("sk-a")).expect("set"));
        assert_eq!(mode(&path), 0o600, "a new file holding a key");

        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400)).expect("chmod");
        assert!(write_provider_key(&path, "openai", Some("sk-o")).expect("set"));
        assert_eq!(mode(&path), 0o400, "a stricter mode is kept");

        assert!(write_provider_key(&path, "anthropic", None).expect("unset"));
        assert!(!write_provider_key(&path, "anthropic", None).expect("unset again"));
        let conf =
            crate::config::parse(&std::fs::read_to_string(&path).expect("read")).expect("parses");
        assert_eq!(conf.provider_keys().get("anthropic"), None);
        assert_eq!(
            conf.provider_keys().get("openai").map(String::as_str),
            Some("sk-o")
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// Most machines never have one, and `set` has to create the first.
    #[test]
    fn an_absent_file_reads_as_empty() {
        let path = std::env::temp_dir().join(format!("llmman-absent-{}.conf", std::process::id()));
        std::fs::remove_file(&path).ok();
        assert!(entries(&read(&path).expect("absent is empty")).is_empty());
    }

    /// Every `set` after the first renames over a file already there.
    /// Not `cfg(unix)`: catches a platform whose rename will not
    /// replace.
    #[test]
    fn a_second_write_replaces_the_file_in_place() {
        let dir = scratch("rewrite");
        let path = dir.join("llmman.conf");

        let mut d = doc("");
        set_in(&mut d, "aliases.gemma4", "docker.io/ai/gemma4").expect("set");
        save(&path, &d).expect("first save");

        let mut d = read(&path).expect("read back");
        set_in(&mut d, "aliases.gemma4", "docker.io/ai/gemma5").expect("set");
        save(&path, &d).expect("second save must replace, not fail");

        assert_eq!(
            entries(&read(&path).expect("read back")),
            vec![(
                "aliases.gemma4".to_string(),
                "docker.io/ai/gemma5".to_string()
            )]
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// `~/.config/llmman` may not exist yet, and an editor that cannot
    /// save takes the edit anyway — llmman would then report success
    /// over an empty file. An abandoned edit still leaves nothing.
    #[test]
    fn edit_prepares_the_directory_but_not_the_file() {
        let root = scratch("edit");
        let path = root.join("never").join("existed").join("llmman.conf");

        prepare(&path).expect("prepare");
        assert!(path.parent().expect("a parent").is_dir());
        assert!(!path.exists(), "{}", path.display());

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

    /// `$EDITOR` is a command line, not a program name: a path with a
    /// space in it and a trailing flag both have to survive.
    #[cfg(unix)]
    #[test]
    fn an_editor_with_spaces_and_flags_reaches_the_file() {
        use std::os::unix::fs::PermissionsExt;

        let dir = scratch("editor");
        let script = dir.join("my editor.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf '%s\\n' \"$1\" > \"$2\"\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).expect("chmod");
        let out = dir.join("out");

        let status = editor_command(&format!("\"{}\" --flag", script.display()), &out)
            .expect("a command")
            .status()
            .expect("run");
        assert!(status.success(), "{status}");
        assert_eq!(
            std::fs::read_to_string(&out).expect("read"),
            "--flag\n",
            "the flag and the path both arrived"
        );

        assert!(editor_command("   ", &out).is_err(), "an empty editor");
        std::fs::remove_dir_all(&dir).ok();
    }

    /// A malformed key is refused rather than treated as a literal name
    /// with a dot in it.
    #[test]
    fn a_malformed_key_is_refused() {
        assert!(parse_key("").is_err());
        assert!(parse_key("providers..api_key").is_err());
        assert!(parse_key("providers.\"unterminated").is_err());
        assert_eq!(parse_key("aliases.gemma4").expect("valid").len(), 2);
    }
}