yolop 0.3.0

Yolop — a terminal coding agent built on everruns-runtime
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
// Persistent yolop settings — preferred provider, optional per-provider
// API tokens, per-provider model picks, custom endpoint base URLs, and
// small behavior toggles.
//
// Stored at `<config_dir>/yolop/settings.toml` (`~/.config/yolop/settings.toml`
// on Linux). The `/setup` capability writes through `SettingsStore` so user
// choices survive across runs.
//
// Tokens are written with 0o600 on Unix (owner-only) and are still less
// secure than a real secret manager — they sit in plain text on disk. The
// `/setup` command can remove a stored entry. Env vars
// (OPENAI_API_KEY, ANTHROPIC_API_KEY, …) continue to take precedence so a
// per-run override is always possible.

use anyhow::{Context, Result};
use std::collections::BTreeMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use toml::Table;
use toml::Value;

/// How aggressively yolop pauses for spoken ("soft") approval before
/// running critical actions. Soft approval is prompt-engineering, not a
/// hard gate: the chosen level is injected into the system prompt so the
/// model itself decides when to ask, batches safe calls, and the user
/// approves in plain text ("yes", "approved"). See `capabilities::approval`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ApprovalMode {
    /// Ask before any state-changing action — the lowest threshold.
    Protective,
    /// Ask only before clearly destructive, irreversible, or outward-facing
    /// actions. The default.
    #[default]
    Normal,
    /// Never pause for approval; act autonomously.
    Off,
}

impl ApprovalMode {
    /// Canonical lowercase name, as written to settings.toml and shown in
    /// the status bar.
    pub fn as_str(self) -> &'static str {
        match self {
            ApprovalMode::Protective => "protective",
            ApprovalMode::Normal => "normal",
            ApprovalMode::Off => "off",
        }
    }

    /// Parse a user- or config-supplied level. Accepts the canonical names
    /// plus a few intuitive aliases so natural-language requests ("be more
    /// paranoid", "yolo mode") and config files both resolve.
    pub fn parse(raw: &str) -> Option<Self> {
        match raw.trim().to_ascii_lowercase().as_str() {
            "protective" | "strict" | "paranoid" | "high" | "careful" => Some(Self::Protective),
            "normal" | "default" | "medium" | "standard" => Some(Self::Normal),
            "off" | "none" | "yolo" | "disabled" | "low" => Some(Self::Off),
            _ => None,
        }
    }
}

impl std::fmt::Display for ApprovalMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, Clone)]
pub struct Settings {
    /// The default provider used when neither `--provider` nor an env
    /// credential forces a choice. Schema-described as `default_provider`.
    pub provider: Option<String>,
    pub tokens: BTreeMap<String, String>,
    /// Last model spec chosen per provider, in the provider-relative
    /// `model [effort]` form `/setup model` accepts. Lets a model pick
    /// survive restarts and provider switches.
    pub models: BTreeMap<String, String>,
    /// Global fallback model spec applied to the active provider when no
    /// per-provider `[models]` entry exists. Provider-relative, same form as
    /// `[models]` (`gpt-5.5 high`). A per-provider pick always wins over this.
    pub default_model: Option<String>,
    /// Endpoint base URLs keyed by provider. Today only `custom` (the
    /// generic OpenAI-compatible provider) writes here.
    pub base_urls: BTreeMap<String, String>,
    pub attribution: bool,
    /// Soft-approval paranoia level, injected into the system prompt each
    /// turn. Central, cross-session, surfaced in the status bar.
    pub approval_mode: ApprovalMode,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            provider: None,
            tokens: BTreeMap::new(),
            models: BTreeMap::new(),
            default_model: None,
            base_urls: BTreeMap::new(),
            attribution: true,
            approval_mode: ApprovalMode::Normal,
        }
    }
}

