Skip to content

desktop/src-tauri/src/price_refresh.rs

Comment 1, line 1

desktop/src-tauri/src/price_refresh.rs:1

//! F2 models.dev refresh: fetch, raw-response caching, failure fallback and repeatable imports.

Comment 2, line 2

desktop/src-tauri/src/price_refresh.rs:2

//!

Comment 3, line 3

desktop/src-tauri/src/price_refresh.rs:3

//! Online refresh rules in docs/design/desktop-usage/pricing.md:

Comment 4, line 4

desktop/src-tauri/src/price_refresh.rs:4

//! - off by default; enabling it sends only an HTTPS GET for models.dev api.json,

Comment 5, line 5

desktop/src-tauri/src/price_refresh.rs:5

//! without local usage, host/source identity, session content or account credentials;

Comment 6, line 6

desktop/src-tauri/src/price_refresh.rs:6

//! - raw responses live in <database-directory>/price-cache/, default TTL three days, range 1-365;

Comment 7, line 7

desktop/src-tauri/src/price_refresh.rs:7

//! fresh caches avoid network requests;

Comment 8, line 8

desktop/src-tauri/src/price_refresh.rs:8

//! - download/validation failure uses the last successful cache; no cache means an error with snapshots retained (A9);

Comment 9, line 9

desktop/src-tauri/src/price_refresh.rs:9

//! - invalid responses never replace the cache; repeated import does not duplicate or reprice existing estimates.

Comment 10, line 18

desktop/src-tauri/src/price_refresh.rs:18

/// Default cache TTL in days and its valid range.

Comment 11, line 23

desktop/src-tauri/src/price_refresh.rs:23

/// Sidecar cache metadata remains on disk independently of database rebuilding.

Comment 12, line 28

desktop/src-tauri/src/price_refresh.rs:28

/// Hexadecimal FNV-1a content hash, included in snapshot identity.

Comment 13, line 33

desktop/src-tauri/src/price_refresh.rs:33

/// Cache state for displaying freshness.

Comment 14, line 42

desktop/src-tauri/src/price_refresh.rs:42

/// Refresh result for UI state and operation logs.

Comment 15, line 45

desktop/src-tauri/src/price_refresh.rs:45

/// Status: fetched, cache_fresh, fetch_failed_used_cache or fetch_failed_no_cache.

Comment 16, line 54

desktop/src-tauri/src/price_refresh.rs:54

/// Observable refresh state held by AppState for UI polling.

Comment 17, line 72

desktop/src-tauri/src/price_refresh.rs:72

/// Read cache; missing/invalid metadata falls back to file mtime so download failure can still use it.

Comment 18, line 101

desktop/src-tauri/src/price_refresh.rs:101

/// Write validated cache through temporary files and rename.

Comment 19, line 117

desktop/src-tauri/src/price_refresh.rs:117

/// HTTPS GET models.dev api.json with ureq/rustls, a global timeout and bounded response size.

Comment 20, line 151

desktop/src-tauri/src/price_refresh.rs:151

/// Import content without duplicates; return (snapshot_id, inserted, already_present).

Comment 21, line 173

desktop/src-tauri/src/price_refresh.rs:173

/// Main refresh procedure with an injectable fetch function for tests.

Comment 22, line 174

desktop/src-tauri/src/price_refresh.rs:174

///

Comment 23, line 175

desktop/src-tauri/src/price_refresh.rs:175

/// force bypasses TTL for manual refresh. Failures retain existing snapshot/cache semantics:

Comment 24, line 176

desktop/src-tauri/src/price_refresh.rs:176

/// fetch/validation failure imports the last successful cached content; without a cache, return an error.

Comment 25, line 186

desktop/src-tauri/src/price_refresh.rs:186

// 1. Fresh cache without force: import cached content without a network request or duplicates.

Comment 26, line 210

desktop/src-tauri/src/price_refresh.rs:210

// 2. Fetch, validate, write cache and import.

Comment 27, line 215

desktop/src-tauri/src/price_refresh.rs:215

// Validate conversion before replacing cache files.

Comment 28, line 239

desktop/src-tauri/src/price_refresh.rs:239

// 3. A9 failure fallback uses the last successful downloaded cache.

Comment 29, line 269

desktop/src-tauri/src/price_refresh.rs:269

/// Cache metadata for status queries, or None when absent.

Comment 30, line 274

desktop/src-tauri/src/price_refresh.rs:274

/// Command/scheduler entry: background refresh using current settings, with operation logging.

Comment 31, line 306

desktop/src-tauri/src/price_refresh.rs:306

/// After collection, refresh only when enabled and stale/missing, unless explicitly forced.

Comment 32, line 307

desktop/src-tauri/src/price_refresh.rs:307

/// AppState.price_refresh.running prevents concurrent entry; report failure without blocking collection.

Comment 33, line 357

desktop/src-tauri/src/price_refresh.rs:357

/// Minimal synthetic api.json: one canonical provider model with pay-as-you-go rates.

Comment 34, line 360

desktop/src-tauri/src/price_refresh.rs:360

/// Live HTTPS smoke check: ureq/rustls fetch, response limit and actual catalog conversion.

Comment 35, line 361

desktop/src-tauri/src/price_refresh.rs:361

/// Requires network and is skipped in CI; run cargo test -p llm-usage-desktop http_fetch_live -- --ignored.

Comment 36, line 402

desktop/src-tauri/src/price_refresh.rs:402

// Synthetic time: 2026-09-21T14:13:20Z.

Comment 37, line 405

desktop/src-tauri/src/price_refresh.rs:405

/// A9 fetch failure falls back to the last successful cache for import.

Comment 38, line 410

desktop/src-tauri/src/price_refresh.rs:410

// Stale cache from 30 days earlier.

Comment 39, line 424

desktop/src-tauri/src/price_refresh.rs:424

/// A9 no-cache fetch failure returns an error while retaining existing snapshots.

Comment 40, line 436

desktop/src-tauri/src/price_refresh.rs:436

// Existing snapshots remain unchanged.

Comment 41, line 440

desktop/src-tauri/src/price_refresh.rs:440

/// A fresh cache avoids network requests within TTL.

Comment 42, line 445

desktop/src-tauri/src/price_refresh.rs:445

// One day old is within the default three-day TTL.

Comment 43, line 452

desktop/src-tauri/src/price_refresh.rs:452

// force bypasses TTL and invokes fetch.

Comment 44, line 463

desktop/src-tauri/src/price_refresh.rs:463

/// Invalid downloaded content leaves cache intact and falls back to its prior bytes (A9).

Comment 45, line 477

desktop/src-tauri/src/price_refresh.rs:477

// Cache bytes still match the previous successful download.

Comment 46, line 485

desktop/src-tauri/src/price_refresh.rs:485

/// Successful download writes/imports the snapshot; repeated identical content creates no duplicates.

Comment 47, line 498

desktop/src-tauri/src/price_refresh.rs:498

// A forced repeat produces the same snapshot ID and skips duplicate insertion.