From da75f59f661acc4f996fa3b2d473d7a694828a3e Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 6 Oct 2026 22:00:35 +0000 Subject: [PATCH] Refresh the model catalog on the blocking pool (#12727) Co-authored-by: Claude Opus 5.5 --- crates/goose-provider-types/src/canonical.rs | 4 ++- .../src/canonical/registry.rs | 32 +++++++++++++------ crates/goose/src/model_catalog.rs | 14 ++++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/goose-provider-types/src/canonical.rs b/crates/goose-provider-types/src/canonical.rs index a2c69e539..6070af231 100644 --- a/crates/goose-provider-types/src/canonical.rs +++ b/crates/goose-provider-types/src/canonical.rs @@ -8,7 +8,9 @@ pub use model::{CanonicalModel, Limit, Modalities, Modality, Pricing, ThinkingMo pub use name_builder::{ canonical_name, map_provider_name, map_to_canonical_model, strip_version_suffix, }; -pub use registry::{load_cached_catalog, refresh_remote_catalog, CanonicalModelRegistry}; +pub use registry::{ + fetch_remote_catalog, load_cached_catalog, CanonicalModelRegistry, RemoteCatalog, +}; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct ModelMapping { diff --git a/crates/goose-provider-types/src/canonical/registry.rs b/crates/goose-provider-types/src/canonical/registry.rs index f9f8eed08..576b4354b 100644 --- a/crates/goose-provider-types/src/canonical/registry.rs +++ b/crates/goose-provider-types/src/canonical/registry.rs @@ -140,7 +140,13 @@ pub fn load_cached_catalog(cache_dir: &Path) -> Result { Ok(true) } -pub async fn refresh_remote_catalog(url: &str, cache_dir: &Path) -> Result { +pub struct RemoteCatalog { + body: Vec, + etag: Option, +} + +/// Returns `None` when the cached catalog is still current. +pub async fn fetch_remote_catalog(url: &str, cache_dir: &Path) -> Result> { let client = reqwest::Client::builder().build()?; let etag_path = cache_dir.join(ETAG_FILENAME); let mut request = client.get(url).timeout(Duration::from_secs(15)).header( @@ -153,7 +159,7 @@ pub async fn refresh_remote_catalog(url: &str, cache_dir: &Path) -> Result let response = request.send().await?.error_for_status()?; if response.status() == reqwest::StatusCode::NOT_MODIFIED { - return Ok(false); + return Ok(None); } if response .content_length() @@ -175,16 +181,22 @@ pub async fn refresh_remote_catalog(url: &str, cache_dir: &Path) -> Result } body.extend_from_slice(&chunk); } - let content = std::str::from_utf8(&body).context("canonical model catalog is not UTF-8")?; - let registry = super::models_dev::from_models_dev(content)?; + Ok(Some(RemoteCatalog { body, etag })) +} - std::fs::create_dir_all(cache_dir)?; - atomic_write(cache_dir.join(CATALOG_FILENAME), &body)?; - if let Some(etag) = etag { - atomic_write(etag_path, etag.as_bytes())?; +impl RemoteCatalog { + pub fn install(self, cache_dir: &Path) -> Result<()> { + let content = + std::str::from_utf8(&self.body).context("canonical model catalog is not UTF-8")?; + let registry = super::models_dev::from_models_dev(content)?; + + std::fs::create_dir_all(cache_dir)?; + atomic_write(cache_dir.join(CATALOG_FILENAME), &self.body)?; + if let Some(etag) = self.etag { + atomic_write(cache_dir.join(ETAG_FILENAME), etag.as_bytes())?; + } + activate(registry) } - activate(registry)?; - Ok(true) } fn atomic_write(destination: PathBuf, content: &[u8]) -> Result<()> { diff --git a/crates/goose/src/model_catalog.rs b/crates/goose/src/model_catalog.rs index 75f682b1a..728091d3b 100644 --- a/crates/goose/src/model_catalog.rs +++ b/crates/goose/src/model_catalog.rs @@ -1,5 +1,5 @@ use crate::config::paths::Paths; -use goose_providers::canonical::{load_cached_catalog, refresh_remote_catalog}; +use goose_providers::canonical::{fetch_remote_catalog, load_cached_catalog}; use std::path::PathBuf; const CATALOG_URL: &str = "https://models.dev/api.json"; @@ -15,8 +15,18 @@ pub fn initialize() { } tokio::spawn(async move { - if let Err(error) = refresh_remote_catalog(CATALOG_URL, &cache_dir).await { + if let Err(error) = refresh(cache_dir).await { tracing::warn!(%error, "failed to refresh remote model catalog"); } }); } + +async fn refresh(cache_dir: PathBuf) -> anyhow::Result<()> { + let Some(catalog) = fetch_remote_catalog(CATALOG_URL, &cache_dir).await? else { + return Ok(()); + }; + // Parsing the catalog is slow enough to stall the runtime's timers. Only + // this step goes to the blocking pool, so shutting the runtime down still + // cancels the download instead of waiting for it. + tokio::task::spawn_blocking(move || catalog.install(&cache_dir)).await? +}