impl Settings {
    pub fn from_table(table: &Table) -> Self {
        let provider = table
            .get("provider")
            .and_then(Value::as_str)
            .map(str::to_string);
        let default_model = table
            .get("default_model")
            .and_then(Value::as_str)
            .map(str::to_string);
        let attribution = table
            .get("attribution")
            .and_then(Value::as_bool)
            .unwrap_or(true);
        let approval_mode = table
            .get("approval_mode")
            .and_then(Value::as_str)
            .and_then(ApprovalMode::parse)
            .unwrap_or_default();
        let string_map = |key: &str| {
            let mut map = BTreeMap::new();
            if let Some(t) = table.get(key).and_then(Value::as_table) {
                for (k, v) in t {
                    if let Some(s) = v.as_str() {
                        map.insert(k.clone(), s.to_string());
                    }
                }
            }
            map
        };
        Self {
            provider,
            tokens: string_map("tokens"),
            models: string_map("models"),
            default_model,
            base_urls: string_map("base_urls"),
            attribution,
            approval_mode,
        }
    }

    pub fn to_table(&self) -> Table {
        let mut table = Table::new();
        if let Some(p) = &self.provider {
            table.insert("provider".to_string(), Value::String(p.clone()));
        }
        if let Some(m) = &self.default_model {
            table.insert("default_model".to_string(), Value::String(m.clone()));
        }
        if !self.attribution {
            table.insert("attribution".to_string(), Value::Boolean(false));
        }
        // Only persist a non-default level so settings.toml stays sparse.
        if self.approval_mode != ApprovalMode::Normal {
            table.insert(
                "approval_mode".to_string(),
                Value::String(self.approval_mode.as_str().to_string()),
            );
        }
        let mut insert_map = |key: &str, map: &BTreeMap<String, String>| {
            if !map.is_empty() {
                let mut t = Table::new();
                for (k, v) in map {
                    t.insert(k.clone(), Value::String(v.clone()));
                }
                table.insert(key.to_string(), Value::Table(t));
            }
        };
        insert_map("tokens", &self.tokens);
        insert_map("models", &self.models);
        insert_map("base_urls", &self.base_urls);
        table
    }

    pub fn token_for(&self, provider: &str) -> Option<&str> {
        self.tokens.get(provider).map(String::as_str)
    }

    pub fn has_token(&self, provider: &str) -> bool {
        self.tokens.contains_key(provider)
    }

    pub fn model_for(&self, provider: &str) -> Option<&str> {
        self.models.get(provider).map(String::as_str)
    }

    pub fn default_model(&self) -> Option<&str> {
        self.default_model.as_deref()
    }

    pub fn base_url_for(&self, provider: &str) -> Option<&str> {
        self.base_urls.get(provider).map(String::as_str)
    }

    pub fn attribution_enabled(&self) -> bool {
        self.attribution
    }

    pub fn approval_mode(&self) -> ApprovalMode {
        self.approval_mode
    }
}

pub fn default_settings_path() -> Option<PathBuf> {
    dirs::config_dir().map(|p| p.join("yolop").join("settings.toml"))
}

pub fn load_from(path: &Path) -> Settings {
    let Ok(text) = std::fs::read_to_string(path) else {
        return Settings::default();
    };
    let table: Table = toml::from_str(&text).unwrap_or_default();
    Settings::from_table(&table)
}

