Expand description
Disk-backed OAuth usage cache + lock file.
Two storage layers per docs/specs/data-fetching.md §OAuth usage
cache stack:
CacheStore—usage.jsonholds either the last endpoint response (CachedData) or a tag-only error record (CachedError), stamped withschema_version+cached_at. The orchestrator comparescached_atagainstusage.cache_durationto decide freshness.LockStore—usage.lockholds ablocked_untilUnix timestamp that prevents concurrent linesmith processes from stampeding the endpoint under 429 backoff.
Both writes go through atomic_write_json: write to a sibling
tempfile, then rename over the target. tempfile::NamedTempFile:: persist is rename-on-Unix and MoveFileEx with
MOVEFILE_REPLACE_EXISTING on Windows — atomic at the filesystem
level on both platforms.
Structs§
- Cache
Store - Reader/writer for
usage.json. - Cached
Data - Disk-serializable mirror of
UsageApiResponse. The wire shape uses#[serde(flatten)]forunknown_bucketsso codenamed keys appear at the top level of the endpoint response. The cache nests them under a namedunknown_bucketskey so the outerCachedUsagewrapper’s fields (schema_version,cached_at, etc.) don’t collide with endpoint-emitted keys likefive_hour. - Cached
Error - On-disk error record. Intentionally lossy — carries only the
UsageError::codetag, not the full Rust enum. Live errors from the current process take precedence over cached ones; the cache is just for cross-invocation hints. - Cached
Usage - Single cache-file entry. Writers produced via
Self::with_dataorSelf::with_errorkeep exactly one ofdata/errornull perdocs/specs/data-fetching.md§OAuth usage cache stack. Readers tolerate any combination (both null, both populated) and treat anomalies as misses at the orchestrator layer rather than erroring at parse time. - Lock
- On-disk lock file shape.
blocked_untilis a signed Unix timestamp in seconds —i64wide enough for any plausible Unix time, signed so themtime + LEGACY_LOCK_TTL_SECSarithmetic used by the legacy path can’t underflow. - Lock
Store - Reader/writer for
usage.lock.
Enums§
- Cache
Error - Cache-layer I/O / parse failures. Distinct from [
UsageError] because cache-miss variants collapse toOk(None)inCacheStore::read;CacheErrorcarries only the cases that indicate a real problem (filesystem error on write, inability to create the parent dir).
Constants§
- CACHE_
SCHEMA_ VERSION - Cache schema version. Bump when the on-disk shape changes in a
way that can’t be read by an older linesmith — readers with a
mismatched version treat the file as a miss per
docs/specs/data-fetching.md§Schema versioning.
Functions§
- atomic_
write_ json - Write a JSON-serializable value to
pathatomically: serialize into a sibling tempfile, thenpersist(rename on Unix,MoveFileExon Windows). The parent directory is created on demand; a concurrent writer will always see either the old file or the new one, never a torn write. - default_
root - Locate the linesmith cache root. Returns
Nonein environments that provide neither$XDG_CACHE_HOMEnor$HOME. Delegates toxdg::resolve_subdir; thefrom_process_envfactory usesvar_osso non-UTF-8 paths (Unix byte-string paths) survive through to the cache reader.