Reject stale plugin skill snapshots

This commit is contained in:
Matthew Zeng
2026-06-18 00:18:52 -07:00
parent b08fde8827
commit 132972507a
2 changed files with 47 additions and 10 deletions
+26 -10
View File
@@ -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<SkillRootCacheKey, SkillRootSnapshot>,
}
#[derive(Default)]
pub struct SkillRootLoader {
plugin_root_cache: RwLock<HashMap<SkillRootCacheKey, SkillRootSnapshot>>,
plugin_root_cache: RwLock<PluginRootCache>,
}
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<SkillRootSnapshot> {
fn cached_snapshot(&self, key: &SkillRootCacheKey) -> (u64, Option<SkillRootSnapshot>) {
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);
}
}
}
@@ -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());
}