Skip to main content

ai_usagebar/
cache.rs

1//! Per-vendor on-disk cache with atomic writes, TTL checks, and inter-process
2//! locking.
3//!
4//! Mirrors claudebar's cache layout but per-vendor:
5//!   `~/.cache/ai-usagebar/<vendor>/usage.json`         payload
6//!   `~/.cache/ai-usagebar/<vendor>/.stale`             marker (cache is stale)
7//!   `~/.cache/ai-usagebar/<vendor>/.last_error`        HTTP code\nmessage
8//!   `~/.cache/ai-usagebar/<vendor>/.fetch.lock`        flock target
9//!
10//! Multi-monitor safety: callers should `acquire_lock()` before the refresh+
11//! fetch window, mirroring claudebar:402-407's `exec 9>"$_lockfile" / flock`.
12
13use std::fs::{self, File, OpenOptions};
14use std::io::{Read, Write};
15use std::path::{Path, PathBuf};
16use std::time::{Duration, SystemTime};
17
18use fs2::FileExt;
19
20use crate::error::{AppError, Result};
21
22/// Default TTL — claudebar's `CACHE_TTL=60`.
23pub const DEFAULT_TTL: Duration = Duration::from_secs(60);
24
25/// Maximum staleness before we refuse to serve cached data even on failure.
26/// Mirrors claudebar's `WEEKLY_WINDOW` (7 days).
27pub const MAX_STALE: Duration = Duration::from_secs(7 * 24 * 3600);
28
29/// Per-vendor cache directory and helper API.
30///
31/// Construct with [`Cache::for_vendor`]; the directory is created lazily.
32#[derive(Debug, Clone)]
33pub struct Cache {
34    dir: PathBuf,
35}
36
37impl Cache {
38    /// Build a cache rooted at `~/.cache/ai-usagebar/<vendor>` (or under
39    /// `$XDG_CACHE_HOME` when set).
40    pub fn for_vendor(vendor: &str) -> Result<Self> {
41        let base = xdg_cache_dir()?.join("ai-usagebar").join(vendor);
42        Ok(Self { dir: base })
43    }
44
45    /// Cache rooted at an arbitrary directory — for tests.
46    pub fn at(path: PathBuf) -> Self {
47        Self { dir: path }
48    }
49
50    /// Ensure the directory exists. Safe to call repeatedly.
51    pub fn ensure_dir(&self) -> Result<()> {
52        fs::create_dir_all(&self.dir).map_err(|e| AppError::io_at(&self.dir, e))
53    }
54
55    pub fn dir(&self) -> &Path {
56        &self.dir
57    }
58
59    pub fn payload_path(&self) -> PathBuf {
60        self.dir.join("usage.json")
61    }
62    pub fn stale_path(&self) -> PathBuf {
63        self.dir.join(".stale")
64    }
65    pub fn last_error_path(&self) -> PathBuf {
66        self.dir.join(".last_error")
67    }
68    pub fn lock_path(&self) -> PathBuf {
69        self.dir.join(".fetch.lock")
70    }
71
72    /// Age of the payload (`None` if it doesn't exist). Used by the widget to
73    /// decide whether the 60s cache window applies.
74    pub fn payload_age(&self) -> Option<Duration> {
75        let meta = fs::metadata(self.payload_path()).ok()?;
76        let mtime = meta.modified().ok()?;
77        SystemTime::now().duration_since(mtime).ok()
78    }
79
80    /// Returns the cached payload only if it is younger than `ttl`. Used as
81    /// the fast path in `_fetch_usage` (claudebar:343-349).
82    pub fn fresh_payload(&self, ttl: Duration) -> Result<Option<Vec<u8>>> {
83        let Some(age) = self.payload_age() else {
84            return Ok(None);
85        };
86        if age < ttl {
87            self.read_payload().map(Some)
88        } else {
89            Ok(None)
90        }
91    }
92
93    /// Read the payload regardless of age. `Err` if the file exists but is
94    /// unreadable; `Ok(None)` if it just doesn't exist.
95    pub fn maybe_payload(&self) -> Result<Option<Vec<u8>>> {
96        if !self.payload_path().exists() {
97            return Ok(None);
98        }
99        self.read_payload().map(Some)
100    }
101
102    fn read_payload(&self) -> Result<Vec<u8>> {
103        let p = self.payload_path();
104        let mut f = File::open(&p).map_err(|e| AppError::io_at(&p, e))?;
105        let mut buf = Vec::new();
106        f.read_to_end(&mut buf)
107            .map_err(|e| AppError::io_at(&p, e))?;
108        Ok(buf)
109    }
110
111    /// Atomically write a new payload. Uses `tempfile + persist` (POSIX
112    /// rename), matching claudebar's `mktemp + mv` invariant.
113    pub fn write_payload(&self, bytes: &[u8]) -> Result<()> {
114        self.ensure_dir()?;
115        let mut tmp = tempfile::Builder::new()
116            .prefix(".usage.")
117            .tempfile_in(&self.dir)
118            .map_err(|e| AppError::io_at(&self.dir, e))?;
119        tmp.write_all(bytes)
120            .map_err(|e| AppError::io_at(tmp.path(), e))?;
121        tmp.as_file_mut()
122            .sync_all()
123            .map_err(|e| AppError::io_at(tmp.path(), e))?;
124        tmp.persist(self.payload_path())
125            .map_err(|e| AppError::io_at(self.payload_path(), e.error))?;
126        // A successful write clears any stale marker.
127        let _ = fs::remove_file(self.stale_path());
128        let _ = fs::remove_file(self.last_error_path());
129        Ok(())
130    }
131
132    /// Mark the cache as stale. Idempotent.
133    pub fn mark_stale(&self) {
134        let _ = self.ensure_dir();
135        let _ = File::create(self.stale_path());
136    }
137
138    pub fn is_stale(&self) -> bool {
139        self.stale_path().exists()
140    }
141
142    /// Write the `.last_error` marker — first line `code`, second line `msg`.
143    /// Best-effort, never errors (matches claudebar:478-486 which silently
144    /// continues if the cache dir isn't writable).
145    pub fn write_last_error(&self, code: u16, msg: &str) {
146        let _ = self.ensure_dir();
147        let path = self.last_error_path();
148        let body = format!("{code}\n{msg}");
149        let _ = atomic_write(&path, body.as_bytes());
150    }
151
152    pub fn read_last_error(&self) -> Option<(u16, String)> {
153        let raw = fs::read_to_string(self.last_error_path()).ok()?;
154        let mut lines = raw.lines();
155        let code = lines.next()?.parse::<u16>().ok()?;
156        let msg = lines.next().unwrap_or_default().to_string();
157        Some((code, msg))
158    }
159}
160
161/// Acquire an exclusive flock on `path`, blocking up to `timeout`.
162/// Returned guard releases the lock on drop.
163///
164/// The flock file is created if missing, but its content is unused — only
165/// the lock matters.
166pub fn acquire_lock(path: &Path, timeout: Duration) -> Result<LockGuard> {
167    if let Some(parent) = path.parent() {
168        fs::create_dir_all(parent).map_err(|e| AppError::io_at(parent, e))?;
169    }
170    let f = OpenOptions::new()
171        .create(true)
172        .read(true)
173        .write(true)
174        .truncate(false)
175        .open(path)
176        .map_err(|e| AppError::io_at(path, e))?;
177
178    let deadline = std::time::Instant::now() + timeout;
179    loop {
180        match f.try_lock_exclusive() {
181            Ok(()) => return Ok(LockGuard { file: f }),
182            Err(_) => {
183                if std::time::Instant::now() >= deadline {
184                    return Err(AppError::Other(format!(
185                        "cache lock timeout after {:?}",
186                        timeout
187                    )));
188                }
189                std::thread::sleep(Duration::from_millis(50));
190            }
191        }
192    }
193}
194
195/// Releases the flock on drop. Holding this across an `.await` is fine as
196/// long as you don't move it across tasks (we always use it in `tokio::main`
197/// on a single thread).
198pub struct LockGuard {
199    file: File,
200}
201
202impl Drop for LockGuard {
203    fn drop(&mut self) {
204        let _ = FileExt::unlock(&self.file);
205    }
206}
207
208/// Atomic write helper used by `write_last_error`. Public for vendors that
209/// need to write small sidecar files (credentials, etc.).
210pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
211    let dir = path.parent().ok_or_else(|| {
212        AppError::Other(format!(
213            "atomic_write: path has no parent: {}",
214            path.display()
215        ))
216    })?;
217    fs::create_dir_all(dir).map_err(|e| AppError::io_at(dir, e))?;
218    let mut tmp = tempfile::Builder::new()
219        .prefix(".tmp.")
220        .tempfile_in(dir)
221        .map_err(|e| AppError::io_at(dir, e))?;
222    tmp.write_all(bytes)
223        .map_err(|e| AppError::io_at(tmp.path(), e))?;
224    tmp.as_file_mut()
225        .sync_all()
226        .map_err(|e| AppError::io_at(tmp.path(), e))?;
227    tmp.persist(path)
228        .map_err(|e| AppError::io_at(path, e.error))?;
229    Ok(())
230}
231
232fn xdg_cache_dir() -> Result<PathBuf> {
233    directories::BaseDirs::new()
234        .map(|b| b.cache_dir().to_path_buf())
235        .ok_or_else(|| AppError::Other("could not resolve XDG cache dir (no HOME?)".into()))
236}
237
238/// The user's home directory, resolved cross-platform via `directories`
239/// (`$HOME` on Unix/macOS, `%USERPROFILE%` / the Known Folder on Windows).
240///
241/// The OAuth-credential vendors (`anthropic`, `openai`) read their CLI-managed
242/// files from fixed dotfiles under `$HOME`; they share this resolver the same
243/// way they already share [`atomic_write`], so home resolution lives in one
244/// place rather than being reimplemented per vendor.
245pub fn home_dir() -> Result<PathBuf> {
246    directories::BaseDirs::new()
247        .map(|b| b.home_dir().to_path_buf())
248        .ok_or_else(|| AppError::Other("could not resolve home directory (no HOME?)".into()))
249}
250
251#[cfg(test)]
252mod tests {
253    use super::*;
254    use tempfile::TempDir;
255
256    fn fixture() -> (TempDir, Cache) {
257        let td = TempDir::new().unwrap();
258        let cache = Cache::at(td.path().join("anthropic"));
259        cache.ensure_dir().unwrap();
260        (td, cache)
261    }
262
263    #[test]
264    fn ensure_dir_is_idempotent() {
265        let (_td, cache) = fixture();
266        cache.ensure_dir().unwrap();
267        cache.ensure_dir().unwrap();
268        assert!(cache.dir().is_dir());
269    }
270
271    #[test]
272    fn write_then_read_round_trip() {
273        let (_td, cache) = fixture();
274        cache.write_payload(b"hello world").unwrap();
275        let got = cache.maybe_payload().unwrap();
276        assert_eq!(got.as_deref(), Some(&b"hello world"[..]));
277    }
278
279    #[test]
280    fn maybe_payload_returns_none_when_missing() {
281        let (_td, cache) = fixture();
282        assert!(cache.maybe_payload().unwrap().is_none());
283    }
284
285    #[test]
286    fn fresh_payload_respects_ttl() {
287        let (_td, cache) = fixture();
288        cache.write_payload(b"x").unwrap();
289        // Fresh = within a generous TTL.
290        assert!(
291            cache
292                .fresh_payload(Duration::from_secs(10))
293                .unwrap()
294                .is_some()
295        );
296        // Force "stale" by passing a zero TTL — payload is older than 0s.
297        assert!(
298            cache
299                .fresh_payload(Duration::from_secs(0))
300                .unwrap()
301                .is_none()
302        );
303    }
304
305    #[test]
306    fn write_clears_stale_marker_and_last_error() {
307        let (_td, cache) = fixture();
308        cache.mark_stale();
309        cache.write_last_error(429, "rate limited");
310        assert!(cache.is_stale());
311        assert!(cache.read_last_error().is_some());
312
313        cache.write_payload(b"fresh").unwrap();
314        assert!(!cache.is_stale());
315        assert!(cache.read_last_error().is_none());
316    }
317
318    #[test]
319    fn last_error_round_trip() {
320        let (_td, cache) = fixture();
321        cache.write_last_error(503, "service unavailable");
322        let (code, msg) = cache.read_last_error().unwrap();
323        assert_eq!(code, 503);
324        assert_eq!(msg, "service unavailable");
325    }
326
327    #[test]
328    fn last_error_with_empty_message_round_trips() {
329        let (_td, cache) = fixture();
330        cache.write_last_error(429, "");
331        let (code, msg) = cache.read_last_error().unwrap();
332        assert_eq!(code, 429);
333        assert_eq!(msg, "");
334    }
335
336    #[test]
337    fn lock_serializes_concurrent_acquirers() {
338        // First lock succeeds; while held, a second non-blocking attempt
339        // should time out quickly.
340        let (_td, cache) = fixture();
341        let lock_path = cache.lock_path();
342        let _guard = acquire_lock(&lock_path, Duration::from_millis(500)).unwrap();
343
344        let res = acquire_lock(&lock_path, Duration::from_millis(100));
345        assert!(matches!(res, Err(AppError::Other(_))));
346    }
347
348    #[test]
349    fn atomic_write_creates_parent_dirs() {
350        let td = TempDir::new().unwrap();
351        let nested = td.path().join("a/b/c/file.txt");
352        atomic_write(&nested, b"abc").unwrap();
353        assert_eq!(fs::read(&nested).unwrap(), b"abc");
354    }
355}