Files
codex/codex-rs/core/src/session/cloud_plugins.rs
T
Tom c1eee51321 Trace capability discovery and cloud catalog refresh (#64917)
## What changed

Add tracing spans for per-step plugin selection, host skill discovery,
and V2 capability-root snapshots. The snapshot span records whether
skill discovery was requested.

Trace cloud catalog refresh and discovery, and time the step's wait
for the catalog lock separately from preparation under the lock.

GitOrigin-RevId: 12eeb6333a8d2b335e92112dc7dccae6e7ed266d
2026-12-01 04:09:45 +00:00

115 lines
4.1 KiB
Rust

//! Refreshes independent cloud catalogs concurrently, serialized with step preparation.
use super::Session;
use crate::McpRefreshReason;
use codex_extension_api::CloudCatalogRefreshInput;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use tokio::sync::Mutex;
use tokio::sync::MutexGuard;
use tracing::Instrument;
#[derive(Default)]
pub(super) struct CloudCatalogPreparation {
/// Serializes catalog refreshes and snapshot capture.
refresh_lock: Mutex<CatalogRefreshState>,
/// Requests resource invalidation on the next lock acquisition.
/// Survives cancellation while waiting.
refresh_pending: AtomicBool,
}
#[derive(Default)]
pub(crate) struct CatalogRefreshState {
// Leave the check for the next catalog lock holder if discovery is cancelled.
startup_retry_pending: bool,
}
#[tracing::instrument(
name = "cloud_catalog.refresh.acquire_lock",
skip_all,
fields(acquired)
)]
async fn acquire_refresh_lock(
lock: &Mutex<CatalogRefreshState>,
) -> MutexGuard<'_, CatalogRefreshState> {
let guard = lock.lock().await;
tracing::Span::current().record("acquired", true);
guard
}
impl Session {
fn finish_catalog_startup(&self, catalogs: &mut CatalogRefreshState) {
if std::mem::take(&mut catalogs.startup_retry_pending)
&& self.services.mcp_runtime.has_failed_startup_for_retry()
{
self.request_mcp_runtime_reprojection(McpRefreshReason::ServerStartupFailed);
}
}
pub(crate) async fn lock_cloud_catalogs(&self) -> MutexGuard<'_, CatalogRefreshState> {
let mut guard = self
.cloud_catalog_preparation
.refresh_lock
.lock()
.instrument(tracing::info_span!("cloud_catalog.step.acquire_lock"))
.await;
self.prepare_locked_catalogs(&mut guard);
guard
}
#[tracing::instrument(name = "cloud_catalog.prepare_locked", skip_all)]
fn prepare_locked_catalogs(&self, catalogs: &mut CatalogRefreshState) {
self.finish_catalog_startup(catalogs);
if self
.cloud_catalog_preparation
.refresh_pending
.swap(false, Ordering::AcqRel)
{
self.services.mcp_runtime.invalidate_resource_caches();
}
}
#[expect(
clippy::await_holding_invalid_type,
reason = "catalog publication must remain serialized with step preparation"
)]
#[tracing::instrument(name = "cloud_catalog.refresh", skip_all)]
pub(crate) async fn refresh_cloud_plugins(self: &Arc<Self>) -> anyhow::Result<()> {
self.cloud_catalog_preparation
.refresh_pending
.store(true, Ordering::Release);
let mut catalogs = acquire_refresh_lock(&self.cloud_catalog_preparation.refresh_lock).await;
self.prepare_locked_catalogs(&mut catalogs);
catalogs.startup_retry_pending = true;
self.refresh_mcp_if_dirty().await;
let results = futures::future::join_all(
self.services
.extensions
.turn_lifecycle_contributors()
.iter()
.map(|contributor| async move {
let outcome = contributor
.refresh_cloud_catalog(CloudCatalogRefreshInput {
session_store: &self.services.session_extension_data,
thread_store: &self.services.thread_extension_data,
})
.await;
// Queue the work before a slower sibling can time out the whole refresh.
if outcome.needs_mcp_refresh {
self.request_mcp_runtime_reprojection(
McpRefreshReason::ExtensionCatalogChanged,
);
}
outcome.result
}),
)
.await;
self.finish_catalog_startup(&mut catalogs);
drop(catalogs);
let errors: Vec<_> = results.into_iter().filter_map(Result::err).collect();
anyhow::ensure!(errors.is_empty(), "{}", errors.join("; "));
Ok(())
}
}