From cb38ee2184eb73eae373aff77454edbfb7641991 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 08:06:47 -0600 Subject: [PATCH] fix(atlas-core): sinks' default_dir honours the HOME contract JsonlTraceSink::default_dir and JsonlFeedbackSink::default_dir fell back to a CWD-relative path when HOME was missing or empty, landing trace/feedback state wherever the process happened to run. That is exactly what the home-resolution contract (issues #5640 / #5762) forbids; every other atlas-core home-rooted path fails loudly naming HOME. Both sinks now resolve through a shared crate-internal require_home() helper implementing the contract, with unit tests for the missing/empty/present cases. Part 3 of #13279 (Atlas CLI family consistency). Parts 1-2 (exit-code philosophy, version strings) are recorded on the issue as the remaining scope. --- .../crates/atlas-core/src/feedback.rs | 14 +++--- .../atlas-search/crates/atlas-core/src/lib.rs | 49 +++++++++++++++++++ .../crates/atlas-core/src/trace.rs | 13 +++-- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/pkgs/atlas-search/crates/atlas-core/src/feedback.rs b/pkgs/atlas-search/crates/atlas-core/src/feedback.rs index 44ec04c454..e148628c9c 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/feedback.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/feedback.rs @@ -262,14 +262,14 @@ impl JsonlFeedbackSink { &self.dir } - /// `~/.local/state/atlas-search/feedback` — the same state - /// root as the trace sink and the service config. + /// `~/.local/state/atlas-search/feedback` - the same state + /// root as the trace sink and the service config. Resolved + /// through the crate's home-resolution contract (issues + /// #5640 / #5762): a missing or empty HOME fails loudly + /// naming HOME, never a CWD-relative substitute + /// (issue #13279). pub fn default_dir() -> PathBuf { - let home = std::env::var_os("HOME") - .filter(|h| !h.is_empty()) - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from(".")); - home.join(".local/state/atlas-search/feedback") + crate::require_home().join(".local/state/atlas-search/feedback") } } diff --git a/pkgs/atlas-search/crates/atlas-core/src/lib.rs b/pkgs/atlas-search/crates/atlas-core/src/lib.rs index 7c25098ccf..e48428de09 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/lib.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/lib.rs @@ -147,3 +147,52 @@ pub use trace_stats::{ }; pub use turso::TursoBackend; pub use types::{BranchQuery, DEFAULT_MODEL, DEFAULT_VEC_TABLE, VectorBackend}; + +// ------------------------------------------------------------- home ---- + +/// Home-resolution contract (issues #5640 / #5762): a missing or +/// empty HOME is an error naming HOME, never a substitute +/// directory. Every atlas-core path rooted at the user's home goes +/// through here so the contract cannot drift per call site - the +/// trace and feedback sinks' `default_dir` once fell back to the +/// CWD (issue #13279), landing state files wherever the process +/// happened to run. +pub(crate) fn require_home() -> std::path::PathBuf { + home_from(std::env::var_os("HOME")) +} + +fn home_from(home: Option) -> std::path::PathBuf { + match home { + Some(home) if !home.is_empty() => std::path::PathBuf::from(home), + _ => panic!( + "HOME is not set or is empty: atlas-core requires a non-empty HOME to resolve its state/config paths (issue #5640)" + ), + } +} + +#[cfg(test)] +mod home_tests { + use super::home_from; + use std::ffi::OsString; + use std::path::PathBuf; + + #[test] + fn home_from_accepts_a_non_empty_home() { + assert_eq!( + home_from(Some(OsString::from("/home/someone"))), + PathBuf::from("/home/someone") + ); + } + + #[test] + #[should_panic(expected = "HOME is not set or is empty")] + fn home_from_rejects_a_missing_home() { + home_from(None); + } + + #[test] + #[should_panic(expected = "HOME is not set or is empty")] + fn home_from_rejects_an_empty_home() { + home_from(Some(OsString::new())); + } +} diff --git a/pkgs/atlas-search/crates/atlas-core/src/trace.rs b/pkgs/atlas-search/crates/atlas-core/src/trace.rs index 1c4fb455c9..4adc40e58f 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/trace.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/trace.rs @@ -103,14 +103,13 @@ impl JsonlTraceSink { JsonlTraceSink { dir } } - /// `~/.local/state/atlas-search/traces` — the same state root - /// as the service config and model store. + /// `~/.local/state/atlas-search/traces` - the same state root + /// as the service config and model store. Resolved through + /// the crate's home-resolution contract (issues #5640 / + /// #5762): a missing or empty HOME fails loudly naming HOME, + /// never a CWD-relative substitute (issue #13279). pub fn default_dir() -> PathBuf { - let home = std::env::var_os("HOME") - .filter(|h| !h.is_empty()) - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from(".")); - home.join(".local/state/atlas-search/traces") + crate::require_home().join(".local/state/atlas-search/traces") } }