From 132972507ad833e1b1930cf46fb2c343c5c204fe Mon Sep 17 00:00:00 2001 From: Matthew Zeng Date: Tue, 16 Jun 2026 17:29:18 -0700 Subject: [PATCH] Reject stale plugin skill snapshots --- codex-rs/core-skills/src/root_loader.rs | 36 +++++++++++++------ codex-rs/core-skills/src/root_loader_tests.rs | 21 +++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/codex-rs/core-skills/src/root_loader.rs b/codex-rs/core-skills/src/root_loader.rs index 42a2a69087..06f423967f 100644 --- a/codex-rs/core-skills/src/root_loader.rs +++ b/codex-rs/core-skills/src/root_loader.rs @@ -60,9 +60,15 @@ impl SkillRootCacheKey { /// /// Non-plugin roots are always loaded directly because their filesystem lifecycle is owned by the /// environment or config layer that supplied them. +#[derive(Default)] +struct PluginRootCache { + generation: u64, + snapshots: HashMap, +} + #[derive(Default)] pub struct SkillRootLoader { - plugin_root_cache: RwLock>, + plugin_root_cache: RwLock, } impl SkillRootLoader { @@ -74,12 +80,15 @@ impl SkillRootLoader { let mut snapshots = Vec::new(); for root in roots { let cache_key = SkillRootCacheKey::from_root(&root); - let snapshot = match cache_key.as_ref().and_then(|key| self.cached_snapshot(key)) { + let (cache_generation, cached_snapshot) = cache_key + .as_ref() + .map_or((0, None), |key| self.cached_snapshot(key)); + let snapshot = match cached_snapshot { Some(snapshot) => snapshot, None => { let snapshot = load_skill_root(root).await; if let Some(cache_key) = cache_key { - self.cache_snapshot(cache_key, snapshot.clone()); + self.cache_snapshot(cache_generation, cache_key, snapshot.clone()); } snapshot } @@ -96,23 +105,30 @@ impl SkillRootLoader { .plugin_root_cache .write() .unwrap_or_else(std::sync::PoisonError::into_inner); - cache.clear(); + cache.generation = cache.generation.wrapping_add(1); + cache.snapshots.clear(); } - fn cached_snapshot(&self, key: &SkillRootCacheKey) -> Option { + fn cached_snapshot(&self, key: &SkillRootCacheKey) -> (u64, Option) { match self.plugin_root_cache.read() { - Ok(cache) => cache.get(key).cloned(), - Err(err) => err.into_inner().get(key).cloned(), + Ok(cache) => (cache.generation, cache.snapshots.get(key).cloned()), + Err(err) => { + let cache = err.into_inner(); + (cache.generation, cache.snapshots.get(key).cloned()) + } } } - fn cache_snapshot(&self, key: SkillRootCacheKey, snapshot: SkillRootSnapshot) { + fn cache_snapshot(&self, generation: u64, key: SkillRootCacheKey, snapshot: SkillRootSnapshot) { let mut cache = self .plugin_root_cache .write() .unwrap_or_else(std::sync::PoisonError::into_inner); - if cache.len() < MAX_CACHED_PLUGIN_SKILL_ROOTS || cache.contains_key(&key) { - cache.insert(key, snapshot); + if cache.generation == generation + && (cache.snapshots.len() < MAX_CACHED_PLUGIN_SKILL_ROOTS + || cache.snapshots.contains_key(&key)) + { + cache.snapshots.insert(key, snapshot); } } } diff --git a/codex-rs/core-skills/src/root_loader_tests.rs b/codex-rs/core-skills/src/root_loader_tests.rs index f2af87bd24..b5a5711aef 100644 --- a/codex-rs/core-skills/src/root_loader_tests.rs +++ b/codex-rs/core-skills/src/root_loader_tests.rs @@ -5,8 +5,10 @@ use codex_exec_server::LOCAL_FS; use codex_protocol::protocol::SkillScope; use codex_utils_absolute_path::test_support::PathExt; +use super::SkillRootCacheKey; use super::SkillRootLoader; use crate::loader::SkillRoot; +use crate::loader::load_skill_root; fn plugin_root(path: &std::path::Path) -> SkillRoot { SkillRoot { @@ -53,3 +55,22 @@ async fn reuses_plugin_root_snapshot_until_cache_is_cleared() { expected[0].description = "second".to_string(); assert_eq!(refreshed.skills, expected); } + +#[tokio::test] +async fn clear_cache_rejects_an_in_flight_snapshot() { + let tempdir = tempfile::tempdir().expect("tempdir"); + let loader = SkillRootLoader::default(); + write_skill(tempdir.path(), "first"); + + let root = plugin_root(tempdir.path()); + let cache_key = SkillRootCacheKey::from_root(&root).expect("plugin root cache key"); + let (generation, cached_snapshot) = loader.cached_snapshot(&cache_key); + assert!(cached_snapshot.is_none()); + + let snapshot = load_skill_root(root).await; + loader.clear_cache(); + loader.cache_snapshot(generation, cache_key.clone(), snapshot); + + let (_, cached_snapshot) = loader.cached_snapshot(&cache_key); + assert!(cached_snapshot.is_none()); +}