Batch filesystem metadata checks for instruction and skill discovery (#63266)

## Why

Discovering project instructions and repository skill directories can
require several metadata checks per ancestor directory. Batching those
checks reduces remote executor round trips and sandbox helper launches.

## What changed

Add bounded metadata batches that return a result or error for each
path in input order. Remote executors advertise support for
`fs/getMetadataBatch`, and sandboxed filesystems can process a batch in
one helper invocation.

Check project root markers alongside the first working-directory
instruction candidate, then batch the next candidate across unresolved
directories in filename priority order. Batch checks for
`.agents/skills` directories between the project root and the working
directory. Fall back to individual checks when batching is unsupported.

## Testing

Extend tests to assert batch counts for shallow and nested `AGENTS.md`
discovery and verify that metadata batches preserve result order and
per-path errors.

GitOrigin-RevId: 6cb04202a905d783454908348d008def95b9a5d6
This commit is contained in:
dozaki-openai
2026-11-23 05:39:53 +00:00
committed by copyberry
parent 794584993e
commit 5f645d732c
17 changed files with 537 additions and 45 deletions
+41 -33
View File
@@ -10,6 +10,7 @@ use codex_config::merge_toml_values;
use codex_config::project_root_markers_from_config;
use codex_exec_server::EnvironmentAccess;
use codex_exec_server::EnvironmentAccessKey;
use codex_exec_server::FileMetadata;
use codex_exec_server::FileSystemEnvironmentAccessor;
use codex_exec_server::GetMetadataOptions;
use codex_exec_server::LOCAL_FS;
@@ -239,20 +240,18 @@ async fn repo_agents_skill_roots<'a>(
cwd,
)
.await;
let directories = dirs_between_project_root_and_cwd(cwd, &project_root);
let mut candidates = Vec::new();
for directory in cwd.ancestors() {
candidates.push(directory.join(AGENTS_DIR_NAME).join(SKILLS_DIR_NAME));
if directory == project_root {
break;
}
}
candidates.reverse();
let results = skill_root_metadata(repository_fs_accessor, &candidates).await;
let mut roots = Vec::new();
let mut missing = Vec::new();
let mut results = futures::stream::iter(directories)
.map(|directory| async move {
let agents_skills = directory.join(AGENTS_DIR_NAME).join(SKILLS_DIR_NAME);
let agents_skills_uri = PathUri::from_abs_path(&agents_skills);
let result = repository_fs_accessor
.get_metadata(&agents_skills_uri, GetMetadataOptions::default())
.await;
(agents_skills, result)
})
.buffered(MAX_CONCURRENT_ANCESTOR_PROBES);
while let Some((agents_skills, result)) = results.next().await {
for (agents_skills, result) in candidates.into_iter().zip(results) {
match result {
Ok(metadata) if metadata.is_directory => roots.push(HostSkillRoot::host(
agents_skills,
@@ -332,6 +331,36 @@ async fn repo_agents_skill_roots<'a>(
roots
}
async fn skill_root_metadata(
fs: &dyn EnvironmentAccess,
paths: &[AbsolutePathBuf],
) -> Vec<io::Result<FileMetadata>> {
let options = GetMetadataOptions::default();
let mut results = Vec::with_capacity(paths.len());
// Keep the existing probe concurrency limit inside each helper. If a batch fails,
// retry the remaining paths individually.
for chunk in paths.chunks(MAX_CONCURRENT_ANCESTOR_PROBES) {
if chunk.len() == 1 {
break;
}
let uris = chunk.iter().map(PathUri::from_abs_path).collect::<Vec<_>>();
match fs.get_metadata_batch(&uris, options).await {
Ok(batch) if batch.len() == chunk.len() => results.extend(batch),
_ => break,
}
}
let mut remaining = futures::stream::iter(results.len()..paths.len())
.map(|index| async move {
fs.get_metadata(&PathUri::from_abs_path(&paths[index]), options)
.await
})
.buffered(MAX_CONCURRENT_ANCESTOR_PROBES);
while let Some(metadata) = remaining.next().await {
results.push(metadata);
}
results
}
fn project_root_markers_from_stack(config_layer_stack: &ConfigLayerStack) -> Vec<String> {
let mut merged = TomlValue::Table(toml::map::Map::new());
for layer in config_layer_stack.layers_low_to_high() {
@@ -419,27 +448,6 @@ async fn find_project_root(
}
}
fn dirs_between_project_root_and_cwd(
cwd: &AbsolutePathBuf,
project_root: &AbsolutePathBuf,
) -> Vec<AbsolutePathBuf> {
let mut directories = cwd
.ancestors()
.scan(false, |done, directory| {
if *done {
None
} else {
if &directory == project_root {
*done = true;
}
Some(directory)
}
})
.collect::<Vec<_>>();
directories.reverse();
directories
}
fn dedupe_skill_roots_by_path(roots: &mut Vec<HostSkillRoot>) {
let mut seen = HashSet::new();
roots.retain(|root| seen.insert(root.path.clone()));