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
@@ -82,6 +82,13 @@ pub trait EnvironmentAccess: Send + Sync {
options: GetMetadataOptions,
) -> ExecutorFileSystemFuture<'a, FileMetadata>;
/// Get one metadata result per path using this accessor's captured permissions.
fn get_metadata_batch<'a>(
&'a self,
paths: &'a [PathUri],
options: GetMetadataOptions,
) -> ExecutorFileSystemFuture<'a, Vec<FileSystemResult<FileMetadata>>>;
fn read_directory<'a>(
&'a self,
path: &'a PathUri,
@@ -294,6 +301,15 @@ impl EnvironmentAccess for FileSystemEnvironmentAccessor<'_> {
.get_metadata(path, options, self.key.sandbox.as_deref())
}
fn get_metadata_batch<'a>(
&'a self,
paths: &'a [PathUri],
options: GetMetadataOptions,
) -> ExecutorFileSystemFuture<'a, Vec<FileSystemResult<FileMetadata>>> {
self.file_system
.get_metadata_batch(paths, options, self.key.sandbox.as_deref())
}
fn read_directory<'a>(
&'a self,
path: &'a PathUri,
+17 -1
View File
@@ -2,6 +2,7 @@ mod environment_accessor;
mod exec_permission_profile_serde;
mod find_up;
mod local_logs;
mod metadata_batch;
mod nfs;
use bytes::Bytes;
@@ -29,6 +30,10 @@ pub use find_up::find_nearest_native_ancestor_with_markers;
use futures::Stream;
use futures::stream::BoxStream;
pub use local_logs::LocalLogsDir;
pub use metadata_batch::MAX_METADATA_BATCH_BYTES;
pub use metadata_batch::MAX_METADATA_BATCH_PROBES;
pub use metadata_batch::metadata_batch_fits;
pub use metadata_batch::resolve_metadata_batch;
pub use nfs::is_nfs;
use serde::Deserialize;
use serde::Serialize;
@@ -112,7 +117,8 @@ pub struct CopyOptions {
pub recursive: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FileMetadata {
pub is_directory: bool,
pub is_file: bool,
@@ -716,6 +722,16 @@ pub trait ExecutorFileSystem: Send + Sync {
sandbox: Option<&'a FileSystemSandboxContext>,
) -> ExecutorFileSystemFuture<'a, FileMetadata>;
/// Return one metadata result or error per input path, in the same order.
fn get_metadata_batch<'a>(
&'a self,
_paths: &'a [PathUri],
_options: GetMetadataOptions,
_sandbox: Option<&'a FileSystemSandboxContext>,
) -> ExecutorFileSystemFuture<'a, Vec<io::Result<FileMetadata>>> {
Box::pin(async { Err(io::ErrorKind::Unsupported.into()) })
}
fn read_directory<'a>(
&'a self,
path: &'a PathUri,
@@ -0,0 +1,39 @@
//! Bounded batches of independent metadata requests.
use crate::ExecutorFileSystem;
use crate::FileMetadata;
use crate::FileSystemSandboxContext;
use crate::GetMetadataOptions;
use codex_utils_path_uri::PathUri;
use futures::StreamExt;
use std::io;
const MAX_CONCURRENT_PROBES: usize = 256;
pub const MAX_METADATA_BATCH_PROBES: usize = 1024;
pub const MAX_METADATA_BATCH_BYTES: usize = 1024 * 1024;
/// Bound both the number of paths and encoded request bytes.
pub fn metadata_batch_fits(paths: &[PathUri]) -> bool {
paths.len() <= MAX_METADATA_BATCH_PROBES
&& paths
.iter()
.try_fold(0usize, |bytes, path| {
bytes
.checked_add(path.to_string().len())
.filter(|total| *total <= MAX_METADATA_BATCH_BYTES)
})
.is_some()
}
/// Fall back to individual calls while preserving each result and request order.
pub async fn resolve_metadata_batch(
fs: &(impl ExecutorFileSystem + ?Sized),
paths: &[PathUri],
options: GetMetadataOptions,
sandbox: Option<&FileSystemSandboxContext>,
) -> io::Result<Vec<io::Result<FileMetadata>>> {
Ok(futures::stream::iter(0..paths.len())
.map(|index: usize| async move { fs.get_metadata(&paths[index], options, sandbox).await })
.buffered(MAX_CONCURRENT_PROBES)
.collect()
.await)
}