Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Centralize cloud skill cache state under the catalog lock (#55705)

Anthony Tafoyab48277327d42
diff --git a/codex-rs/ext/mcp/src/plugin_contributor_state.rs b/codex-rs/ext/mcp/src/plugin_contributor_state.rs
index fadf0a1d9f..f6badc308e 100644
--- a/codex-rs/ext/mcp/src/plugin_contributor_state.rs
+++ b/codex-rs/ext/mcp/src/plugin_contributor_state.rs
@@ -51,18 +51,7 @@ pub(crate) struct CloudPluginGeneration {
}
impl CloudPluginGeneration {
- pub(crate) fn new(mcp_resources: Option<McpResourceClient>) -> Self {
- let auth_cache_key = mcp_resources
- .as_ref()
- .map(|client| client.auth_cache_key_for_server(CODEX_APPS_MCP_SERVER_NAME));
- Self {
- auth_cache_key,
- mcp_resources,
- catalog: None,
- }
- }
-
- pub(crate) fn is_current(&self) -> bool {
+ pub(crate) fn is_auth_current(&self) -> bool {
self.auth_cache_key
== self
.mcp_resources
@@ -104,10 +93,11 @@ impl PluginContributorState {
pub(crate) fn cloud_catalog(&self) -> Option<&PluginCatalog> {
self.cloud_generation
.as_ref()
- .filter(|generation| generation.is_current())
+ .filter(|generation| generation.is_auth_current())
.and_then(|generation| generation.catalog.as_ref())
}
+ /// Establishes the auth scope and returns the key used by that generation.
pub(crate) fn prepare_cloud_generation(
&mut self,
mcp_resources: Option<McpResourceClient>,
@@ -120,7 +110,11 @@ impl PluginContributorState {
.as_ref()
.is_none_or(|generation| generation.auth_cache_key != auth_cache_key)
{
- self.cloud_generation = Some(CloudPluginGeneration::new(mcp_resources));
+ self.cloud_generation = Some(CloudPluginGeneration {
+ auth_cache_key: auth_cache_key.clone(),
+ mcp_resources,
+ catalog: None,
+ });
}
auth_cache_key
}
@@ -136,7 +130,7 @@ impl PluginContributorState {
.cloud_generation
.as_mut()
.filter(|generation| generation.auth_cache_key.as_ref() == auth_cache_key)
- .filter(|generation| generation.is_current())
+ .filter(|generation| generation.is_auth_current())
else {
return false;
};
diff --git a/codex-rs/ext/skills/src/catalog_state.rs b/codex-rs/ext/skills/src/catalog_state.rs
index e96db212bb..b5458fe562 100644
--- a/codex-rs/ext/skills/src/catalog_state.rs
+++ b/codex-rs/ext/skills/src/catalog_state.rs
@@ -3,7 +3,6 @@
use std::collections::HashMap;
use std::sync::Arc;
-use std::sync::Mutex;
use codex_exec_server::ExecutorCapabilityDiscoverySnapshot;
use codex_exec_server::ResolvedSelectedCapabilityRoot;
@@ -32,10 +31,39 @@ pub(crate) struct SkillCatalogState {
// DiscoveryV2.
pub(crate) executor_v2_cache: Option<CachedExecutorV2Catalog>,
// Shared by both discovery modes.
- pub(crate) cloud_cache: Option<Arc<CloudSkillGeneration>>,
+ pub(crate) cloud_generation: Option<Arc<CloudSkillGeneration>>,
+ pub(crate) cloud_resources: CloudResourceCache,
}
-/// Keeps cloud metadata and contents within one published auth scope and Apps availability state.
+impl SkillCatalogState {
+ /// Reuses the auth scope or replaces it, clearing contents from the previous scope.
+ pub(crate) fn prepare_cloud_generation(
+ &mut self,
+ mcp_resources: Option<&McpResourceClient>,
+ ) -> Arc<CloudSkillGeneration> {
+ let auth_cache_key = mcp_resources
+ .map(|client| client.auth_cache_key_for_server(codex_mcp::CODEX_APPS_MCP_SERVER_NAME));
+ if let Some(generation) = self
+ .cloud_generation
+ .as_ref()
+ .filter(|generation| generation.auth_cache_key == auth_cache_key)
+ {
+ return Arc::clone(generation);
+ }
+
+ let generation = Arc::new(CloudSkillGeneration {
+ auth_cache_key,
+ resource_cache_key: None,
+ mcp_resources: mcp_resources.cloned(),
+ catalog: None,
+ });
+ self.cloud_generation = Some(Arc::clone(&generation));
+ self.cloud_resources = CloudResourceCache::default();
+ generation
+ }
+}
+
+/// Keeps cloud metadata within one published auth scope and Apps availability state.
/// Warning-free discovery is reused until the Apps resource generation changes.
/// A published refresh replaces the Arc so late reads cannot populate its successor.
pub(crate) struct CloudSkillGeneration {
@@ -43,7 +71,6 @@ pub(crate) struct CloudSkillGeneration {
pub(crate) resource_cache_key: Option<McpResourceServerCacheKey>,
pub(crate) mcp_resources: Option<McpResourceClient>,
pub(crate) catalog: Option<SkillCatalog>,
- pub(crate) resources: Mutex<CloudResourceCache>,
}
impl CloudSkillGeneration {
@@ -53,7 +80,7 @@ impl CloudSkillGeneration {
.and_then(|client| client.server_cache_key(codex_mcp::CODEX_APPS_MCP_SERVER_NAME))
}
- pub(crate) fn is_current(&self) -> bool {
+ pub(crate) fn is_auth_current(&self) -> bool {
self.auth_cache_key
== self.mcp_resources.as_ref().map(|client| {
client.auth_cache_key_for_server(codex_mcp::CODEX_APPS_MCP_SERVER_NAME)
diff --git a/codex-rs/ext/skills/src/cloud_cache_tests.rs b/codex-rs/ext/skills/src/cloud_cache_tests.rs
index 4c87eeaf08..780c0392c9 100644
--- a/codex-rs/ext/skills/src/cloud_cache_tests.rs
+++ b/codex-rs/ext/skills/src/cloud_cache_tests.rs
@@ -33,7 +33,12 @@ impl SkillProvider for Provider {
self.entered.notify_one();
self.resume.notified().await;
}
- if let Some(catalog) = self.next_catalog.lock().unwrap().take() {
+ if let Some(catalog) = self
+ .next_catalog
+ .lock()
+ .expect("test provider catalog mutex should not be poisoned")
+ .take()
+ {
return Ok(catalog);
}
let is_partial = self.partial_list.swap(false, Ordering::SeqCst);
@@ -128,14 +133,22 @@ async fn frozen_cloud_catalog_and_reads_ignore_live_mcp_changes() {
.to_string()
.as_bytes(),
)
- .unwrap();
+ .expect("frozen cloud skill fixture should be valid");
let providers = SkillProviders::new().with_cloud_provider(Arc::new(provider));
state
.refresh_cloud_catalog(&providers, query(Some(client())))
.await
- .unwrap();
- let catalog = state.available_cloud_catalog().unwrap();
- let generation = state.cloud_cache(/*mcp_resources*/ None);
+ .expect("cloud catalog refresh should succeed");
+ let catalog = state
+ .available_cloud_catalog()
+ .expect("cloud catalog should be available after refresh");
+ let generation = state
+ .catalog_state
+ .lock()
+ .expect("catalog state mutex should not be poisoned")
+ .cloud_generation
+ .clone()
+ .expect("initial refresh should publish a cloud generation");
for mcp_resources in [Some(client()), None, Some(client())] {
let request = SkillReadRequest {
package: SkillPackageId("skill://demo/work".into()),
@@ -143,7 +156,10 @@ async fn frozen_cloud_catalog_and_reads_ignore_live_mcp_changes() {
..request(mcp_resources.clone())
};
assert_eq!(
- state.read_skill(&providers, request).await.unwrap(),
+ state
+ .read_skill(&providers, request)
+ .await
+ .expect("cloud skill read should succeed"),
SkillReadResult {
resource: SkillResourceId::new("skill://demo/work/SKILL.md"),
contents: "Frozen contents".into(),
@@ -152,11 +168,17 @@ async fn frozen_cloud_catalog_and_reads_ignore_live_mcp_changes() {
state
.refresh_cloud_catalog(&providers, query(mcp_resources))
.await
- .unwrap();
+ .expect("cloud catalog refresh should succeed");
assert_eq!(state.available_cloud_catalog(), Some(catalog.clone()));
assert!(Arc::ptr_eq(
&generation,
- &state.cloud_cache(/*mcp_resources*/ None)
+ state
+ .catalog_state
+ .lock()
+ .expect("catalog state mutex should not be poisoned")
+ .cloud_generation
+ .as_ref()
+ .expect("frozen cloud generation should remain available")
));
}
}
@@ -176,7 +198,7 @@ async fn late_read_cannot_return_or_populate_a_replaced_cache() {
state
.refresh_cloud_catalog(&providers, query(replacement.clone()))
.await
- .unwrap();
+ .expect("cloud catalog refresh should succeed");
provider.resume.notify_one();
});
assert!(late.is_err());
@@ -184,7 +206,7 @@ async fn late_read_cannot_return_or_populate_a_replaced_cache() {
state
.read_skill(&providers, request(replacement))
.await
- .unwrap()
+ .expect("cloud skill read should succeed")
.contents,
"contents 1"
);
@@ -201,7 +223,11 @@ async fn late_discovery_cannot_publish_into_a_new_generation() {
state.refresh_cloud_catalog(&providers, query(Some(client()))),
async {
provider.entered.notified().await;
- state.cloud_cache(Some(&client()));
+ state
+ .catalog_state
+ .lock()
+ .expect("catalog state mutex should not be poisoned")
+ .prepare_cloud_generation(Some(&client()));
provider.resume.notify_one();
}
);
@@ -210,10 +236,10 @@ async fn late_discovery_cannot_publish_into_a_new_generation() {
state
.catalog_state
.lock()
- .unwrap()
- .cloud_cache
+ .expect("catalog state mutex should not be poisoned")
+ .cloud_generation
.as_ref()
- .unwrap()
+ .expect("replacement cloud generation should exist")
.catalog
.is_none()
);
@@ -229,9 +255,11 @@ async fn partial_cloud_catalog_without_warnings_is_refreshed_next_turn() {
state
.refresh_cloud_catalog(&providers, query(client.clone()))
.await
- .unwrap();
+ .expect("cloud catalog refresh should succeed");
assert_eq!(
- state.available_cloud_catalog().unwrap(),
+ state
+ .available_cloud_catalog()
+ .expect("cloud catalog should be available after refresh"),
SkillCatalog {
is_partial: true,
..Default::default()
@@ -240,9 +268,11 @@ async fn partial_cloud_catalog_without_warnings_is_refreshed_next_turn() {
state
.refresh_cloud_catalog(&providers, query(client))
.await
- .unwrap();
+ .expect("cloud catalog refresh should succeed");
assert_eq!(
- state.available_cloud_catalog().unwrap(),
+ state
+ .available_cloud_catalog()
+ .expect("cloud catalog should be available after refresh"),
SkillCatalog {
is_partial: false,
warnings: vec!["account catalog".into()],
@@ -273,19 +303,33 @@ async fn cloud_refresh_preserves_complete_catalogs_only_within_the_same_auth_sco
let providers = SkillProviders::new().with_cloud_provider(provider.clone());
let original_client = client();
let mut previous = catalog("previous", previous_partial);
- *provider.next_catalog.lock().unwrap() = Some(previous.clone());
+ *provider
+ .next_catalog
+ .lock()
+ .expect("test provider catalog mutex should not be poisoned") =
+ Some(previous.clone());
state
.refresh_cloud_catalog(&providers, query(Some(original_client.clone())))
.await
- .unwrap();
- let generation = state.cloud_cache(Some(&original_client));
+ .expect("cloud catalog refresh should succeed");
+ let generation = state
+ .catalog_state
+ .lock()
+ .expect("catalog state mutex should not be poisoned")
+ .cloud_generation
+ .clone()
+ .expect("initial refresh should publish a cloud generation");
let contents = state
.read_skill(&providers, request(Some(original_client.clone())))
.await
- .unwrap();
+ .expect("cloud skill read should succeed");
let incoming = catalog("incoming", incoming_partial);
- *provider.next_catalog.lock().unwrap() = Some(incoming.clone());
+ *provider
+ .next_catalog
+ .lock()
+ .expect("test provider catalog mutex should not be poisoned") =
+ Some(incoming.clone());
let current_client = if changed_auth {
client()
} else {
@@ -294,10 +338,14 @@ async fn cloud_refresh_preserves_complete_catalogs_only_within_the_same_auth_sco
state
.refresh_cloud_catalog(&providers, query(Some(current_client.clone())))
.await
- .unwrap();
+ .expect("cloud catalog refresh should succeed");
let retained = !changed_auth && !previous_partial && incoming_partial;
assert_eq!(
- generation.catalog.as_ref().unwrap().is_partial,
+ generation
+ .catalog
+ .as_ref()
+ .expect("published generation should contain a catalog")
+ .is_partial,
previous_partial
);
if retained {
@@ -309,13 +357,19 @@ async fn cloud_refresh_preserves_complete_catalogs_only_within_the_same_auth_sco
);
assert!(!Arc::ptr_eq(
&generation,
- &state.cloud_cache(Some(&current_client))
+ state
+ .catalog_state
+ .lock()
+ .expect("catalog state mutex should not be poisoned")
+ .cloud_generation
+ .as_ref()
+ .expect("refresh should publish a cloud generation")
));
assert_eq!(
state
.read_skill(&providers, request(Some(current_client)))
.await
- .unwrap()
+ .expect("cloud skill read should succeed")
.contents,
if retained {
contents.contents
diff --git a/codex-rs/ext/skills/src/state.rs b/codex-rs/ext/skills/src/state.rs
index caee6292ca..896fb8f290 100644
--- a/codex-rs/ext/skills/src/state.rs
+++ b/codex-rs/ext/skills/src/state.rs
@@ -87,7 +87,8 @@ impl SkillsThreadState {
.catalog_state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- catalogs.cloud_cache = None;
+ catalogs.cloud_generation = None;
+ catalogs.cloud_resources = CloudResourceCache::default();
}
*previous = config;
}
@@ -247,38 +248,12 @@ impl SkillsThreadState {
self.catalog_state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
- .cloud_cache
+ .cloud_generation
.as_ref()
- .filter(|cache| cache.is_current())
+ .filter(|cache| cache.is_auth_current())
.and_then(|cache| cache.catalog.clone())
}
- fn cloud_cache(&self, mcp_resources: Option<&McpResourceClient>) -> Arc<CloudSkillGeneration> {
- let mut state = self
- .catalog_state
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let cache_key = mcp_resources
- .map(|client| client.auth_cache_key_for_server(codex_mcp::CODEX_APPS_MCP_SERVER_NAME));
- if let Some(cache) = state
- .cloud_cache
- .as_ref()
- .filter(|cache| cache.auth_cache_key == cache_key)
- {
- return Arc::clone(cache);
- }
-
- let next_cache = Arc::new(CloudSkillGeneration {
- auth_cache_key: cache_key,
- resource_cache_key: None,
- mcp_resources: mcp_resources.cloned(),
- catalog: None,
- resources: Mutex::new(CloudResourceCache::default()),
- });
- state.cloud_cache = Some(Arc::clone(&next_cache));
- next_cache
- }
-
/// The serialized turn-start lifecycle is the only discovery writer.
/// Reuse warning-free discovery until invalidated; retry failures and partial catalogs next turn.
#[tracing::instrument(name = "skills.cloud.refresh_cloud_catalog", level = "info", skip_all)]
@@ -287,32 +262,44 @@ impl SkillsThreadState {
providers: &SkillProviders,
query: SkillListQuery,
) -> SkillProviderResult<()> {
- if !self.cloud_skill_enabled() {
- return Ok(());
- }
- // Switch generations before the fallible lookup; only same-auth-scope failures
- // may retain previously authorized metadata and contents.
- let cache = self.cloud_cache(providers.cloud_mcp_resources(query.mcp_resources.as_deref()));
- let resource_cache_key = cache.current_resource_cache_key();
- if cache.is_current()
- && cache
- .catalog
- .as_ref()
- .is_some_and(|catalog| !catalog.is_partial && catalog.warnings.is_empty())
- && cache.resource_cache_key == resource_cache_key
- {
- return Ok(());
- }
+ let (cache, resource_cache_key) = {
+ let config = self
+ .config
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if !config.cloud_skill_enabled || !self.cloud_skills_available {
+ return Ok(());
+ }
+ let mut catalogs = self
+ .catalog_state
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ // Switch auth scopes before discovery so failures retain only authorized contents.
+ let cache = catalogs.prepare_cloud_generation(
+ providers.cloud_mcp_resources(query.mcp_resources.as_deref()),
+ );
+ let resource_cache_key = cache.current_resource_cache_key();
+ if cache.is_auth_current()
+ && cache
+ .catalog
+ .as_ref()
+ .is_some_and(|catalog| !catalog.is_partial && catalog.warnings.is_empty())
+ && cache.resource_cache_key == resource_cache_key
+ {
+ return Ok(());
+ }
+ (cache, resource_cache_key)
+ };
let mut catalog = providers.list_cloud_for_turn(query).await?;
catalog.entries.sort_by(|a, b| a.id.0.cmp(&b.id.0));
let mut catalogs = self
.catalog_state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- if !cache.is_current()
+ if !cache.is_auth_current()
|| cache.current_resource_cache_key() != resource_cache_key
|| !catalogs
- .cloud_cache
+ .cloud_generation
.as_ref()
.is_some_and(|current| Arc::ptr_eq(current, &cache))
{
@@ -320,7 +307,7 @@ impl SkillsThreadState {
"cloud skill cache changed during discovery",
));
}
- let resources = if catalog.is_partial
+ if catalog.is_partial
&& let Some(previous) = cache
.catalog
.as_ref()
@@ -328,20 +315,14 @@ impl SkillsThreadState {
{
catalog = previous.clone();
catalog.is_partial = true;
- cache
- .resources
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .clone()
} else {
- CloudResourceCache::default()
- };
- catalogs.cloud_cache = Some(Arc::new(CloudSkillGeneration {
+ catalogs.cloud_resources = CloudResourceCache::default();
+ }
+ catalogs.cloud_generation = Some(Arc::new(CloudSkillGeneration {
auth_cache_key: cache.auth_cache_key.clone(),
resource_cache_key,
mcp_resources: cache.mcp_resources.clone(),
catalog: Some(catalog),
- resources: Mutex::new(resources),
}));
Ok(())
}
@@ -360,9 +341,8 @@ impl SkillsThreadState {
"cloud skill reads require a cloud context",
));
};
- let cache = self.cloud_cache(providers.cloud_mcp_resources(mcp_resources.as_deref()));
let cache_key = SkillReadCacheKey::from(&request);
- {
+ let cache = {
let config = self
.config
.lock()
@@ -370,38 +350,31 @@ impl SkillsThreadState {
if !config.cloud_skill_enabled || !self.cloud_skills_available {
return Err(SkillProviderError::new("cloud skills are disabled"));
}
- let catalogs = self
+ let mut catalogs = self
.catalog_state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- if !cache.is_current()
- || !catalogs
- .cloud_cache
- .as_ref()
- .is_some_and(|current| Arc::ptr_eq(current, &cache))
- {
+ let cache = catalogs
+ .prepare_cloud_generation(providers.cloud_mcp_resources(mcp_resources.as_deref()));
+ if !cache.is_auth_current() {
return Err(SkillProviderError::new(
"cloud skill cache changed before read",
));
}
- if let Some(result) = cache
- .resources
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .get(&cache_key)
- {
+ if let Some(result) = catalogs.cloud_resources.get(&cache_key) {
return Ok(result);
}
- }
+ cache
+ };
let result = providers.read(request).await?;
- let catalogs = self
+ let mut catalogs = self
.catalog_state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
- if !cache.is_current()
+ if !cache.is_auth_current()
|| !catalogs
- .cloud_cache
+ .cloud_generation
.as_ref()
.is_some_and(|current| Arc::ptr_eq(current, &cache))
{
@@ -412,11 +385,7 @@ impl SkillsThreadState {
if result.resource != cache_key.resource {
return Ok(result);
}
- Ok(cache
- .resources
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .insert(cache_key, result))
+ Ok(catalogs.cloud_resources.insert(cache_key, result))
}
pub(crate) fn replace_shadow_selection_turn(