Simplify skill snapshot refresh API

This commit is contained in:
Xin Lin
2026-06-18 20:06:33 -07:00
parent 317578257f
commit eb7186aba6
7 changed files with 65 additions and 47 deletions
+5 -1
View File
@@ -2078,7 +2078,11 @@ async fn skills_service_reuses_skills_parsed_during_plugin_load() {
.with_plugin_skill_snapshots(plugin_skill_snapshots);
let skills_service = SkillsService::new(codex_home_abs, /*bundled_skills_enabled*/ false);
let cached = skills_service
.snapshot_for_config(&skills_input, /*fs*/ None)
.snapshot_for_config(
&skills_input,
/*fs*/ None,
/*refresh_filesystem*/ false,
)
.await;
assert_eq!(
+5 -33
View File
@@ -120,46 +120,24 @@ impl SkillsService {
///
/// This path uses a cache keyed by the effective skill-relevant config state rather than just
/// cwd so role-local and session-local skill overrides cannot bleed across sessions that happen
/// to share a directory.
pub async fn snapshot_for_config(
&self,
input: &SkillsLoadInput,
fs: Option<Arc<dyn ExecutorFileSystem>>,
) -> HostSkillsSnapshot {
self.snapshot_for_config_with_lookup(input, fs, ConfigSnapshotLookup::ReuseCached)
.await
}
/// Revalidates filesystem-derived roots and skill configuration rules before returning the
/// cached snapshot. If they are unchanged, the existing snapshot is reused without rescanning
/// skill contents.
pub async fn refresh_snapshot_for_config(
&self,
input: &SkillsLoadInput,
fs: Option<Arc<dyn ExecutorFileSystem>>,
) -> HostSkillsSnapshot {
self.snapshot_for_config_with_lookup(input, fs, ConfigSnapshotLookup::RefreshFilesystem)
.await
}
/// to share a directory. When `refresh_filesystem` is true, filesystem-derived roots and skill
/// configuration rules are revalidated before reusing the cached snapshot.
#[instrument(
name = "skills_for_config",
level = "info",
skip_all,
fields(otel.name = "skills_for_config")
)]
async fn snapshot_for_config_with_lookup(
pub async fn snapshot_for_config(
&self,
input: &SkillsLoadInput,
fs: Option<Arc<dyn ExecutorFileSystem>>,
lookup: ConfigSnapshotLookup,
refresh_filesystem: bool,
) -> HostSkillsSnapshot {
let extra_roots = self.extra_roots();
let cache_key = config_skills_cache_key(input, &extra_roots, fs.as_ref());
let cached = self.cached_snapshot_for_config(&cache_key);
if lookup == ConfigSnapshotLookup::ReuseCached
&& let Some(cached) = &cached
{
if !refresh_filesystem && let Some(cached) = &cached {
return cached.snapshot.clone();
}
@@ -323,12 +301,6 @@ impl SkillsService {
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum ConfigSnapshotLookup {
ReuseCached,
RefreshFilesystem,
}
#[derive(Clone)]
struct CachedConfigSkillsSnapshot {
fingerprint: ConfigSkillsFilesystemFingerprint,
+42 -9
View File
@@ -320,7 +320,11 @@ async fn skills_for_config_with_stack(
bundled_skills_enabled_from_stack(config_layer_stack),
);
skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&LOCAL_FS)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&LOCAL_FS)),
/*refresh_filesystem*/ false,
)
.await
.outcome()
.clone()
@@ -393,7 +397,11 @@ async fn skills_for_config_cache_hit_avoids_environment_filesystem_operations()
let file_system: Arc<dyn ExecutorFileSystem> = counting_file_system.clone();
skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&file_system)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&file_system)),
/*refresh_filesystem*/ false,
)
.await;
let operations_after_cache_miss = counting_file_system.operation_count();
let content_scans_after_cache_miss = counting_file_system.content_scan_operation_count();
@@ -401,7 +409,11 @@ async fn skills_for_config_cache_hit_avoids_environment_filesystem_operations()
assert!(content_scans_after_cache_miss > 0);
skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&file_system)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&file_system)),
/*refresh_filesystem*/ false,
)
.await;
assert_eq!(
counting_file_system.operation_count(),
@@ -413,7 +425,11 @@ async fn skills_for_config_cache_hit_avoids_environment_filesystem_operations()
);
skills_service
.refresh_snapshot_for_config(&skills_input, Some(Arc::clone(&file_system)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&file_system)),
/*refresh_filesystem*/ true,
)
.await;
let operations_after_refresh = counting_file_system.operation_count();
assert!(operations_after_refresh > operations_after_cache_miss);
@@ -432,6 +448,7 @@ async fn skills_for_config_cache_hit_avoids_environment_filesystem_operations()
.snapshot_for_config(
&unrelated_session_flag_input,
Some(Arc::clone(&file_system)),
/*refresh_filesystem*/ false,
)
.await;
assert_eq!(
@@ -441,14 +458,18 @@ async fn skills_for_config_cache_hit_avoids_environment_filesystem_operations()
skills_service.clear_cache();
skills_service
.snapshot_for_config(&skills_input, Some(file_system))
.snapshot_for_config(
&skills_input,
Some(file_system),
/*refresh_filesystem*/ false,
)
.await;
assert!(counting_file_system.operation_count() > operations_after_refresh);
assert!(counting_file_system.content_scan_operation_count() > content_scans_after_cache_miss);
}
#[tokio::test]
async fn refresh_snapshot_for_config_discovers_new_repo_skill_root() {
async fn snapshot_for_config_refresh_discovers_new_repo_skill_root() {
let codex_home = tempfile::tempdir().expect("tempdir");
let cwd = tempfile::tempdir().expect("tempdir");
fs::create_dir(cwd.path().join(".git")).expect("create project marker");
@@ -465,7 +486,11 @@ async fn refresh_snapshot_for_config_discovers_new_repo_skill_root() {
);
let initial_snapshot = skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&LOCAL_FS)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&LOCAL_FS)),
/*refresh_filesystem*/ false,
)
.await;
assert!(
initial_snapshot
@@ -483,7 +508,11 @@ async fn refresh_snapshot_for_config_discovers_new_repo_skill_root() {
);
let cached_snapshot = skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&LOCAL_FS)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&LOCAL_FS)),
/*refresh_filesystem*/ false,
)
.await;
assert!(
cached_snapshot
@@ -494,7 +523,11 @@ async fn refresh_snapshot_for_config_discovers_new_repo_skill_root() {
);
let refreshed_snapshot = skills_service
.refresh_snapshot_for_config(&skills_input, Some(Arc::clone(&LOCAL_FS)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&LOCAL_FS)),
/*refresh_filesystem*/ true,
)
.await;
assert!(
refreshed_snapshot
+1
View File
@@ -428,6 +428,7 @@ enabled = false
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&codex_exec_server::LOCAL_FS)),
/*refresh_filesystem*/ false,
)
.await;
let outcome = snapshot.outcome();
+1 -1
View File
@@ -449,7 +449,7 @@ async fn warm_plugins_and_skills_for_session_init(
let skills_input = skills_load_input_from_config(config.as_ref(), effective_skill_roots)
.with_plugin_skill_snapshots(plugin_skill_snapshots);
skills_service
.refresh_snapshot_for_config(&skills_input, fs)
.snapshot_for_config(&skills_input, fs, /*refresh_filesystem*/ true)
.await
.outcome()
.errors
+10 -2
View File
@@ -5113,7 +5113,11 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
let skill_fs = environment.get_filesystem();
let skills_snapshot = services
.skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&skill_fs)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&skill_fs)),
/*refresh_filesystem*/ false,
)
.await;
let turn_context = Session::make_turn_context(
thread_id,
@@ -7168,7 +7172,11 @@ where
let skill_fs = environment.get_filesystem();
let skills_snapshot = services
.skills_service
.snapshot_for_config(&skills_input, Some(Arc::clone(&skill_fs)))
.snapshot_for_config(
&skills_input,
Some(Arc::clone(&skill_fs)),
/*refresh_filesystem*/ false,
)
.await;
let turn_context = Arc::new(Session::make_turn_context(
thread_id,
+1 -1
View File
@@ -740,7 +740,7 @@ impl Session {
let skills_snapshot = self
.services
.skills_service
.snapshot_for_config(&skills_input, fs)
.snapshot_for_config(&skills_input, fs, /*refresh_filesystem*/ false)
.await;
let mut turn_context: TurnContext = Self::make_turn_context(
self.thread_id(),