Add streaming file APIs

This commit is contained in:
Jeremy Rose
2026-06-09 07:17:12 -07:00
parent 381f0de531
commit 5c8cfd4129
57 changed files with 3903 additions and 3 deletions
+74
View File
@@ -9,8 +9,12 @@ use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::permissions::NetworkSandboxPolicy;
use codex_protocol::protocol::SandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::future::Future;
use std::io;
use std::path::Path;
use std::pin::Pin;
pub const DEFAULT_FILE_STREAM_CHUNK_BYTES: usize = 256 * 1024;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CreateDirectoryOptions {
@@ -37,6 +41,19 @@ pub struct FileMetadata {
pub modified_at_ms: i64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpenFileMetadata {
pub size_bytes: u64,
pub created_at_ms: i64,
pub modified_at_ms: i64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileWriteCommitResult {
pub size_bytes: u64,
pub modified_at_ms: i64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReadDirectoryEntry {
pub file_name: String,
@@ -129,6 +146,41 @@ fn file_system_policy_has_cwd_dependent_entries(
pub type FileSystemResult<T> = io::Result<T>;
/// An open file object used for bounded positional reads.
///
/// Implementations must keep reads attached to the opened file object rather
/// than resolving the original path again for each operation.
pub trait FileReadHandle: Send + Sync {
fn max_chunk_bytes(&self) -> usize;
fn read(
&self,
offset: u64,
max_bytes: usize,
) -> Pin<Box<dyn Future<Output = FileSystemResult<Vec<u8>>> + Send + '_>>;
fn metadata(
&self,
) -> Pin<Box<dyn Future<Output = FileSystemResult<OpenFileMetadata>> + Send + '_>>;
}
/// An open temporary file used to build an atomic whole-file replacement.
///
/// Implementations append complete chunks and expose the destination only
/// after a successful commit. Dropping an uncommitted handle must discard it.
pub trait FileWriteHandle: Send + Sync {
fn max_chunk_bytes(&self) -> usize;
fn write<'a>(
&'a self,
data: &'a [u8],
) -> Pin<Box<dyn Future<Output = FileSystemResult<()>> + Send + 'a>>;
fn commit(
&self,
) -> Pin<Box<dyn Future<Output = FileSystemResult<FileWriteCommitResult>> + Send + '_>>;
}
/// Abstract filesystem access used by components that may operate locally or via
/// a remote environment.
#[async_trait]
@@ -173,6 +225,28 @@ pub trait ExecutorFileSystem: Send + Sync {
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<()>;
async fn open_file_for_read(
&self,
_path: &AbsolutePathBuf,
_sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Box<dyn FileReadHandle>> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"streaming file reads are not supported by this filesystem",
))
}
async fn open_file_for_write(
&self,
_path: &AbsolutePathBuf,
_sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Box<dyn FileWriteHandle>> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"streaming file writes are not supported by this filesystem",
))
}
async fn create_directory(
&self,
path: &AbsolutePathBuf,