From b3b1528e1fcc489572c88f63cd7b7ae676f451ac Mon Sep 17 00:00:00 2001 From: Kalvin C Date: Thu, 6 Aug 2026 21:05:31 -0700 Subject: [PATCH] fix(agent): resolve oauth cache home cross-platform Co-authored-by: Kalvin C Signed-off-by: Kalvin C --- Cargo.lock | 1 + crates/buzz-agent/Cargo.toml | 1 + crates/buzz-agent/src/auth.rs | 28 +++++++++++++++------------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73ecb249d4..83d2b9e3e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -862,6 +862,7 @@ dependencies = [ "async-trait", "axum", "base64 0.22.1", + "dirs", "getrandom 0.4.3", "hex", "nix 0.31.3", diff --git a/crates/buzz-agent/Cargo.toml b/crates/buzz-agent/Cargo.toml index 7889ad34a7..f1a1089046 100644 --- a/crates/buzz-agent/Cargo.toml +++ b/crates/buzz-agent/Cargo.toml @@ -43,6 +43,7 @@ hex = { workspace = true } sha2 = { workspace = true } urlencoding = "2" webbrowser = "1" +dirs = "6" [target.'cfg(unix)'.dependencies] nix = { version = "0.31", default-features = false, features = ["signal", "process"] } diff --git a/crates/buzz-agent/src/auth.rs b/crates/buzz-agent/src/auth.rs index 34974fbf0b..3f43925de3 100644 --- a/crates/buzz-agent/src/auth.rs +++ b/crates/buzz-agent/src/auth.rs @@ -453,15 +453,12 @@ fn cache_path_for(cfg: &PkceOAuthConfig) -> Result { let dir = match &cfg.cache_dir_override { Some(p) => p.join(&cfg.cache_namespace), - None => { - let home = std::env::var("HOME") - .map_err(|_| AgentError::Llm("oauth cache: $HOME not set".into()))?; - PathBuf::from(home) - .join(".config") - .join("buzz-agent") - .join("oauth") - .join(&cfg.cache_namespace) - } + None => dirs::home_dir() + .ok_or_else(|| AgentError::Llm("oauth cache: home directory not found".into()))? + .join(".config") + .join("buzz-agent") + .join("oauth") + .join(&cfg.cache_namespace), }; Ok(dir.join(format!("{hash}.json"))) } @@ -683,8 +680,7 @@ mod tests { } #[test] - fn cache_path_includes_namespace_and_hash() { - // HOME is required; cargo test runs set it. + fn cache_path_uses_platform_home_directory() { let cfg = PkceOAuthConfig { discovery_url: "https://example.com/.well-known".into(), client_id: "abc".into(), @@ -693,8 +689,14 @@ mod tests { cache_dir_override: None, }; let p = cache_path_for(&cfg).unwrap(); - assert!(p.to_string_lossy().contains("/buzz-agent/oauth/demo/")); - assert!(p.extension().and_then(|s| s.to_str()) == Some("json")); + let expected_dir = dirs::home_dir() + .unwrap() + .join(".config") + .join("buzz-agent") + .join("oauth") + .join("demo"); + assert_eq!(p.parent(), Some(expected_dir.as_path())); + assert_eq!(p.extension().and_then(|s| s.to_str()), Some("json")); } #[test]