/// Write the settings file atomically: stage into a sibling temp file,
/// `fsync`, then `rename` over the target. POSIX `rename` is atomic, so
/// concurrent readers and crashes see either the old contents or the
/// fully written new contents — never a truncated TOML. The temp file is
/// created with mode 0o600 on Unix, so the resulting file is owner-only
/// regardless of any prior file's permissions.
fn save_to(path: &Path, settings: &Settings) -> Result<()> {
    let parent = path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .map(Path::to_path_buf)
        .unwrap_or_else(|| PathBuf::from("."));
    std::fs::create_dir_all(&parent)
        .with_context(|| format!("create settings dir {}", parent.display()))?;
    let toml_text = toml::to_string(&settings.to_table()).context("serialize settings")?;

    let file_name = path
        .file_name()
        .with_context(|| format!("settings path has no file name: {}", path.display()))?;
    let mut tmp_name = std::ffi::OsString::from(".");
    tmp_name.push(file_name);
    tmp_name.push(format!(".tmp.{}", std::process::id()));
    let tmp_path = parent.join(tmp_name);

    let mut opts = std::fs::OpenOptions::new();
    opts.write(true).create(true).truncate(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    let write_result = (|| -> Result<()> {
        let mut file = opts
            .open(&tmp_path)
            .with_context(|| format!("open temp settings {}", tmp_path.display()))?;
        file.write_all(toml_text.as_bytes())
            .with_context(|| format!("write temp settings {}", tmp_path.display()))?;
        file.sync_all()
            .with_context(|| format!("sync temp settings {}", tmp_path.display()))?;
        Ok(())
    })();
    if let Err(e) = write_result {
        let _ = std::fs::remove_file(&tmp_path);
        return Err(e);
    }
    std::fs::rename(&tmp_path, path)
        .with_context(|| format!("rename {} -> {}", tmp_path.display(), path.display()))?;
    Ok(())
}

/// Thread-safe handle shared across capabilities. The cached `Settings`
/// is the source of truth in memory; mutations flush to disk via an
/// atomic temp-file + rename (see `save_to`), so readers and crashes
/// never observe a partially written settings file.
pub struct SettingsStore {
    path: PathBuf,
    inner: Mutex<Settings>,
}

impl SettingsStore {
    pub fn open(path: PathBuf) -> Self {
        let settings = load_from(&path);
        Self {
            path,
            inner: Mutex::new(settings),
        }
    }

    pub fn snapshot(&self) -> Settings {
        self.inner.lock().expect("settings lock poisoned").clone()
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn set_provider(&self, provider: Option<String>) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.provider = provider;
        save_to(&self.path, &guard)
    }

    pub fn set_attribution(&self, enabled: bool) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.attribution = enabled;
        save_to(&self.path, &guard)
    }

    pub fn set_approval_mode(&self, mode: ApprovalMode) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.approval_mode = mode;
        save_to(&self.path, &guard)
    }

    pub fn set_token(&self, provider: String, token: String) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.tokens.insert(provider, token);
        save_to(&self.path, &guard)
    }

    /// Returns whether a token was actually present before removal.
    pub fn clear_token(&self, provider: &str) -> Result<bool> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        let existed = guard.tokens.remove(provider).is_some();
        save_to(&self.path, &guard)?;
        Ok(existed)
    }

    pub fn set_model(&self, provider: String, spec: String) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.models.insert(provider, spec);
        save_to(&self.path, &guard)
    }

    /// Returns whether a per-provider model was actually present before removal.
    pub fn clear_model(&self, provider: &str) -> Result<bool> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        let existed = guard.models.remove(provider).is_some();
        save_to(&self.path, &guard)?;
        Ok(existed)
    }

    /// Set or clear the global fallback model spec (`default_model`).
    pub fn set_default_model(&self, spec: Option<String>) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.default_model = spec.filter(|s| !s.trim().is_empty());
        save_to(&self.path, &guard)
    }

    pub fn set_base_url(&self, provider: String, url: String) -> Result<()> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        guard.base_urls.insert(provider, url);
        save_to(&self.path, &guard)
    }

    /// Returns whether a base URL was actually present before removal.
    pub fn clear_base_url(&self, provider: &str) -> Result<bool> {
        let mut guard = self.inner.lock().expect("settings lock poisoned");
        let existed = guard.base_urls.remove(provider).is_some();
        save_to(&self.path, &guard)?;
        Ok(existed)
    }
}

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

    #[test]
    fn provider_roundtrip_via_disk() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("nested").join("settings.toml");
        let store = SettingsStore::open(path.clone());
        assert!(store.snapshot().provider.is_none());
        store
            .set_provider(Some("anthropic".to_string()))
            .expect("save");

        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(
            on_disk.contains("provider = \"anthropic\""),
            "expected TOML key/value, got: {on_disk}"
        );

        let reloaded = SettingsStore::open(path);
        assert_eq!(reloaded.snapshot().provider.as_deref(), Some("anthropic"));
    }

    #[test]
    fn attribution_defaults_to_enabled() {
        let settings = Settings::from_table(&Table::new());

        assert!(settings.attribution_enabled());
        assert!(!settings.to_table().contains_key("attribution"));
    }

    #[test]
    fn attribution_can_be_disabled_via_disk() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store.set_attribution(false).expect("save");

        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(
            on_disk.contains("attribution = false"),
            "expected attribution setting, got: {on_disk}"
        );

        let reloaded = SettingsStore::open(path);
        assert!(!reloaded.snapshot().attribution_enabled());
    }

    #[test]
    fn approval_mode_defaults_to_normal_and_is_omitted() {
        let settings = Settings::from_table(&Table::new());
        assert_eq!(settings.approval_mode(), ApprovalMode::Normal);
        // The default level is not written, keeping settings.toml sparse.
        assert!(!settings.to_table().contains_key("approval_mode"));
    }

    #[test]
    fn approval_mode_roundtrips_via_disk() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store
            .set_approval_mode(ApprovalMode::Protective)
            .expect("save");

        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(
            on_disk.contains("approval_mode = \"protective\""),
            "expected approval level, got: {on_disk}"
        );

        let reloaded = SettingsStore::open(path);
        assert_eq!(
            reloaded.snapshot().approval_mode(),
            ApprovalMode::Protective
        );
    }

    #[test]
    fn approval_mode_parses_canonical_names_and_aliases() {
        assert_eq!(
            ApprovalMode::parse("protective"),
            Some(ApprovalMode::Protective)
        );
        assert_eq!(
            ApprovalMode::parse("Paranoid"),
            Some(ApprovalMode::Protective)
        );
        assert_eq!(ApprovalMode::parse(" normal "), Some(ApprovalMode::Normal));
        assert_eq!(ApprovalMode::parse("yolo"), Some(ApprovalMode::Off));
        assert_eq!(ApprovalMode::parse("off"), Some(ApprovalMode::Off));
        assert!(ApprovalMode::parse("sometimes").is_none());
    }

    #[test]
    fn token_roundtrip_via_disk() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store
            .set_token("openai".to_string(), "sk-test-abc".to_string())
            .expect("save");
        store
            .set_token("anthropic".to_string(), "anthropic-key".to_string())
            .expect("save");

        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(
            on_disk.contains("[tokens]") && on_disk.contains("openai = \"sk-test-abc\""),
            "expected TOML tokens table, got: {on_disk}"
        );

        let reloaded = SettingsStore::open(path);
        assert_eq!(reloaded.snapshot().token_for("openai"), Some("sk-test-abc"));
        assert_eq!(
            reloaded.snapshot().token_for("anthropic"),
            Some("anthropic-key")
        );
        assert!(reloaded.snapshot().token_for("google").is_none());
    }

    #[test]
    fn clearing_token_removes_only_that_entry() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store
            .set_token("openai".to_string(), "sk-1".to_string())
            .expect("save");
        store
            .set_token("anthropic".to_string(), "anth-1".to_string())
            .expect("save");

        let removed = store.clear_token("openai").expect("clear");
        assert!(removed);

        let reloaded = SettingsStore::open(path);
        assert!(reloaded.snapshot().token_for("openai").is_none());
        assert_eq!(reloaded.snapshot().token_for("anthropic"), Some("anth-1"));
    }

    #[test]
    fn clearing_absent_token_reports_false_but_succeeds() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path);
        let removed = store.clear_token("openai").expect("clear");
        assert!(!removed);
    }

    #[test]
    fn model_and_base_url_roundtrip_via_disk() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store
            .set_model("openai".to_string(), "gpt-5.5 high".to_string())
            .expect("save model");
        store
            .set_base_url("custom".to_string(), "http://localhost:8000/v1".to_string())
            .expect("save base url");

        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(on_disk.contains("[models]"), "got: {on_disk}");
        assert!(on_disk.contains("[base_urls]"), "got: {on_disk}");

        let reloaded = SettingsStore::open(path);
        assert_eq!(
            reloaded.snapshot().model_for("openai"),
            Some("gpt-5.5 high")
        );
        assert_eq!(
            reloaded.snapshot().base_url_for("custom"),
            Some("http://localhost:8000/v1")
        );
        assert!(reloaded.snapshot().model_for("anthropic").is_none());
    }

    #[test]
    fn default_model_roundtrips_and_clears() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        assert!(store.snapshot().default_model().is_none());

        store
            .set_default_model(Some("claude-sonnet-4-5".to_string()))
            .expect("save default model");
        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(
            on_disk.contains("default_model = \"claude-sonnet-4-5\""),
            "expected default_model key, got: {on_disk}"
        );

        let reloaded = SettingsStore::open(path.clone());
        assert_eq!(
            reloaded.snapshot().default_model(),
            Some("claude-sonnet-4-5")
        );

        // Empty/whitespace clears, and the key drops out of the TOML.
        reloaded
            .set_default_model(Some("  ".to_string()))
            .expect("clear");
        assert!(reloaded.snapshot().default_model().is_none());
        let on_disk = std::fs::read_to_string(&path).expect("read");
        assert!(!on_disk.contains("default_model"), "got: {on_disk}");
    }

    #[test]
    fn clearing_model_reports_presence() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path);
        assert!(!store.clear_model("openai").expect("clear absent"));
        store
            .set_model("openai".to_string(), "gpt-5.5 high".to_string())
            .expect("save");
        assert!(store.clear_model("openai").expect("clear present"));
        assert!(store.snapshot().model_for("openai").is_none());
    }

    #[test]
    fn clearing_base_url_reports_presence() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path);
        assert!(!store.clear_base_url("custom").expect("clear absent"));
        store
            .set_base_url("custom".to_string(), "http://localhost:1234/v1".to_string())
            .expect("save");
        assert!(store.clear_base_url("custom").expect("clear present"));
        assert!(store.snapshot().base_url_for("custom").is_none());
    }

    #[test]
    fn missing_file_yields_default() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("absent.toml");
        let store = SettingsStore::open(path);
        assert!(store.snapshot().provider.is_none());
        assert!(store.snapshot().tokens.is_empty());
    }

    #[test]
    fn malformed_file_yields_default() {
        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        std::fs::write(&path, "this = is = not = toml").expect("write");
        let store = SettingsStore::open(path);
        assert!(store.snapshot().provider.is_none());
    }

    #[cfg(unix)]
    #[test]
    fn save_tightens_permissions_on_preexisting_file() {
        use std::os::unix::fs::PermissionsExt;

        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        // Simulate a stale file from a previous yolop version that wrote
        // settings with the default 0o644.
        std::fs::write(&path, "provider = \"openai\"\n").expect("seed");
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).expect("chmod");

        let store = SettingsStore::open(path.clone());
        store
            .set_token("openai".to_string(), "sk-new".to_string())
            .expect("save");

        let mode = std::fs::metadata(&path).expect("meta").permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }

    #[cfg(unix)]
    #[test]
    fn save_writes_owner_only_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let tmp = tempfile::tempdir().expect("tmp");
        let path = tmp.path().join("settings.toml");
        let store = SettingsStore::open(path.clone());
        store
            .set_token("openai".to_string(), "sk-test".to_string())
            .expect("save");

        let mode = std::fs::metadata(&path).expect("meta").permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }
}