Files
codex/codex-rs/models-manager/src/catalog.rs
T
evanz-oai 44ccf39457 Preserve model catalogs when starting child agents (#60049)
## Why

Child startup can lose remote model capabilities such as `tool_search` when the selected catalog is uninitialized or disk data is older than the parent's in-memory snapshot.

## What changed

- Initialize the final provider, auth, and originator catalog before resolving a child's startup model, then read it from memory.
- Preserve loaded catalogs, including empty responses and updates that failed to persist, without overwriting concurrent refreshes.
- Reuse catalogs across retry and WebSocket transport overrides while retaining routing and header distinctions.

## Testing

Add regression coverage for child tool search with warm, cold, and disabled caches; failed cache writes; concurrent refreshes; empty catalogs; and Guardian reviewer catalog reuse.

GitOrigin-RevId: 01d995a65b623cc7d6f26e2dc9b6e8cf380de782
2026-11-07 03:16:52 +00:00

82 lines
2.4 KiB
Rust

//! Tracks the source of model metadata without changing the catalog wire or cache formats.
use std::borrow::Cow;
use std::collections::HashSet;
use codex_protocol::openai_models::ModelInfo;
use crate::cache::ModelsCacheEntry;
pub use codex_protocol::openai_models::CatalogModel;
pub use codex_protocol::openai_models::ModelOrigin;
/// Catalog authority is independent of whether its model metadata is borrowed or owned.
pub(crate) enum SelectedCatalog<'a> {
CurrentSnapshot(Cow<'a, [ModelInfo]>),
Fallback(Cow<'a, [ModelInfo]>),
}
impl<'a> SelectedCatalog<'a> {
pub fn into_models(self) -> Cow<'a, [ModelInfo]> {
match self {
Self::CurrentSnapshot(models) | Self::Fallback(models) => models,
}
}
}
/// The effective catalog and the IDs in the unmerged provider response share one lock.
#[derive(Debug)]
pub(crate) struct CatalogState {
pub entry: ModelsCacheEntry,
pub provider_model_ids: HashSet<String>,
// Distinguishes a successfully loaded empty catalog from the initial bundled seed.
pub initialized: bool,
}
impl CatalogState {
pub fn bundled(entry: ModelsCacheEntry) -> Self {
Self {
entry,
provider_model_ids: HashSet::new(),
initialized: false,
}
}
pub fn provider(entry: ModelsCacheEntry) -> Self {
// Capture raw response IDs before merging, including when loading from disk cache.
let provider_model_ids = entry
.models
.iter()
.map(|model| model.slug.clone())
.collect();
Self {
entry,
provider_model_ids,
initialized: true,
}
}
pub fn model_origin(&self, selected: &SelectedCatalog<'_>, slug: &str) -> ModelOrigin {
if matches!(selected, SelectedCatalog::CurrentSnapshot(_))
&& self.provider_model_ids.contains(slug)
{
ModelOrigin::Provider
} else {
ModelOrigin::Bundled
}
}
pub fn model_catalog(&self, selected: SelectedCatalog<'_>) -> Vec<CatalogModel> {
let models = match &selected {
SelectedCatalog::CurrentSnapshot(models) | SelectedCatalog::Fallback(models) => models,
};
models
.iter()
.map(|model| CatalogModel {
origin: self.model_origin(&selected, &model.slug),
model: model.clone().into(),
})
.collect()
}
}