Skip to main content

studio_worker/
config.rs

1//! Persistent config in `~/.config/minis-studio-worker/config.toml` (Linux/macOS)
2//! or `%APPDATA%\minis-studio-worker\config.toml` (Windows).
3//!
4//! Every load/save emits a structured tracing breadcrumb so operators
5//! can tell from `journalctl` which file the worker actually consulted
6//! (and whether the file existed, was freshly bootstrapped with
7//! defaults, or failed to read/parse).  The events deliberately omit
8//! the secret fields
9//! (`auth_token`, `registration_secret`) so logs can be shipped
10//! off-box without leaking credentials.  See `tests/config_tracing.rs`
11//! for the regression contract.
12//!
13//! What lives here vs. what's stripped from the user-editable surface:
14//!
15//! * **Operator-facing**: `api_base_url`, `vram_threshold_gb`,
16//!   `auto_start`, `auto_update_*`, `models_root`.
17//!   These are exposed in the desktop UI's Config tab.
18//! * **Internal state, persisted but not user-editable**: `worker_id`,
19//!   `auth_token`, `install_id`, `registration_request_id`,
20//!   `registration_secret`.  The auto-register flow owns them; the UI
21//!   hides them entirely.
22//! * **Engine selection**: removed.  The runtime always builds a
23//!   `MultiEngine` containing every backend compiled into this binary
24//!   and routes each job to the right one.
25
26use anyhow::{anyhow, Context, Result};
27use directories::{ProjectDirs, UserDirs};
28use parking_lot::Mutex;
29use serde::{Deserialize, Serialize};
30use std::path::{Path, PathBuf};
31
32/// Tracing target for config persistence events.  Stable so operators
33/// can filter with `RUST_LOG=studio_worker::config=debug`.
34const TRACE_TARGET: &str = "studio_worker::config";
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct Config {
38    /// Base URL of the studio API (e.g. `https://studio.minis.gg/`).
39    pub api_base_url: String,
40    /// Worker id, written on operator approval.  Cleared by
41    /// `studio-worker register --reset`.  Internal — not surfaced as
42    /// a user-editable widget.
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub worker_id: Option<String>,
45    /// Per-worker token issued at registration.  Internal — never
46    /// surfaced in the UI and redacted from log events.
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub auth_token: Option<String>,
49    /// VRAM threshold the worker reports as its max claim size, in GB.
50    pub vram_threshold_gb: f32,
51    /// Whether to auto-launch the run loop at boot via the OS service.
52    pub auto_start: bool,
53    /// Start the desktop UI minimised (taskbar only — not hidden, so
54    /// the window stays reachable even when no tray host exists).
55    /// Default `true`: a worker auto-started at login must not pop a
56    /// window over the operator's session.
57    #[serde(default = "default_start_minimised")]
58    pub start_minimised: bool,
59    /// Periodically check the release feed and auto-install newer
60    /// versions when no job is running.
61    #[serde(default = "default_auto_update_enabled")]
62    pub auto_update_enabled: bool,
63    /// How often (seconds) to check the release feed.
64    #[serde(default = "default_auto_update_interval")]
65    pub auto_update_interval_secs: u64,
66    /// GitHub Releases feed for this binary.
67    #[serde(default = "default_auto_update_feed")]
68    pub auto_update_feed: String,
69    /// Whether to upgrade to pre-release versions.
70    #[serde(default)]
71    pub auto_update_prerelease: bool,
72    /// Root directory for downloaded model files (per-engine
73    /// subdirectories: `llm/`, `stt/`, `tts/`, `image/`, `video/`).
74    /// Defaults to `~/models` (resolved at load time).
75    #[serde(default = "default_models_root_persisted")]
76    pub models_root: PathBuf,
77    /// Maximum number of WebSocket reconnect attempts before the
78    /// worker gives up and exits non-zero (relying on the service
79    /// manager to restart it).  `0` = infinite.  Defaults to `5`.
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub ws_reconnect_attempts: Option<u32>,
82    /// Per-install UUID written once on first launch.  Stable across
83    /// worker restarts so the studio can dedup pending requests.
84    /// Internal state, populated by the auto-register flow.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub install_id: Option<String>,
87    /// `requestId` returned by `POST /workers/register-request`.
88    /// Cleared on approval / rejection.  Internal.
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub registration_request_id: Option<String>,
91    /// Bearer secret presented when polling the request status.
92    /// Cleared on approval / rejection.  Internal.
93    #[serde(default, skip_serializing_if = "Option::is_none")]
94    pub registration_secret: Option<String>,
95}
96
97fn default_auto_update_enabled() -> bool {
98    true
99}
100fn default_start_minimised() -> bool {
101    true
102}
103fn default_auto_update_interval() -> u64 {
104    1800
105}
106fn default_auto_update_feed() -> String {
107    "https://api.github.com/repos/webbertakken/studio-worker/releases".into()
108}
109
110/// Resolve `~/models` for the user running the worker.  Falls back to
111/// `$TMPDIR/studio-worker-models` on the (extremely unusual) machines
112/// where `directories` can't find a home directory.
113pub fn default_models_root() -> PathBuf {
114    models_root_from(home_dir())
115}
116
117/// The running user's home directory, if `directories` can resolve it.
118/// Returns `None` on the home-less boxes the fallbacks below guard
119/// against (containers, `DynamicUser=` systemd units, minimal images).
120fn home_dir() -> Option<PathBuf> {
121    UserDirs::new().map(|d| d.home_dir().to_path_buf())
122}
123
124/// Resolve the default models root given the running user's home dir.
125/// Pure (the home dir is injected) so both the normal path and the
126/// home-less fallback are unit-testable without touching the host.
127fn models_root_from(home: Option<PathBuf>) -> PathBuf {
128    match home {
129        Some(home) => home.join("models"),
130        None => std::env::temp_dir().join("studio-worker-models"),
131    }
132}
133
134fn default_models_root_persisted() -> PathBuf {
135    default_models_root()
136}
137
138/// Resolve a leading `~` to the running user's home dir.  Stops the
139/// worker from creating a literal `~` directory on disk when the
140/// config carries an unexpanded path (most commonly: a hand-edited
141/// `models_root = "~/models"`).
142fn expand_home(path: PathBuf) -> PathBuf {
143    expand_home_with(path, home_dir())
144}
145
146/// Pure core of [`expand_home`]: the home dir is injected so the
147/// home-less branches (where the path stays unexpanded) are testable
148/// without depending on the host having a real home directory.
149fn expand_home_with(path: PathBuf, home: Option<PathBuf>) -> PathBuf {
150    let s = path.to_string_lossy();
151    if s == "~" {
152        return home.unwrap_or(path);
153    }
154    if let Some(rest) = s.strip_prefix("~/") {
155        if let Some(home) = home {
156            return home.join(rest);
157        }
158    }
159    path
160}
161
162impl Default for Config {
163    fn default() -> Self {
164        Self {
165            api_base_url: "https://studio.minis.gg/".into(),
166            worker_id: None,
167            auth_token: None,
168            vram_threshold_gb: 12.0,
169            auto_start: true,
170            start_minimised: default_start_minimised(),
171            auto_update_enabled: default_auto_update_enabled(),
172            auto_update_interval_secs: default_auto_update_interval(),
173            auto_update_feed: default_auto_update_feed(),
174            auto_update_prerelease: false,
175            models_root: default_models_root(),
176            ws_reconnect_attempts: None,
177            install_id: None,
178            registration_request_id: None,
179            registration_secret: None,
180        }
181    }
182}
183
184fn default_config_path() -> Result<PathBuf> {
185    let dirs = ProjectDirs::from("gg", "minis", "minis-studio-worker")
186        .ok_or_else(|| anyhow!("cannot resolve config directory"))?;
187    Ok(dirs.config_dir().join("config.toml"))
188}
189
190pub fn resolve_path(override_path: Option<&str>) -> Result<PathBuf> {
191    if let Some(p) = override_path {
192        Ok(PathBuf::from(p))
193    } else {
194        default_config_path()
195    }
196}
197
198pub fn load(override_path: Option<&str>) -> Result<(Config, PathBuf)> {
199    let path = resolve_path(override_path)?;
200    if !path.exists() {
201        let cfg = Config::default();
202        save(&cfg, &path)?;
203        tracing::info!(
204            target: TRACE_TARGET,
205            op = "load",
206            source = "default_created",
207            config_path = %path.display(),
208            api_base_url = %cfg.api_base_url,
209            vram_threshold_gb = cfg.vram_threshold_gb,
210            auto_start = cfg.auto_start,
211            models_root = %cfg.models_root.display(),
212            "config file missing — bootstrapped defaults"
213        );
214        return Ok((cfg, path));
215    }
216    let text = match std::fs::read_to_string(&path) {
217        Ok(text) => text,
218        Err(e) => {
219            // Mirror save()'s failure breadcrumb: an unreadable config
220            // is never silent.  The io error names the path/cause only
221            // (never file content), so it is safe to log verbatim.
222            tracing::warn!(
223                target: TRACE_TARGET,
224                op = "load",
225                config_path = %path.display(),
226                error = %e,
227                "failed to read config file"
228            );
229            return Err(e).with_context(|| format!("reading {}", path.display()));
230        }
231    };
232    let mut cfg: Config = match toml::from_str(&text) {
233        Ok(cfg) => cfg,
234        Err(e) => {
235            // Deliberately omit the parser detail: toml renders the
236            // offending source span, which can echo a secret value
237            // (e.g. an unterminated `auth_token = "...`).  The path +
238            // category keep the failure operator-visible without
239            // risking a credential leak in journalctl / Sentry.
240            tracing::warn!(
241                target: TRACE_TARGET,
242                op = "load",
243                config_path = %path.display(),
244                "config file is not valid TOML"
245            );
246            return Err(e).context("parsing config.toml");
247        }
248    };
249    cfg.models_root = expand_home(std::mem::take(&mut cfg.models_root));
250    tracing::debug!(
251        target: TRACE_TARGET,
252        op = "load",
253        source = "existing_file",
254        config_path = %path.display(),
255        api_base_url = %cfg.api_base_url,
256        vram_threshold_gb = cfg.vram_threshold_gb,
257        auto_start = cfg.auto_start,
258        models_root = %cfg.models_root.display(),
259        worker_id = cfg.worker_id.as_deref().unwrap_or("(unregistered)"),
260        has_auth_token = cfg.auth_token.is_some(),
261        "loaded config from disk"
262    );
263    Ok((cfg, path))
264}
265
266pub fn save(cfg: &Config, path: &Path) -> Result<()> {
267    match write_config(cfg, path) {
268        Ok(bytes) => {
269            tracing::debug!(
270                target: TRACE_TARGET,
271                op = "save",
272                config_path = %path.display(),
273                vram_threshold_gb = cfg.vram_threshold_gb,
274                auto_start = cfg.auto_start,
275                models_root = %cfg.models_root.display(),
276                bytes = bytes,
277                "persisted config to disk"
278            );
279            Ok(())
280        }
281        Err(e) => {
282            // Log at the source so a failed persist is never silent,
283            // regardless of whether the caller logs the returned Err
284            // (the UI Save button discards it, the auto-register flow
285            // logs it with extra context).  `error` carries an
286            // IO / serialisation message + the path only — never the
287            // config's secret fields — so this stays log-shippable.
288            tracing::warn!(
289                target: TRACE_TARGET,
290                op = "save",
291                config_path = %path.display(),
292                error = %e,
293                "failed to persist config to disk"
294            );
295            Err(e)
296        }
297    }
298}
299
300/// Side-effecting half of [`save`]: serialise + write, returning the
301/// byte count on success.  Split out so `save` can log a structured
302/// event on both the success and failure branch without duplicating
303/// the happy path.
304fn write_config(cfg: &Config, path: &Path) -> Result<usize> {
305    if let Some(parent) = path.parent() {
306        std::fs::create_dir_all(parent)
307            .with_context(|| format!("creating {}", parent.display()))?;
308    }
309    let text = toml::to_string_pretty(cfg).with_context(|| "serialising config")?;
310    let bytes = text.len();
311    write_atomic(path, text.as_bytes())?;
312    Ok(bytes)
313}
314
315/// Persist `bytes` to `path` atomically and owner-only.  The config
316/// carries the worker's identity and registration secrets
317/// (`auth_token`, `registration_secret`), so a plain `fs::write` is
318/// unsafe on two counts:
319///
320/// * **Durability**: an interrupted write (crash, power loss, full
321///   disk) truncates `path` to a half-written, unparseable file,
322///   wiping the worker's registration and forcing a fresh operator
323///   approval.  We stream into a temp file in the *same directory* (so
324///   the final step is a same-filesystem rename, which is atomic) and
325///   rename it over the target.  A failure leaves the previous config
326///   intact and drops the temp file.
327/// * **Confidentiality**: `fs::write` honours the umask and typically
328///   lands `0644`, exposing the secrets to every other local user.
329///   `tempfile` creates the temp file `0600` on Unix and `persist`
330///   keeps that mode through the rename.
331fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> {
332    use std::io::Write as _;
333    let dir = match path.parent() {
334        Some(p) if !p.as_os_str().is_empty() => p,
335        _ => Path::new("."),
336    };
337    let mut tmp = tempfile::NamedTempFile::new_in(dir)
338        .with_context(|| format!("creating temp file in {}", dir.display()))?;
339    tmp.write_all(bytes)
340        .with_context(|| "writing temp config")?;
341    tmp.as_file()
342        .sync_all()
343        .with_context(|| "flushing temp config to disk")?;
344    tmp.persist(path)
345        .map_err(|e| anyhow!("atomically replacing {}: {}", path.display(), e.error))?;
346    Ok(())
347}
348
349/// Wrap a Config in a mutex for use across the runtime.
350pub type SharedConfig = std::sync::Arc<Mutex<Config>>;
351
352pub fn shared(cfg: Config) -> SharedConfig {
353    std::sync::Arc::new(Mutex::new(cfg))
354}
355
356#[cfg(test)]
357mod tests {
358    use super::*;
359    use tempfile::tempdir;
360
361    #[test]
362    fn start_minimised_defaults_true_for_configs_predating_the_field() {
363        // Operators upgrading from a config.toml written before the
364        // field existed must get the minimised-by-default behaviour.
365        let cfg: Config = toml::from_str(
366            r#"
367            api_base_url = "https://studio.minis.gg/"
368            vram_threshold_gb = 12.0
369            auto_start = true
370            "#,
371        )
372        .unwrap();
373        assert!(cfg.start_minimised);
374    }
375
376    #[test]
377    fn default_values_are_sensible() {
378        let cfg = Config::default();
379        assert_eq!(cfg.api_base_url, "https://studio.minis.gg/");
380        assert!(cfg.auto_start);
381        assert!(
382            cfg.start_minimised,
383            "the UI must start minimised by default"
384        );
385        assert!(cfg.auto_update_enabled);
386        assert_eq!(cfg.auto_update_interval_secs, 1800);
387        assert!(!cfg.auto_update_prerelease);
388        assert!(cfg.auto_update_feed.contains("webbertakken/studio-worker"));
389        assert_eq!(cfg.vram_threshold_gb, 12.0);
390        assert!(cfg.worker_id.is_none());
391        assert!(cfg.auth_token.is_none());
392        // Models root defaults to ~/models (or a temp fallback on
393        // headless boxes without UserDirs).
394        let m = cfg.models_root.to_string_lossy().to_string();
395        assert!(m.ends_with("models") || m.contains("studio-worker-models"));
396    }
397
398    #[test]
399    fn resolve_path_uses_override_when_provided() {
400        let path = resolve_path(Some("/tmp/test-config.toml")).unwrap();
401        assert_eq!(path, PathBuf::from("/tmp/test-config.toml"));
402    }
403
404    #[test]
405    fn resolve_path_defaults_when_no_override() {
406        let path = resolve_path(None).unwrap();
407        let s = path.to_string_lossy();
408        assert!(
409            s.contains("minis-studio-worker") || s.contains("minis.gg.minis-studio-worker"),
410            "unexpected default path: {s}"
411        );
412        assert!(s.ends_with("config.toml"));
413    }
414
415    #[test]
416    fn load_creates_default_when_file_missing() {
417        let dir = tempdir().unwrap();
418        let path = dir.path().join("sub").join("config.toml");
419        let path_str = path.to_string_lossy().to_string();
420        let (cfg, returned_path) = load(Some(&path_str)).unwrap();
421        assert_eq!(returned_path, path);
422        assert_eq!(cfg.api_base_url, "https://studio.minis.gg/");
423        // File should have been written.
424        assert!(path.exists());
425    }
426
427    #[test]
428    fn round_trip_via_save_and_load_preserves_fields() {
429        let dir = tempdir().unwrap();
430        let path = dir.path().join("config.toml");
431        let cfg = Config {
432            worker_id: Some("w-123".into()),
433            auth_token: Some("tok-xyz".into()),
434            vram_threshold_gb: 24.0,
435            auto_update_prerelease: true,
436            models_root: PathBuf::from("/tmp/test-models"),
437            ..Config::default()
438        };
439        save(&cfg, &path).unwrap();
440
441        let path_str = path.to_string_lossy().to_string();
442        let (loaded, _) = load(Some(&path_str)).unwrap();
443        assert_eq!(loaded.api_base_url, cfg.api_base_url);
444        assert_eq!(loaded.worker_id, cfg.worker_id);
445        assert_eq!(loaded.auth_token, cfg.auth_token);
446        assert_eq!(loaded.vram_threshold_gb, cfg.vram_threshold_gb);
447        assert_eq!(loaded.auto_update_prerelease, cfg.auto_update_prerelease);
448        assert_eq!(loaded.models_root, cfg.models_root);
449    }
450
451    #[test]
452    fn shared_wraps_in_arc_mutex() {
453        let cfg = Config::default();
454        let shared = shared(cfg.clone());
455        let guard = shared.lock();
456        assert_eq!(guard.api_base_url, cfg.api_base_url);
457    }
458
459    #[test]
460    fn load_returns_error_on_malformed_toml() {
461        let dir = tempdir().unwrap();
462        let path = dir.path().join("config.toml");
463        std::fs::write(&path, "this :: is = not = toml = :").unwrap();
464        let path_str = path.to_string_lossy().to_string();
465        let err = load(Some(&path_str)).unwrap_err();
466        assert!(err.to_string().contains("parsing config.toml"));
467    }
468
469    #[test]
470    fn load_strips_legacy_engine_fields_silently() {
471        // Older configs had `engine`, `engines`, `auto_enabled`, `label`.
472        // serde::Deserialize on the new struct should ignore them (they
473        // aren't in the schema any more); the worker keeps running.
474        let dir = tempdir().unwrap();
475        let path = dir.path().join("config.toml");
476        let legacy = r#"
477            api_base_url = "https://example.invalid"
478            vram_threshold_gb = 8.0
479            auto_start = true
480            engine = "multi"
481            engines = ["llama", "synthetic"]
482            auto_enabled = false
483            label = "alice's rig"
484        "#;
485        std::fs::write(&path, legacy).unwrap();
486        let (cfg, _) = load(Some(&path.to_string_lossy())).unwrap();
487        assert_eq!(cfg.api_base_url, "https://example.invalid");
488        assert_eq!(cfg.vram_threshold_gb, 8.0);
489    }
490
491    #[test]
492    fn load_expands_leading_tilde_in_models_root() {
493        // Users who hand-edit `config.toml` often write `~/models`;
494        // the worker must expand it, not create a literal `~` dir.
495        let dir = tempdir().unwrap();
496        let path = dir.path().join("config.toml");
497        let raw = r#"
498            api_base_url = "https://x.invalid"
499            vram_threshold_gb = 4.0
500            auto_start = true
501            auto_update_enabled = false
502            auto_update_interval_secs = 1
503            auto_update_feed = "https://x.invalid"
504            auto_update_prerelease = false
505            models_root = "~/models-test"
506        "#;
507        std::fs::write(&path, raw).unwrap();
508        let (cfg, _) = load(Some(&path.to_string_lossy())).unwrap();
509        assert!(
510            cfg.models_root.is_absolute(),
511            "~/ should expand to an absolute path, got {}",
512            cfg.models_root.display()
513        );
514        assert!(cfg.models_root.ends_with("models-test"));
515    }
516
517    #[test]
518    fn expand_home_leaves_absolute_paths_alone() {
519        let p = PathBuf::from("/tmp/anywhere");
520        assert_eq!(expand_home(p.clone()), p);
521    }
522
523    #[test]
524    fn expand_home_handles_bare_tilde() {
525        let expanded = expand_home(PathBuf::from("~"));
526        assert!(
527            expanded.is_absolute() || expanded == Path::new("~"),
528            "bare ~ expands to home (or stays put on weird boxes), got {}",
529            expanded.display()
530        );
531    }
532
533    // The injected-home seams below pin the home-less fallback paths
534    // (containers, `DynamicUser=` systemd units, minimal images where
535    // `UserDirs::new()` returns `None`) without depending on the host's
536    // real home directory.
537
538    #[test]
539    fn models_root_from_uses_home_when_available() {
540        let home = PathBuf::from("/home/someuser");
541        assert_eq!(models_root_from(Some(home.clone())), home.join("models"));
542    }
543
544    #[test]
545    fn models_root_from_falls_back_to_tmp_without_home() {
546        assert_eq!(
547            models_root_from(None),
548            std::env::temp_dir().join("studio-worker-models")
549        );
550    }
551
552    #[test]
553    fn expand_home_with_bare_tilde_uses_injected_home() {
554        let home = PathBuf::from("/home/x");
555        assert_eq!(
556            expand_home_with(PathBuf::from("~"), Some(home.clone())),
557            home
558        );
559    }
560
561    #[test]
562    fn expand_home_with_bare_tilde_without_home_stays_put() {
563        assert_eq!(
564            expand_home_with(PathBuf::from("~"), None),
565            PathBuf::from("~")
566        );
567    }
568
569    #[test]
570    fn expand_home_with_prefix_joins_injected_home() {
571        let home = PathBuf::from("/home/x");
572        assert_eq!(
573            expand_home_with(PathBuf::from("~/models"), Some(home.clone())),
574            home.join("models")
575        );
576    }
577
578    #[test]
579    fn expand_home_with_prefix_without_home_stays_unexpanded() {
580        let p = PathBuf::from("~/models");
581        assert_eq!(expand_home_with(p.clone(), None), p);
582    }
583
584    #[test]
585    fn expand_home_with_leaves_absolute_paths_alone() {
586        let p = PathBuf::from("/tmp/anywhere");
587        assert_eq!(
588            expand_home_with(p.clone(), Some(PathBuf::from("/home/x"))),
589            p
590        );
591    }
592
593    #[cfg(unix)]
594    #[test]
595    fn save_writes_config_owner_only_because_it_holds_secrets() {
596        // config.toml persists `auth_token` + `registration_secret`.
597        // A plain `fs::write` honours the umask and typically lands
598        // `0644`, exposing those credentials to every other local
599        // user.  The atomic temp-file write must leave the file
600        // owner-only (`0600`).
601        use std::os::unix::fs::PermissionsExt;
602        let dir = tempdir().unwrap();
603        let path = dir.path().join("config.toml");
604        let cfg = Config {
605            auth_token: Some("super-secret-token".into()),
606            registration_secret: Some("reg-secret".into()),
607            ..Config::default()
608        };
609        save(&cfg, &path).unwrap();
610        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
611        assert_eq!(
612            mode & 0o077,
613            0,
614            "secrets-bearing config must not be group/world-accessible; got mode {mode:o}"
615        );
616    }
617
618    #[test]
619    fn save_atomically_replaces_existing_config_without_temp_litter() {
620        // A second save must fully replace the file (no stale fields
621        // from a longer previous version) and leave no temp-file
622        // siblings behind from the write-then-rename dance.
623        let dir = tempdir().unwrap();
624        let path = dir.path().join("config.toml");
625
626        let big = Config {
627            api_base_url: "https://a-very-long-host-name.example.invalid/studio/".into(),
628            worker_id: Some("worker-with-a-longish-id-000000".into()),
629            ..Config::default()
630        };
631        save(&big, &path).unwrap();
632
633        let small = Config {
634            api_base_url: "https://x/".into(),
635            ..Config::default()
636        };
637        save(&small, &path).unwrap();
638
639        let (loaded, _) = load(Some(&path.to_string_lossy())).unwrap();
640        assert_eq!(loaded.api_base_url, "https://x/");
641        assert!(
642            loaded.worker_id.is_none(),
643            "a replacing save must not leave the previous worker_id behind"
644        );
645
646        let names: Vec<String> = std::fs::read_dir(dir.path())
647            .unwrap()
648            .map(|e| e.unwrap().file_name().to_string_lossy().to_string())
649            .collect();
650        assert_eq!(
651            names,
652            vec!["config.toml".to_string()],
653            "atomic save must leave only the target file, found: {names:?}"
654        );
655    }
656}