Skip to main content

sysknife_core/
config.rs

1//! `~/.config/sysknife/config.toml` — optional user configuration file.
2//!
3//! # Resolution order (highest priority wins)
4//!
5//! 1. Environment variables (always win — set by the caller or systemd unit)
6//! 2. Values in `~/.config/sysknife/config.toml`
7//! 3. Built-in defaults (defined in `sysknife-brain` and `sysknife-core`)
8//!
9//! # Usage
10//!
11//! Call [`LacsConfig::load`] once at startup, then call
12//! [`LacsConfig::apply_defaults_to_env`] to populate env vars for any
13//! key that the config file sets but that is *not* already present in the
14//! environment. After that, the rest of the codebase continues reading env
15//! vars as before — no callers need to change.
16//!
17//! ```no_run
18//! use sysknife_core::config::LacsConfig;
19//!
20//! LacsConfig::load().apply_defaults_to_env();
21//! ```
22//!
23//! # Example `config.toml`
24//!
25//! ```toml
26//! [daemon]
27//! socket   = "/run/sysknife/daemon.sock"   # written as a raw path, not a URI
28//! database = "/var/lib/sysknife/daemon.sqlite"
29//!
30//! [llm]
31//! provider     = "ollama"              # "ollama" | "anthropic" | "openai" | "gemini" | ...
32//! model        = "qwen3:8b"            # default — see sysknife-brain DEFAULT_OLLAMA_MODEL
33//! ollama_url   = "http://localhost:11434"
34//! max_turns    = 10
35//! # Optional: override the auto-detected thinking mode for Ollama.
36//! # Default: auto-detect from the model name (qwen3 / qwq / deepseek-r → true).
37//! # Set to `false` on CPU-only hosts running thinking models — thinking
38//! # traces exceed Ollama's internal request timeout on 4 vCPUs.
39//! # ollama_think = false
40//! ```
41
42use std::collections::HashMap;
43use std::path::PathBuf;
44
45use serde::Deserialize;
46
47// ---------------------------------------------------------------------------
48// Types
49// ---------------------------------------------------------------------------
50
51/// Top-level structure of `~/.config/sysknife/config.toml`.
52///
53/// All fields are optional — absent sections use built-in defaults.
54#[derive(Debug, Default, Deserialize)]
55pub struct LacsConfig {
56    pub daemon: Option<DaemonSection>,
57    pub llm: Option<LlmSection>,
58    pub policy: Option<PolicySection>,
59    pub audit: Option<AuditSection>,
60    pub storage: Option<StorageSection>,
61}
62
63/// `[daemon]` section.
64#[derive(Debug, Deserialize)]
65pub struct DaemonSection {
66    /// Unix socket path (raw path, not a URI). Maps to `SYSKNIFE_LISTEN_URI`.
67    /// The loader prepends `unix://` when setting the env var.
68    pub socket: Option<String>,
69    /// SQLite database path. Maps to `SYSKNIFE_DATABASE_PATH`.
70    pub database: Option<String>,
71}
72
73/// `[storage]` section. Selects the audit-log backend.
74///
75/// Default (absent or `backend = "sqlite"`) uses the local rusqlite-backed
76/// store at `[daemon].database`. **Production deployments should set
77/// `backend = "postgres"`** and supply a connection URL — the local SQLite
78/// path is recommended only for testing and sandboxing because the audit
79/// log dies with the host (no off-box durability, nothing to forward to
80/// a SOC).
81///
82/// Example (Postgres, AWS RDS):
83///
84/// ```toml
85/// [storage]
86/// backend = "postgres"
87/// url     = "postgres://sysknife:${PG_PASSWORD}@db.example.com:5432/audit?sslmode=verify-full"
88///
89/// [storage.pool]
90/// max_connections          = 8
91/// acquire_timeout_secs     = 10
92/// statement_cache_capacity = 100   # set to 0 for transaction-mode poolers
93/// ```
94///
95/// See `docs/storage-cloud.md` for cloud-provider URL reference.
96#[derive(Debug, Default, Deserialize)]
97pub struct StorageSection {
98    /// `"sqlite"` (default) or `"postgres"`.
99    #[serde(default = "default_storage_backend")]
100    pub backend: String,
101    /// Postgres connection URL (`postgres://...`). Required when
102    /// `backend = "postgres"`. Ignored otherwise.
103    pub url: Option<String>,
104    /// Optional pool tuning. Defaults are sane for typical SysKnife load.
105    pub pool: Option<StoragePoolSection>,
106}
107
108fn default_storage_backend() -> String {
109    "sqlite".to_string()
110}
111
112/// Type-state projection of [`StorageSection`] with cross-field invariants
113/// enforced.
114///
115/// `StorageSection` is deserialized from `config.toml` with relaxed shape so
116/// existing user configs continue to load — `backend: String`, `url:
117/// Option<String>`. That means a misconfigured `backend = "postgres"` with
118/// no `url` only fails at daemon startup with a string-mismatch error, and
119/// callers downstream of the parse have to keep re-checking the
120/// invariant.
121///
122/// `StorageBackend` is the parsed form: variants carry exactly the fields
123/// they require, so it is impossible to construct a `Postgres` variant
124/// without a URL or to ask for a pool configuration in a `Sqlite` variant.
125/// Use [`StorageSection::parsed`] at the boundary; downstream code
126/// matches on the enum.
127#[derive(Debug, Clone, PartialEq, Eq)]
128pub enum StorageBackend {
129    Sqlite,
130    Postgres {
131        url: String,
132        pool: StoragePoolSection,
133    },
134}
135
136impl StorageSection {
137    /// Project the relaxed deserialised form into the type-state-checked
138    /// [`StorageBackend`] enum.  Returns a human-readable error when the
139    /// `(backend, url, pool)` tuple is inconsistent (unknown backend,
140    /// `postgres` without `url`, etc.).
141    pub fn parsed(&self) -> Result<StorageBackend, String> {
142        match self.backend.to_ascii_lowercase().as_str() {
143            "sqlite" => Ok(StorageBackend::Sqlite),
144            "postgres" => {
145                let url = self
146                    .url
147                    .as_ref()
148                    .ok_or_else(|| {
149                        "[storage] backend = \"postgres\" requires `url = \"postgres://...\"`"
150                            .to_string()
151                    })?
152                    .clone();
153                let pool = self.pool.clone().unwrap_or_default();
154                Ok(StorageBackend::Postgres { url, pool })
155            }
156            other => Err(format!(
157                "[storage] unknown backend {other:?}; expected \"sqlite\" or \"postgres\""
158            )),
159        }
160    }
161}
162
163/// `[storage.pool]` section.
164#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
165pub struct StoragePoolSection {
166    /// Max connections in the pool. Default 8.
167    pub max_connections: Option<u32>,
168    /// `acquire_timeout` for pool checkout (seconds). Default 10.
169    /// Raise above 10 if you observe Neon cold-start failures.
170    pub acquire_timeout_secs: Option<u64>,
171    /// Per-connection prepared-statement cache size. Default 100.
172    /// **Set to 0** for transaction-mode PgBouncer (including the Supabase
173    /// pooler), which rejects sqlx's named prepared statements.
174    pub statement_cache_capacity: Option<usize>,
175}
176
177/// `[audit]` section.
178///
179/// Absent → no SIEM forwarding (the daemon's local hash-chained log is the
180/// only audit sink). When present, [`AuditSection::forward`] enables one or
181/// more external receivers.
182#[derive(Debug, Default, Deserialize)]
183pub struct AuditSection {
184    pub forward: Option<AuditForwardSection>,
185}
186
187/// `[audit.forward]` section. Each subsection enables one external sink.
188///
189/// All sinks are best-effort: forwarding failures never block the daemon's
190/// audit-log INSERT. Phase 1 ships RFC 5424 syslog over UDP; CEF and
191/// NDJSON-over-TCP arrive in follow-up PRs.
192#[derive(Debug, Default, Deserialize)]
193pub struct AuditForwardSection {
194    /// `[audit.forward.syslog]` — RFC 5424 over UDP.
195    pub syslog: Option<SyslogForwardSection>,
196}
197
198/// `[audit.forward.syslog]` section.
199///
200/// Example:
201/// ```toml
202/// [audit.forward.syslog]
203/// host = "siem.internal:514"
204/// facility = 1            # 1 = user-level (default)
205/// ```
206#[derive(Debug, Deserialize)]
207pub struct SyslogForwardSection {
208    /// Receiver address (`host:port`). Mandatory.
209    pub host: String,
210    /// Syslog facility number (default 1 = user-level messages).
211    #[serde(default = "default_syslog_facility")]
212    pub facility: u8,
213}
214
215fn default_syslog_facility() -> u8 {
216    1
217}
218
219/// `[policy]` section. Currently holds per-action risk-level overrides.
220///
221/// See [`PolicySection::risk_overrides`] for semantics. Absent → no overrides
222/// (the daemon uses compile-time defaults from `sysknife-daemon::policy`).
223#[derive(Debug, Default, Deserialize)]
224pub struct PolicySection {
225    /// Per-action risk-level overrides. Map from action name → risk level
226    /// (`"Low"` | `"Medium"` | `"High"`). The daemon validates this map at
227    /// startup and rejects unknown action names or attempted downgrades.
228    ///
229    /// Overrides may only **raise** the minimum role required for an action
230    /// — never lower it. The compile-time default is a floor.
231    ///
232    /// Example:
233    ///
234    /// ```toml
235    /// [policy.risk_overrides]
236    /// InstallFlatpak = "High"   # require Admin in this org (default: Medium/Dev)
237    /// ```
238    pub risk_overrides: Option<HashMap<String, String>>,
239}
240
241/// `[llm]` section.
242#[derive(Debug, Deserialize)]
243pub struct LlmSection {
244    /// LLM provider. One of: `"anthropic"`, `"ollama"`, `"openai"`,
245    /// `"gemini"`, `"groq"`, `"deepseek"`, `"mistral"`, `"xai"`.
246    /// Maps to `SYSKNIFE_LLM_PROVIDER`.
247    pub provider: Option<String>,
248    /// Model identifier. Maps to `SYSKNIFE_LLM_MODEL`.
249    pub model: Option<String>,
250    /// Ollama base URL. Maps to `SYSKNIFE_OLLAMA_URL`.
251    pub ollama_url: Option<String>,
252    /// Anthropic base URL. Maps to `SYSKNIFE_ANTHROPIC_URL`.
253    pub anthropic_url: Option<String>,
254    /// Planning loop turn limit. Maps to `SYSKNIFE_BRAIN_MAX_TURNS`.
255    pub max_turns: Option<u32>,
256    /// Override Ollama thinking-mode auto-detection. `None` means
257    /// `sysknife-brain` decides based on the model name. Maps to
258    /// `SYSKNIFE_OLLAMA_THINK` (`"true"` or `"false"`).
259    pub ollama_think: Option<bool>,
260}
261
262// ---------------------------------------------------------------------------
263// Loading
264// ---------------------------------------------------------------------------
265
266impl LacsConfig {
267    /// Returns the path to the config file (`~/.config/sysknife/config.toml`).
268    pub fn config_path() -> PathBuf {
269        config_path()
270    }
271
272    /// Load `~/.config/sysknife/config.toml`.
273    ///
274    /// Returns `LacsConfig::default()` (all `None`) if the file is absent —
275    /// this is the normal, unconfigured happy path and is unchanged.
276    ///
277    /// If the file IS present but cannot be parsed as TOML, or exists but
278    /// cannot be read (e.g. permission denied), this is treated as a fatal
279    /// misconfiguration rather than silently falling back to defaults: a clear
280    /// message is printed to stderr and the process exits with status 1.
281    /// Silently defaulting in that case would drop `[storage]` / `[policy]`
282    /// overrides (backend selection, risk-level overrides) without any
283    /// indication to the operator — a silent security downgrade. See
284    /// `try_load` for the non-exiting core used by tests.
285    pub fn load() -> Self {
286        let path = config_path();
287        Self::try_load(&path).unwrap_or_else(|reason| {
288            eprintln!("[sysknife] FATAL: {reason}");
289            std::process::exit(1);
290        })
291    }
292
293    /// Core of [`Self::load`], factored out so the parse-error / read-error
294    /// paths can be exercised in tests without triggering `process::exit`.
295    ///
296    /// Returns `Ok(Self::default())` when `path` does not exist. Returns
297    /// `Err` for any other failure (present-but-unparseable, or a read
298    /// error) — see [`Self::load`] for why this must never silently
299    /// default instead.
300    fn try_load(path: &std::path::Path) -> Result<Self, String> {
301        match std::fs::read_to_string(path) {
302            Ok(content) => toml::from_str(&content).map_err(|e| {
303                format!(
304                    "config file {} is present but could not be parsed as TOML: {e}; \
305                     refusing to silently fall back to defaults",
306                    path.display()
307                )
308            }),
309            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
310            Err(e) => Err(format!(
311                "config file {} exists but could not be read: {e}; refusing to \
312                 silently fall back to defaults",
313                path.display()
314            )),
315        }
316    }
317
318    /// Set environment variables from the config file for any key that is NOT
319    /// already present in the process environment.
320    ///
321    /// # Safety note
322    ///
323    /// This must be called during single-threaded startup, before the async
324    /// runtime or any thread pool is initialised. Modifying env vars while
325    /// other threads are reading them is undefined behaviour. Both `main.rs`
326    /// (daemon) and the Tauri `setup` hook (shell) satisfy this contract.
327    pub fn apply_defaults_to_env(&self) {
328        if let Some(daemon) = &self.daemon {
329            if let Some(socket) = &daemon.socket {
330                // Accept a raw path like `/run/sysknife/daemon.sock` and convert
331                // to the URI format the daemon expects.
332                let uri = if socket.starts_with("unix://") {
333                    socket.clone()
334                } else {
335                    format!("unix://{socket}")
336                };
337                set_if_absent("SYSKNIFE_LISTEN_URI", &uri);
338            }
339            if let Some(db) = &daemon.database {
340                set_if_absent("SYSKNIFE_DATABASE_PATH", db);
341            }
342        }
343
344        if let Some(llm) = &self.llm {
345            if let Some(provider) = &llm.provider {
346                set_if_absent("SYSKNIFE_LLM_PROVIDER", provider);
347            }
348            if let Some(model) = &llm.model {
349                set_if_absent("SYSKNIFE_LLM_MODEL", model);
350            }
351            if let Some(url) = &llm.ollama_url {
352                set_if_absent("SYSKNIFE_OLLAMA_URL", url);
353            }
354            if let Some(url) = &llm.anthropic_url {
355                set_if_absent("SYSKNIFE_ANTHROPIC_URL", url);
356            }
357            if let Some(turns) = llm.max_turns {
358                set_if_absent("SYSKNIFE_BRAIN_MAX_TURNS", &turns.to_string());
359            }
360            if let Some(think) = llm.ollama_think {
361                // `sysknife-brain::planner::resolve_ollama_think` parses
362                // case-insensitive "true"/"false"; emit the canonical
363                // form for clarity in ps/systemctl output.
364                set_if_absent(
365                    "SYSKNIFE_OLLAMA_THINK",
366                    if think { "true" } else { "false" },
367                );
368            }
369        }
370    }
371}
372
373// ---------------------------------------------------------------------------
374// Helpers
375// ---------------------------------------------------------------------------
376
377/// Returns `~/.config/sysknife`, respecting `XDG_CONFIG_HOME` if set.
378///
379/// `XDG_CONFIG_HOME` is accepted only if it is an absolute path with no `..`
380/// components, to prevent path-traversal attacks where an attacker sets
381/// `XDG_CONFIG_HOME=/etc` to redirect config and prefs writes to system
382/// directories. Invalid values are ignored and the default `~/.config` is used.
383///
384/// **Panics** when both `XDG_CONFIG_HOME` (or its validated form) and `HOME`
385/// are unset.  The previous behaviour silently fell back to `./.config`
386/// (relative to the daemon's CWD), which routinely surprised operators —
387/// the daemon would write its prefs file under whatever directory the
388/// systemd unit happened to be working in, often `/tmp` on a fresh boot.
389/// Failing loudly at the first call site forces the deployment manifest
390/// (or shell environment) to make HOME explicit.  Production
391/// daemon/shell paths already do — every systemd unit ships with
392/// `Environment=HOME=…` — so this panic is the right place to surface
393/// the misconfiguration.
394fn config_dir() -> PathBuf {
395    // Validate XDG_CONFIG_HOME: must be absolute and contain no `..` components.
396    let xdg_valid = std::env::var("XDG_CONFIG_HOME").ok().and_then(|v| {
397        let p = PathBuf::from(&v);
398        if p.is_absolute() && !p.components().any(|c| c == std::path::Component::ParentDir) {
399            Some(p)
400        } else {
401            eprintln!(
402                "[sysknife] warning: XDG_CONFIG_HOME={v:?} is not a safe absolute path; \
403                 ignoring and using default ~/.config"
404            );
405            None
406        }
407    });
408
409    let base = xdg_valid.unwrap_or_else(|| match std::env::var("HOME") {
410        Ok(home) => PathBuf::from(home).join(".config"),
411        Err(_) => panic!(
412            "[sysknife] FATAL: HOME and XDG_CONFIG_HOME are both unset — refusing \
413             to fall back to ./.config (relative paths produce ./.config/sysknife/ \
414             under whatever CWD the daemon was launched in, typically a tempdir or \
415             worse).  Set `HOME` (or `XDG_CONFIG_HOME` to an absolute path) in the \
416             systemd unit / shell session and try again."
417        ),
418    });
419    base.join("sysknife")
420}
421
422/// Returns the path to `~/.config/sysknife/config.toml`, respecting
423/// `XDG_CONFIG_HOME` if set.
424pub fn config_path() -> PathBuf {
425    config_dir().join("config.toml")
426}
427
428/// Returns the path to `~/.config/sysknife/prefs.md`, respecting
429/// `XDG_CONFIG_HOME` if set. Same directory as `config.toml`.
430pub fn prefs_path() -> PathBuf {
431    config_dir().join("prefs.md")
432}
433
434/// Set `key` to `value` only if `key` is absent from the process environment.
435fn set_if_absent(key: &str, value: &str) {
436    if std::env::var_os(key).is_none() {
437        // SAFETY: single-threaded startup — no other threads are reading env
438        // vars yet. See `apply_defaults_to_env` safety note.
439        unsafe {
440            std::env::set_var(key, value);
441        }
442    }
443}
444
445// ---------------------------------------------------------------------------
446// Tests
447// ---------------------------------------------------------------------------
448
449#[cfg(test)]
450mod tests {
451    use super::*;
452
453    #[test]
454    fn storage_section_parses_sqlite_default() {
455        let s = StorageSection {
456            backend: "sqlite".to_string(),
457            url: None,
458            pool: None,
459        };
460        assert_eq!(s.parsed().unwrap(), StorageBackend::Sqlite);
461    }
462
463    #[test]
464    fn storage_section_parses_sqlite_case_insensitive() {
465        let s = StorageSection {
466            backend: "Sqlite".to_string(),
467            url: None,
468            pool: None,
469        };
470        assert_eq!(s.parsed().unwrap(), StorageBackend::Sqlite);
471    }
472
473    #[test]
474    fn storage_section_parses_postgres_with_url() {
475        let s = StorageSection {
476            backend: "postgres".to_string(),
477            url: Some("postgres://user@host/db".to_string()),
478            pool: None,
479        };
480        match s.parsed().unwrap() {
481            StorageBackend::Postgres { url, pool } => {
482                assert_eq!(url, "postgres://user@host/db");
483                assert_eq!(pool, StoragePoolSection::default());
484            }
485            other => panic!("expected Postgres, got {other:?}"),
486        }
487    }
488
489    #[test]
490    fn storage_section_rejects_postgres_without_url() {
491        let s = StorageSection {
492            backend: "postgres".to_string(),
493            url: None,
494            pool: None,
495        };
496        let err = s.parsed().unwrap_err();
497        assert!(err.contains("requires `url"), "got: {err}");
498    }
499
500    #[test]
501    fn storage_section_rejects_unknown_backend() {
502        let s = StorageSection {
503            backend: "sqlit".to_string(), // typo
504            url: None,
505            pool: None,
506        };
507        let err = s.parsed().unwrap_err();
508        assert!(err.contains("unknown backend"), "got: {err}");
509    }
510
511    #[test]
512    fn load_returns_default_when_file_absent() {
513        // XDG_CONFIG_HOME pointing to a temp dir with no sysknife/config.toml
514        let dir = tempfile::tempdir().unwrap();
515        // Temporarily override XDG_CONFIG_HOME in this process.
516        // Tests that mutate env vars must not run in parallel — use a mutex.
517        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
518        unsafe {
519            std::env::set_var("XDG_CONFIG_HOME", dir.path());
520        }
521        let cfg = LacsConfig::load();
522        unsafe {
523            std::env::remove_var("XDG_CONFIG_HOME");
524        }
525        assert!(cfg.daemon.is_none());
526        assert!(cfg.llm.is_none());
527    }
528
529    #[test]
530    fn try_load_returns_default_when_file_absent() {
531        // No env-var mutation needed: try_load takes the path directly.
532        let dir = tempfile::tempdir().unwrap();
533        let path = dir.path().join("does-not-exist").join("config.toml");
534        let cfg = LacsConfig::try_load(&path).expect("absent file must be Ok(default)");
535        assert!(cfg.daemon.is_none());
536        assert!(cfg.llm.is_none());
537        assert!(cfg.storage.is_none());
538        assert!(cfg.policy.is_none());
539    }
540
541    #[test]
542    fn try_load_fails_loud_on_present_but_unparseable_file() {
543        // A present-but-broken config file must error, not silently fall
544        // back to defaults (which would drop [storage]/[policy] overrides
545        // without any indication to the operator).
546        let dir = tempfile::tempdir().unwrap();
547        let path = dir.path().join("config.toml");
548        std::fs::write(&path, "this is [ not valid toml at all").unwrap();
549
550        let result = LacsConfig::try_load(&path);
551        assert!(
552            result.is_err(),
553            "a present-but-unparseable config file must error, not silently default"
554        );
555        let err = result.unwrap_err();
556        assert!(
557            err.contains("could not be parsed"),
558            "error should explain the parse failure; got: {err}"
559        );
560    }
561
562    #[test]
563    fn load_parses_full_config() {
564        let dir = tempfile::tempdir().unwrap();
565        let sysknife_dir = dir.path().join("sysknife");
566        std::fs::create_dir_all(&sysknife_dir).unwrap();
567        std::fs::write(
568            sysknife_dir.join("config.toml"),
569            r#"
570[daemon]
571socket   = "/run/sysknife/daemon.sock"
572database = "/var/lib/sysknife/daemon.sqlite"
573
574[llm]
575provider     = "ollama"
576model        = "llama3.2"
577ollama_url   = "http://localhost:11434"
578max_turns    = 7
579"#,
580        )
581        .unwrap();
582
583        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
584        unsafe {
585            std::env::set_var("XDG_CONFIG_HOME", dir.path());
586        }
587        let cfg = LacsConfig::load();
588        unsafe {
589            std::env::remove_var("XDG_CONFIG_HOME");
590        }
591
592        let daemon = cfg.daemon.expect("daemon section missing");
593        assert_eq!(daemon.socket.as_deref(), Some("/run/sysknife/daemon.sock"));
594        assert_eq!(
595            daemon.database.as_deref(),
596            Some("/var/lib/sysknife/daemon.sqlite")
597        );
598
599        let llm = cfg.llm.expect("llm section missing");
600        assert_eq!(llm.provider.as_deref(), Some("ollama"));
601        assert_eq!(llm.model.as_deref(), Some("llama3.2"));
602        assert_eq!(llm.max_turns, Some(7));
603    }
604
605    #[test]
606    fn apply_defaults_does_not_override_existing_env() {
607        let dir = tempfile::tempdir().unwrap();
608        let sysknife_dir = dir.path().join("sysknife");
609        std::fs::create_dir_all(&sysknife_dir).unwrap();
610        std::fs::write(
611            sysknife_dir.join("config.toml"),
612            r#"
613[llm]
614provider = "anthropic"
615"#,
616        )
617        .unwrap();
618
619        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
620        unsafe {
621            std::env::set_var("XDG_CONFIG_HOME", dir.path());
622            // Pre-set the env var — config file must NOT override it.
623            std::env::set_var("SYSKNIFE_LLM_PROVIDER", "ollama");
624        }
625        let cfg = LacsConfig::load();
626        cfg.apply_defaults_to_env();
627        let provider = std::env::var("SYSKNIFE_LLM_PROVIDER").unwrap();
628        unsafe {
629            std::env::remove_var("XDG_CONFIG_HOME");
630            std::env::remove_var("SYSKNIFE_LLM_PROVIDER");
631        }
632        assert_eq!(provider, "ollama", "env var must win over config file");
633    }
634
635    #[test]
636    fn socket_path_gets_unix_prefix() {
637        let dir = tempfile::tempdir().unwrap();
638        let sysknife_dir = dir.path().join("sysknife");
639        std::fs::create_dir_all(&sysknife_dir).unwrap();
640        std::fs::write(
641            sysknife_dir.join("config.toml"),
642            r#"
643[daemon]
644socket = "/tmp/test.sock"
645"#,
646        )
647        .unwrap();
648
649        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
650        unsafe {
651            std::env::set_var("XDG_CONFIG_HOME", dir.path());
652            std::env::remove_var("SYSKNIFE_LISTEN_URI");
653        }
654        let cfg = LacsConfig::load();
655        cfg.apply_defaults_to_env();
656        let uri = std::env::var("SYSKNIFE_LISTEN_URI").unwrap();
657        unsafe {
658            std::env::remove_var("XDG_CONFIG_HOME");
659            std::env::remove_var("SYSKNIFE_LISTEN_URI");
660        }
661        assert_eq!(uri, "unix:///tmp/test.sock");
662    }
663
664    #[test]
665    fn ollama_think_false_emits_env_var() {
666        let dir = tempfile::tempdir().unwrap();
667        let sysknife_dir = dir.path().join("sysknife");
668        std::fs::create_dir_all(&sysknife_dir).unwrap();
669        std::fs::write(
670            sysknife_dir.join("config.toml"),
671            r#"
672[llm]
673ollama_think = false
674"#,
675        )
676        .unwrap();
677
678        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
679        unsafe {
680            std::env::set_var("XDG_CONFIG_HOME", dir.path());
681            std::env::remove_var("SYSKNIFE_OLLAMA_THINK");
682        }
683        let cfg = LacsConfig::load();
684        cfg.apply_defaults_to_env();
685        let think = std::env::var("SYSKNIFE_OLLAMA_THINK").unwrap();
686        unsafe {
687            std::env::remove_var("XDG_CONFIG_HOME");
688            std::env::remove_var("SYSKNIFE_OLLAMA_THINK");
689        }
690        assert_eq!(think, "false");
691    }
692
693    #[test]
694    fn ollama_think_true_emits_env_var() {
695        let dir = tempfile::tempdir().unwrap();
696        let sysknife_dir = dir.path().join("sysknife");
697        std::fs::create_dir_all(&sysknife_dir).unwrap();
698        std::fs::write(
699            sysknife_dir.join("config.toml"),
700            r#"
701[llm]
702ollama_think = true
703"#,
704        )
705        .unwrap();
706
707        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
708        unsafe {
709            std::env::set_var("XDG_CONFIG_HOME", dir.path());
710            std::env::remove_var("SYSKNIFE_OLLAMA_THINK");
711        }
712        let cfg = LacsConfig::load();
713        cfg.apply_defaults_to_env();
714        let think = std::env::var("SYSKNIFE_OLLAMA_THINK").unwrap();
715        unsafe {
716            std::env::remove_var("XDG_CONFIG_HOME");
717            std::env::remove_var("SYSKNIFE_OLLAMA_THINK");
718        }
719        assert_eq!(think, "true");
720    }
721
722    #[test]
723    fn ollama_think_absent_does_not_set_env_var() {
724        let dir = tempfile::tempdir().unwrap();
725        let sysknife_dir = dir.path().join("sysknife");
726        std::fs::create_dir_all(&sysknife_dir).unwrap();
727        std::fs::write(
728            sysknife_dir.join("config.toml"),
729            r#"
730[llm]
731model = "qwen3:8b"
732"#,
733        )
734        .unwrap();
735
736        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
737        unsafe {
738            std::env::set_var("XDG_CONFIG_HOME", dir.path());
739            std::env::remove_var("SYSKNIFE_OLLAMA_THINK");
740        }
741        let cfg = LacsConfig::load();
742        cfg.apply_defaults_to_env();
743        let think_set = std::env::var_os("SYSKNIFE_OLLAMA_THINK").is_some();
744        unsafe {
745            std::env::remove_var("XDG_CONFIG_HOME");
746        }
747        assert!(!think_set, "absent ollama_think must not set the env var");
748    }
749
750    #[test]
751    fn prefs_path_lives_alongside_config() {
752        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
753        let prefs = prefs_path();
754        let config = config_path();
755        assert_eq!(prefs.parent(), config.parent());
756        assert_eq!(prefs.file_name().unwrap(), "prefs.md");
757    }
758
759    /// XDG_CONFIG_HOME validation must reject obvious traversal attempts
760    /// (relative paths, `..` components).  The remaining attack surface is a
761    /// **symlink-based** redirect — an attacker who cannot write to
762    /// XDG_CONFIG_HOME but CAN create a symlink at the resolved path.  We
763    /// don't dereference symlinks today: this regression test pins that
764    /// behaviour so a future change either documents the trade-off or
765    /// upgrades to canonicalising the path.
766    #[test]
767    #[cfg(unix)]
768    fn xdg_config_home_does_not_follow_symlinks() {
769        use std::os::unix::fs::symlink;
770        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
771
772        let dir = tempfile::tempdir().unwrap();
773        let real = dir.path().join("real");
774        let link = dir.path().join("link");
775        std::fs::create_dir_all(&real).unwrap();
776        symlink(&real, &link).unwrap();
777
778        let prev = std::env::var("XDG_CONFIG_HOME").ok();
779        unsafe {
780            std::env::set_var("XDG_CONFIG_HOME", &link);
781        }
782
783        let resolved = config_path();
784
785        // Restore env before asserting so a panic does not leak state.
786        match prev {
787            Some(v) => unsafe { std::env::set_var("XDG_CONFIG_HOME", v) },
788            None => unsafe { std::env::remove_var("XDG_CONFIG_HOME") },
789        }
790
791        // Today: symlinks ARE accepted (resolver only rejects relative + `..`),
792        // and the returned path keeps the symlink form. If a future change
793        // adds canonicalize(), this assert will need updating to point at
794        // `real` and the symlink-redirect threat will be neutralised.
795        assert!(
796            resolved.starts_with(&link),
797            "current resolver keeps the symlink path verbatim — got {resolved:?}"
798        );
799    }
800
801    /// MD-12 — `config_dir` must panic loudly when both `HOME` and
802    /// `XDG_CONFIG_HOME` are unset.  The previous behaviour silently fell
803    /// back to `./.config`, which writes prefs under whatever CWD the
804    /// daemon happened to be launched in (often a tempdir) — a class of
805    /// data-loss footgun the operator never sees until rotation moves
806    /// CWD.  Fail loud at startup instead.
807    #[test]
808    fn config_dir_panics_when_home_and_xdg_config_home_are_both_unset() {
809        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
810        let prev_home = std::env::var("HOME").ok();
811        let prev_xdg = std::env::var("XDG_CONFIG_HOME").ok();
812        unsafe {
813            std::env::remove_var("HOME");
814            std::env::remove_var("XDG_CONFIG_HOME");
815        }
816
817        let result = std::panic::catch_unwind(config_dir);
818
819        // Restore env before any assertion so a failed test cannot leak
820        // state into sibling tests in the same process.
821        unsafe {
822            match prev_home {
823                Some(v) => std::env::set_var("HOME", v),
824                None => std::env::remove_var("HOME"),
825            }
826            match prev_xdg {
827                Some(v) => std::env::set_var("XDG_CONFIG_HOME", v),
828                None => std::env::remove_var("XDG_CONFIG_HOME"),
829            }
830        }
831
832        let panic = result
833            .expect_err("config_dir() must panic when both HOME and XDG_CONFIG_HOME are unset");
834        let msg: String = panic
835            .downcast::<String>()
836            .map(|b| *b)
837            .or_else(|p| p.downcast::<&'static str>().map(|b| (*b).to_string()))
838            .unwrap_or_else(|_| "<non-string panic>".to_string());
839        assert!(
840            msg.contains("HOME") && msg.contains("XDG_CONFIG_HOME"),
841            "panic message must mention both env vars; got: {msg}"
842        );
843    }
844
845    use std::sync::Mutex;
846    static ENV_LOCK: Mutex<()> = Mutex::new(());
847}