Match streamed writes to writeFile semantics

This commit is contained in:
Jeremy Rose
2026-06-10 10:13:53 -07:00
parent f03bc23e89
commit cb96f207af
32 changed files with 89 additions and 529 deletions
+2 -38
View File
@@ -1015,19 +1015,7 @@
"type": "object"
},
"FsWriteFileCloseParams": {
"description": "Close an open write handle and discard its temporary file.",
"properties": {
"handleId": {
"type": "string"
}
},
"required": [
"handleId"
],
"type": "object"
},
"FsWriteFileCommitParams": {
"description": "Atomically publish an open write handle.",
"description": "Close an open write handle.",
"properties": {
"handleId": {
"type": "string"
@@ -1039,7 +1027,7 @@
"type": "object"
},
"FsWriteFileOpenParams": {
"description": "Open a destination-local temporary file for streamed writes.",
"description": "Create or truncate a file for streamed writes.",
"properties": {
"handleId": {
"description": "Client-supplied handle identifier scoped to this connection.",
@@ -5532,30 +5520,6 @@
"title": "Fs/writeFile/writeRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"fs/writeFile/commit"
],
"title": "Fs/writeFile/commitRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/FsWriteFileCommitParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/writeFile/commitRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -1261,30 +1261,6 @@
"title": "Fs/writeFile/writeRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
"fs/writeFile/commit"
],
"title": "Fs/writeFile/commitRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/v2/FsWriteFileCommitParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/writeFile/commitRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -9881,7 +9857,7 @@
},
"FsWriteFileCloseParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Close an open write handle and discard its temporary file.",
"description": "Close an open write handle.",
"properties": {
"handleId": {
"type": "string"
@@ -9899,44 +9875,9 @@
"title": "FsWriteFileCloseResponse",
"type": "object"
},
"FsWriteFileCommitParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Atomically publish an open write handle.",
"properties": {
"handleId": {
"type": "string"
}
},
"required": [
"handleId"
],
"title": "FsWriteFileCommitParams",
"type": "object"
},
"FsWriteFileCommitResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Metadata returned after a successful write commit.",
"properties": {
"modifiedAtMs": {
"format": "int64",
"type": "integer"
},
"sizeBytes": {
"format": "uint64",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"modifiedAtMs",
"sizeBytes"
],
"title": "FsWriteFileCommitResponse",
"type": "object"
},
"FsWriteFileOpenParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Open a destination-local temporary file for streamed writes.",
"description": "Create or truncate a file for streamed writes.",
"properties": {
"handleId": {
"description": "Client-supplied handle identifier scoped to this connection.",
@@ -2170,30 +2170,6 @@
"title": "Fs/writeFile/writeRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"fs/writeFile/commit"
],
"title": "Fs/writeFile/commitRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/FsWriteFileCommitParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/writeFile/commitRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -6223,7 +6199,7 @@
},
"FsWriteFileCloseParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Close an open write handle and discard its temporary file.",
"description": "Close an open write handle.",
"properties": {
"handleId": {
"type": "string"
@@ -6241,44 +6217,9 @@
"title": "FsWriteFileCloseResponse",
"type": "object"
},
"FsWriteFileCommitParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Atomically publish an open write handle.",
"properties": {
"handleId": {
"type": "string"
}
},
"required": [
"handleId"
],
"title": "FsWriteFileCommitParams",
"type": "object"
},
"FsWriteFileCommitResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Metadata returned after a successful write commit.",
"properties": {
"modifiedAtMs": {
"format": "int64",
"type": "integer"
},
"sizeBytes": {
"format": "uint64",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"modifiedAtMs",
"sizeBytes"
],
"title": "FsWriteFileCommitResponse",
"type": "object"
},
"FsWriteFileOpenParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Open a destination-local temporary file for streamed writes.",
"description": "Create or truncate a file for streamed writes.",
"properties": {
"handleId": {
"description": "Client-supplied handle identifier scoped to this connection.",
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Close an open write handle and discard its temporary file.",
"description": "Close an open write handle.",
"properties": {
"handleId": {
"type": "string"
@@ -1,14 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Atomically publish an open write handle.",
"properties": {
"handleId": {
"type": "string"
}
},
"required": [
"handleId"
],
"title": "FsWriteFileCommitParams",
"type": "object"
}
@@ -1,21 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Metadata returned after a successful write commit.",
"properties": {
"modifiedAtMs": {
"format": "int64",
"type": "integer"
},
"sizeBytes": {
"format": "uint64",
"minimum": 0.0,
"type": "integer"
}
},
"required": [
"modifiedAtMs",
"sizeBytes"
],
"title": "FsWriteFileCommitResponse",
"type": "object"
}
@@ -6,7 +6,7 @@
"type": "string"
}
},
"description": "Open a destination-local temporary file for streamed writes.",
"description": "Create or truncate a file for streamed writes.",
"properties": {
"handleId": {
"description": "Client-supplied handle identifier scoped to this connection.",
File diff suppressed because one or more lines are too long
@@ -3,6 +3,6 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
/**
* Close an open write handle and discard its temporary file.
* Close an open write handle.
*/
export type FsWriteFileCloseParams = { handleId: string, };
@@ -1,8 +0,0 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
/**
* Atomically publish an open write handle.
*/
export type FsWriteFileCommitParams = { handleId: string, };
@@ -1,8 +0,0 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
/**
* Metadata returned after a successful write commit.
*/
export type FsWriteFileCommitResponse = { sizeBytes: number, modifiedAtMs: number, };
@@ -4,7 +4,7 @@
import type { AbsolutePathBuf } from "../AbsolutePathBuf";
/**
* Open a destination-local temporary file for streamed writes.
* Create or truncate a file for streamed writes.
*/
export type FsWriteFileOpenParams = {
/**
@@ -148,8 +148,6 @@ export type { FsWatchParams } from "./FsWatchParams";
export type { FsWatchResponse } from "./FsWatchResponse";
export type { FsWriteFileCloseParams } from "./FsWriteFileCloseParams";
export type { FsWriteFileCloseResponse } from "./FsWriteFileCloseResponse";
export type { FsWriteFileCommitParams } from "./FsWriteFileCommitParams";
export type { FsWriteFileCommitResponse } from "./FsWriteFileCommitResponse";
export type { FsWriteFileOpenParams } from "./FsWriteFileOpenParams";
export type { FsWriteFileOpenResponse } from "./FsWriteFileOpenResponse";
export type { FsWriteFileParams } from "./FsWriteFileParams";
@@ -748,11 +748,6 @@ client_request_definitions! {
serialization: file_handle_id(params.handle_id),
response: v2::FsWriteFileWriteResponse,
},
FsWriteFileCommit => "fs/writeFile/commit" {
params: v2::FsWriteFileCommitParams,
serialization: file_handle_id(params.handle_id),
response: v2::FsWriteFileCommitResponse,
},
FsWriteFileClose => "fs/writeFile/close" {
params: v2::FsWriteFileCloseParams,
serialization: None,
@@ -119,7 +119,7 @@ pub struct FsReadFileCloseParams {
#[ts(export_to = "v2/")]
pub struct FsReadFileCloseResponse {}
/// Open a destination-local temporary file for streamed writes.
/// Create or truncate a file for streamed writes.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
@@ -154,26 +154,7 @@ pub struct FsWriteFileWriteParams {
#[ts(export_to = "v2/")]
pub struct FsWriteFileWriteResponse {}
/// Atomically publish an open write handle.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsWriteFileCommitParams {
pub handle_id: String,
}
/// Metadata returned after a successful write commit.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsWriteFileCommitResponse {
#[ts(type = "number")]
pub size_bytes: u64,
#[ts(type = "number")]
pub modified_at_ms: i64,
}
/// Close an open write handle and discard its temporary file.
/// Close an open write handle.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
+3 -3
View File
@@ -181,7 +181,7 @@ Example with notification opt-out:
- `fs/readFile` — read an absolute file path and return `{ dataBase64 }`.
- `fs/writeFile` — write an absolute file path from base64-encoded `{ dataBase64 }`; returns `{}`.
- `fs/readFile/open`, `fs/readFile/read`, `fs/readFile/stat`, `fs/readFile/close` — bounded positional reads through a connection-scoped file handle.
- `fs/writeFile/open`, `fs/writeFile/write`, `fs/writeFile/commit`, `fs/writeFile/close` — bounded sequential writes to a destination-local temporary file, atomically published by commit.
- `fs/writeFile/open`, `fs/writeFile/write`, `fs/writeFile/close` — bounded sequential writes that create or truncate the destination directly.
- `fs/createDirectory` — create an absolute directory path; `recursive` defaults to `true`.
- `fs/getMetadata` — return metadata for an absolute path: `isDirectory`, `isFile`, `isSymlink`, `createdAtMs`, and `modifiedAtMs`.
- `fs/readDirectory` — list direct child entries for an absolute directory path; each entry contains `fileName`, `isDirectory`, and `isFile`, and `fileName` is just the child name, not a path.
@@ -1163,8 +1163,8 @@ All filesystem paths in this section must be absolute.
- Streaming file handles use caller-supplied `handleId` values scoped to one connection. The server advertises a 262144-byte decoded chunk limit from each open response.
- `fs/readFile/read` is positional: pass an absolute byte `offset` and optional `maxBytes`. `eof` reports whether more bytes were available at that read; a growing file may later return more data.
- `fs/readFile/stat` reads metadata from the opened file object, so it remains valid if the original path is renamed or replaced.
- Streamed writes remain in a destination-local temporary file until `fs/writeFile/commit`, which flushes the file and atomically replaces the destination.
- `fs/readFile/close` and `fs/writeFile/close` are idempotent cancellation operations. A write commit that has started runs to completion. Disconnecting closes all handles owned by that connection.
- Opening a streamed write creates or truncates the destination immediately. Completed chunks are visible immediately, and cancellation, disconnection, or an I/O error may leave a partially written file. Existing file identity and metadata are preserved like `fs/writeFile`.
- `fs/readFile/close` and `fs/writeFile/close` are idempotent cancellation operations. Disconnecting closes all handles owned by that connection.
- `fs/copy` handles both file copies and directory-tree copies; it requires `recursive: true` when `sourcePath` is a directory. Recursive copies traverse regular files, directories, and symlinks; other entry types are skipped.
### Example: Filesystem watch
@@ -1004,11 +1004,6 @@ impl MessageProcessor {
.write_file_write(connection_id, params)
.await
.map(|response| Some(response.into())),
ClientRequest::FsWriteFileCommit { params, .. } => self
.fs_processor
.write_file_commit(connection_id, params)
.await
.map(|response| Some(response.into())),
ClientRequest::FsWriteFileClose { params, .. } => self
.fs_processor
.write_file_close(connection_id, params)
@@ -31,8 +31,6 @@ use codex_app_server_protocol::FsWatchParams;
use codex_app_server_protocol::FsWatchResponse;
use codex_app_server_protocol::FsWriteFileCloseParams;
use codex_app_server_protocol::FsWriteFileCloseResponse;
use codex_app_server_protocol::FsWriteFileCommitParams;
use codex_app_server_protocol::FsWriteFileCommitResponse;
use codex_app_server_protocol::FsWriteFileOpenParams;
use codex_app_server_protocol::FsWriteFileOpenResponse;
use codex_app_server_protocol::FsWriteFileParams;
@@ -240,23 +238,6 @@ impl FsRequestProcessor {
Ok(FsWriteFileWriteResponse {})
}
pub(crate) async fn write_file_commit(
&self,
connection_id: ConnectionId,
params: FsWriteFileCommitParams,
) -> Result<FsWriteFileCommitResponse, JSONRPCErrorError> {
let result = self
.handles(connection_id)
.await
.commit_write(&params.handle_id)
.await
.map_err(map_file_handle_error)?;
Ok(FsWriteFileCommitResponse {
size_bytes: result.size_bytes,
modified_at_ms: result.modified_at_ms,
})
}
pub(crate) async fn write_file_close(
&self,
connection_id: ConnectionId,
+10 -13
View File
@@ -14,7 +14,6 @@ use codex_app_server_protocol::FsReadFileResponse;
use codex_app_server_protocol::FsReadFileStatResponse;
use codex_app_server_protocol::FsUnwatchParams;
use codex_app_server_protocol::FsWatchResponse;
use codex_app_server_protocol::FsWriteFileCommitResponse;
use codex_app_server_protocol::FsWriteFileOpenResponse;
use codex_app_server_protocol::FsWriteFileParams;
use codex_app_server_protocol::JSONRPCNotification;
@@ -505,7 +504,7 @@ async fn fs_streaming_read_supports_stat_and_positional_reads() -> Result<()> {
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fs_streaming_write_is_atomic_and_protocol_errors_keep_handle_open() -> Result<()> {
async fn fs_streaming_write_is_direct() -> Result<()> {
let codex_home = TempDir::new()?;
let file_path = codex_home.path().join("stream.txt");
std::fs::write(&file_path, "old")?;
@@ -528,6 +527,7 @@ async fn fs_streaming_write_is_atomic_and_protocol_errors_keep_handle_open() ->
.await??,
)?;
assert!(open.max_chunk_bytes >= 3);
assert_eq!(std::fs::read_to_string(&file_path)?, "");
let wrong_type_id = mcp
.send_raw_request(
@@ -580,24 +580,21 @@ async fn fs_streaming_write_is_atomic_and_protocol_errors_keep_handle_open() ->
mcp.read_stream_until_response_message(RequestId::Integer(write_id)),
)
.await??;
assert_eq!(std::fs::read_to_string(&file_path)?, "old");
assert_eq!(std::fs::read_to_string(&file_path)?, "new");
let commit_id = mcp
let close_id = mcp
.send_raw_request(
"fs/writeFile/commit",
"fs/writeFile/close",
Some(json!({
"handleId": "write-1",
})),
)
.await?;
let commit: FsWriteFileCommitResponse = to_response(
timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(commit_id)),
)
.await??,
)?;
assert_eq!(commit.size_bytes, 3);
timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(close_id)),
)
.await??;
assert_eq!(std::fs::read_to_string(&file_path)?, "new");
Ok(())
-1
View File
@@ -349,7 +349,6 @@ or unavailable paths:
- `fs/readFile/close`
- `fs/writeFile/open`
- `fs/writeFile/write`
- `fs/writeFile/commit`
- `fs/writeFile/close`
- `fs/createDirectory`
- `fs/getMetadata`
-10
View File
@@ -57,7 +57,6 @@ use crate::protocol::FS_READ_FILE_READ_METHOD;
use crate::protocol::FS_READ_FILE_STAT_METHOD;
use crate::protocol::FS_REMOVE_METHOD;
use crate::protocol::FS_WRITE_FILE_CLOSE_METHOD;
use crate::protocol::FS_WRITE_FILE_COMMIT_METHOD;
use crate::protocol::FS_WRITE_FILE_METHOD;
use crate::protocol::FS_WRITE_FILE_OPEN_METHOD;
use crate::protocol::FS_WRITE_FILE_WRITE_METHOD;
@@ -89,8 +88,6 @@ use crate::protocol::FsRemoveParams;
use crate::protocol::FsRemoveResponse;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileCloseResponse;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileCommitResponse;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileOpenResponse;
use crate::protocol::FsWriteFileParams;
@@ -487,13 +484,6 @@ impl ExecServerClient {
self.call(FS_WRITE_FILE_WRITE_METHOD, &params).await
}
pub async fn fs_write_file_commit(
&self,
params: FsWriteFileCommitParams,
) -> Result<FsWriteFileCommitResponse, ExecServerError> {
self.call(FS_WRITE_FILE_COMMIT_METHOD, &params).await
}
pub async fn fs_write_file_close(
&self,
params: FsWriteFileCloseParams,
@@ -5,7 +5,6 @@ use std::sync::Arc;
use codex_file_system::ExecutorFileSystem;
use codex_file_system::FileReadHandle;
use codex_file_system::FileSystemSandboxContext;
use codex_file_system::FileWriteCommitResult;
use codex_file_system::FileWriteHandle;
use codex_file_system::OpenFileMetadata;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -185,13 +184,6 @@ impl FileHandleManager {
result
}
pub async fn commit_write(&self, handle_id: &str) -> io::Result<FileWriteCommitResult> {
let entry = self.write_entry(handle_id).await?;
let result = entry.handle.commit().await;
self.remove_if_write(handle_id, &entry).await;
result
}
pub async fn close(&self, handle_id: &str) {
let handle = self.handles.lock().await.remove(handle_id);
match handle {
@@ -196,14 +196,6 @@ impl FileWriteHandle for TestWriteHandle {
) -> std::pin::Pin<Box<dyn std::future::Future<Output = io::Result<()>> + Send + 'a>> {
unreachable!("write is not used by these tests")
}
fn commit(
&self,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = io::Result<FileWriteCommitResult>> + Send + '_>,
> {
unreachable!("commit is not used by these tests")
}
}
impl Drop for TestWriteHandle {
-3
View File
@@ -39,7 +39,6 @@ pub use codex_file_system::FileMetadata;
pub use codex_file_system::FileReadHandle;
pub use codex_file_system::FileSystemResult;
pub use codex_file_system::FileSystemSandboxContext;
pub use codex_file_system::FileWriteCommitResult;
pub use codex_file_system::FileWriteHandle;
pub use codex_file_system::OpenFileMetadata;
pub use codex_file_system::ReadDirectoryEntry;
@@ -101,8 +100,6 @@ pub use protocol::FsRemoveParams;
pub use protocol::FsRemoveResponse;
pub use protocol::FsWriteFileCloseParams;
pub use protocol::FsWriteFileCloseResponse;
pub use protocol::FsWriteFileCommitParams;
pub use protocol::FsWriteFileCommitResponse;
pub use protocol::FsWriteFileOpenParams;
pub use protocol::FsWriteFileOpenResponse;
pub use protocol::FsWriteFileParams;
+4 -70
View File
@@ -1,7 +1,6 @@
use async_trait::async_trait;
use codex_file_system::DEFAULT_FILE_STREAM_CHUNK_BYTES;
use codex_file_system::FileReadHandle;
use codex_file_system::FileWriteCommitResult;
use codex_file_system::FileWriteHandle;
use codex_file_system::OpenFileMetadata;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -11,7 +10,6 @@ use std::sync::Arc;
use std::sync::LazyLock;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use tempfile::NamedTempFile;
use tokio::io;
use crate::CopyOptions;
@@ -26,7 +24,6 @@ use crate::RemoveOptions;
use crate::sandboxed_file_system::SandboxedFileSystem;
const MAX_READ_FILE_BYTES: u64 = 512 * 1024 * 1024;
const STREAMING_WRITE_TEMP_FILE_PREFIX: &str = ".codex-tmp-";
pub static LOCAL_FS: LazyLock<Arc<dyn ExecutorFileSystem>> =
LazyLock::new(|| -> Arc<dyn ExecutorFileSystem> { Arc::new(LocalFileSystem::unsandboxed()) });
@@ -390,24 +387,8 @@ impl ExecutorFileSystem for DirectFileSystem {
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Box<dyn FileWriteHandle>> {
reject_sandbox_context(sandbox)?;
let destination = path.as_path().to_path_buf();
let parent = destination.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"streaming file destination must have a parent directory",
)
})?;
let temp_file = tempfile::Builder::new()
.prefix(STREAMING_WRITE_TEMP_FILE_PREFIX)
.tempfile_in(parent)?;
let file = temp_file.as_file().try_clone()?;
Ok(Box::new(DirectFileWriteHandle {
state: std::sync::Mutex::new(Some(DirectFileWriteState {
destination,
temp_file,
file,
})),
}))
let file = std::fs::File::create(path.as_path())?;
Ok(Box::new(DirectFileWriteHandle { file }))
}
async fn create_directory(
@@ -611,14 +592,8 @@ fn read_file_at(file: &std::fs::File, bytes: &mut [u8], offset: u64) -> io::Resu
std::io::Read::read(&mut file, bytes)
}
struct DirectFileWriteState {
destination: PathBuf,
temp_file: NamedTempFile,
file: std::fs::File,
}
struct DirectFileWriteHandle {
state: std::sync::Mutex<Option<DirectFileWriteState>>,
file: std::fs::File,
}
impl FileWriteHandle for DirectFileWriteHandle {
@@ -631,13 +606,7 @@ impl FileWriteHandle for DirectFileWriteHandle {
data: &'a [u8],
) -> std::pin::Pin<Box<dyn std::future::Future<Output = FileSystemResult<()>> + Send + 'a>>
{
let file = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_ref()
.ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "file write handle is closed"))
.and_then(|state| state.file.try_clone());
let file = self.file.try_clone();
Box::pin(async move {
if data.len() > DEFAULT_FILE_STREAM_CHUNK_BYTES {
return Err(io::Error::new(
@@ -657,41 +626,6 @@ impl FileWriteHandle for DirectFileWriteHandle {
.map_err(|err| io::Error::other(format!("filesystem task failed: {err}")))?
})
}
fn commit(
&self,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = FileSystemResult<FileWriteCommitResult>> + Send + '_>,
> {
let state = self
.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take();
Box::pin(async move {
tokio::task::spawn_blocking(move || {
let Some(state) = state else {
return Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"file write handle is closed",
));
};
state.file.sync_all()?;
drop(state.file);
let persisted_file = state
.temp_file
.persist(&state.destination)
.map_err(|err| err.error)?;
let metadata = persisted_file.metadata()?;
Ok(FileWriteCommitResult {
size_bytes: metadata.len(),
modified_at_ms: metadata.modified().ok().map_or(0, system_time_to_unix_ms),
})
})
.await
.map_err(|err| io::Error::other(format!("filesystem task failed: {err}")))?
})
}
}
fn reject_sandbox_context(sandbox: Option<&FileSystemSandboxContext>) -> io::Result<()> {
-14
View File
@@ -28,7 +28,6 @@ pub const FS_READ_FILE_STAT_METHOD: &str = "fs/readFile/stat";
pub const FS_READ_FILE_CLOSE_METHOD: &str = "fs/readFile/close";
pub const FS_WRITE_FILE_OPEN_METHOD: &str = "fs/writeFile/open";
pub const FS_WRITE_FILE_WRITE_METHOD: &str = "fs/writeFile/write";
pub const FS_WRITE_FILE_COMMIT_METHOD: &str = "fs/writeFile/commit";
pub const FS_WRITE_FILE_CLOSE_METHOD: &str = "fs/writeFile/close";
pub const FS_CREATE_DIRECTORY_METHOD: &str = "fs/createDirectory";
pub const FS_GET_METADATA_METHOD: &str = "fs/getMetadata";
@@ -289,19 +288,6 @@ pub struct FsWriteFileWriteParams {
#[serde(rename_all = "camelCase")]
pub struct FsWriteFileWriteResponse {}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FsWriteFileCommitParams {
pub handle_id: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FsWriteFileCommitResponse {
pub size_bytes: u64,
pub modified_at_ms: i64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FsWriteFileCloseParams {
@@ -2,7 +2,6 @@ use async_trait::async_trait;
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use codex_file_system::FileReadHandle;
use codex_file_system::FileWriteCommitResult;
use codex_file_system::FileWriteHandle;
use codex_file_system::OpenFileMetadata;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -34,7 +33,6 @@ use crate::protocol::FsReadFileReadParams;
use crate::protocol::FsReadFileStatParams;
use crate::protocol::FsRemoveParams;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileParams;
use crate::protocol::FsWriteFileWriteParams;
@@ -392,26 +390,6 @@ impl FileWriteHandle for RemoteFileWriteHandle {
Ok(())
})
}
fn commit(
&self,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = FileSystemResult<FileWriteCommitResult>> + Send + '_>,
> {
Box::pin(async move {
let response = self
.client
.fs_write_file_commit(FsWriteFileCommitParams {
handle_id: self.handle_id.clone(),
})
.await
.map_err(map_remote_error)?;
Ok(FileWriteCommitResult {
size_bytes: response.size_bytes,
modified_at_ms: response.modified_at_ms,
})
})
}
}
impl Drop for RemoteFileWriteHandle {
@@ -41,8 +41,6 @@ use crate::protocol::FsRemoveParams;
use crate::protocol::FsRemoveResponse;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileCloseResponse;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileCommitResponse;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileOpenResponse;
use crate::protocol::FsWriteFileParams;
@@ -190,21 +188,6 @@ impl FileSystemHandler {
Ok(FsWriteFileWriteResponse {})
}
pub(crate) async fn write_file_commit(
&self,
params: FsWriteFileCommitParams,
) -> Result<FsWriteFileCommitResponse, JSONRPCErrorError> {
let result = self
.handles
.commit_write(&params.handle_id)
.await
.map_err(map_fs_error)?;
Ok(FsWriteFileCommitResponse {
size_bytes: result.size_bytes,
modified_at_ms: result.modified_at_ms,
})
}
pub(crate) async fn write_file_close(
&self,
params: FsWriteFileCloseParams,
@@ -362,7 +345,7 @@ mod tests {
use crate::protocol::FsReadFileParams;
use crate::protocol::FsReadFileReadParams;
use crate::protocol::FsReadFileStatParams;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileParams;
use crate::protocol::FsWriteFileWriteParams;
@@ -420,7 +403,7 @@ mod tests {
}
#[tokio::test]
async fn streamed_file_operations_are_positional_and_commit_atomically() {
async fn streamed_file_operations_are_positional_and_write_directly() {
let temp_dir = tempfile::tempdir().expect("tempdir");
let runtime_paths = ExecServerRuntimePaths::new(
std::env::current_exe().expect("current exe"),
@@ -431,7 +414,19 @@ mod tests {
let path =
AbsolutePathBuf::from_absolute_path(temp_dir.path().join("stream.txt").as_path())
.expect("absolute path");
let linked_path = temp_dir.path().join("linked.txt");
std::fs::write(&path, "old").expect("write initial file");
std::fs::hard_link(&path, &linked_path).expect("create hard link");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut permissions = std::fs::metadata(&path)
.expect("stat initial file")
.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&path, permissions).expect("make initial file executable");
}
handler
.write_file_open(FsWriteFileOpenParams {
@@ -441,20 +436,10 @@ mod tests {
})
.await
.expect("open write handle");
let temp_file_name = std::fs::read_dir(temp_dir.path())
.expect("read tempdir")
.map(|entry| {
entry
.expect("read tempdir entry")
.file_name()
.to_string_lossy()
.into_owned()
})
.find(|file_name| file_name != "stream.txt")
.expect("streaming write temp file");
assert!(
temp_file_name.starts_with(".codex-tmp-"),
"unexpected streaming write temp file name: {temp_file_name}"
assert_eq!(std::fs::read_to_string(&path).expect("read file"), "");
assert_eq!(
std::fs::read_to_string(&linked_path).expect("read hard link"),
""
);
handler
.write_file_write(FsWriteFileWriteParams {
@@ -464,20 +449,32 @@ mod tests {
.await
.expect("write chunk");
assert_eq!(
std::fs::read_to_string(&path).expect("read old file"),
"old"
std::fs::read_to_string(&path).expect("read written file"),
"new"
);
let commit = handler
.write_file_commit(FsWriteFileCommitParams {
assert_eq!(
std::fs::read_to_string(&linked_path).expect("read written hard link"),
"new"
);
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
let metadata = std::fs::metadata(&path).expect("stat written file");
let linked_metadata = std::fs::metadata(&linked_path).expect("stat written hard link");
assert_eq!(
(metadata.dev(), metadata.ino()),
(linked_metadata.dev(), linked_metadata.ino())
);
assert_eq!(metadata.permissions().mode() & 0o777, 0o755);
}
handler
.write_file_close(FsWriteFileCloseParams {
handle_id: "write-1".to_string(),
})
.await
.expect("commit write");
assert_eq!(commit.size_bytes, 3);
assert_eq!(
std::fs::read_to_string(&path).expect("read new file"),
"new"
);
.expect("close write handle");
handler
.read_file_open(FsReadFileOpenParams {
@@ -45,8 +45,6 @@ use crate::protocol::FsRemoveParams;
use crate::protocol::FsRemoveResponse;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileCloseResponse;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileCommitResponse;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileOpenResponse;
use crate::protocol::FsWriteFileParams;
@@ -301,14 +299,6 @@ impl ExecServerHandler {
self.file_system.write_file_write(params).await
}
pub(crate) async fn fs_write_file_commit(
&self,
params: FsWriteFileCommitParams,
) -> Result<FsWriteFileCommitResponse, JSONRPCErrorError> {
self.require_initialized_for("filesystem")?;
self.file_system.write_file_commit(params).await
}
pub(crate) async fn fs_write_file_close(
&self,
params: FsWriteFileCloseParams,
@@ -20,7 +20,6 @@ use crate::protocol::FS_READ_FILE_READ_METHOD;
use crate::protocol::FS_READ_FILE_STAT_METHOD;
use crate::protocol::FS_REMOVE_METHOD;
use crate::protocol::FS_WRITE_FILE_CLOSE_METHOD;
use crate::protocol::FS_WRITE_FILE_COMMIT_METHOD;
use crate::protocol::FS_WRITE_FILE_METHOD;
use crate::protocol::FS_WRITE_FILE_OPEN_METHOD;
use crate::protocol::FS_WRITE_FILE_WRITE_METHOD;
@@ -38,7 +37,6 @@ use crate::protocol::FsReadFileReadParams;
use crate::protocol::FsReadFileStatParams;
use crate::protocol::FsRemoveParams;
use crate::protocol::FsWriteFileCloseParams;
use crate::protocol::FsWriteFileCommitParams;
use crate::protocol::FsWriteFileOpenParams;
use crate::protocol::FsWriteFileParams;
use crate::protocol::FsWriteFileWriteParams;
@@ -147,12 +145,6 @@ pub(crate) fn build_router() -> RpcRouter<ExecServerHandler> {
handler.fs_write_file_write(params).await
},
);
router.request(
FS_WRITE_FILE_COMMIT_METHOD,
|handler: Arc<ExecServerHandler>, params: FsWriteFileCommitParams| async move {
handler.fs_write_file_commit(params).await
},
);
router.request(
FS_WRITE_FILE_CLOSE_METHOD,
|handler: Arc<ExecServerHandler>, params: FsWriteFileCloseParams| async move {
+4 -13
View File
@@ -48,12 +48,6 @@ pub struct OpenFileMetadata {
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,
@@ -164,10 +158,11 @@ pub trait FileReadHandle: Send + Sync {
) -> Pin<Box<dyn Future<Output = FileSystemResult<OpenFileMetadata>> + Send + '_>>;
}
/// An open temporary file used to build an atomic whole-file replacement.
/// An open file used for bounded sequential writes.
///
/// Implementations append complete chunks and expose the destination only
/// after a successful commit. Dropping an uncommitted handle must discard it.
/// Implementations must match ordinary whole-file write behavior: opening the
/// handle creates or truncates the destination, and writes append complete
/// chunks to that open file object.
pub trait FileWriteHandle: Send + Sync {
fn max_chunk_bytes(&self) -> usize;
@@ -175,10 +170,6 @@ pub trait FileWriteHandle: Send + Sync {
&'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
+15 -34
View File
@@ -12,7 +12,8 @@ The existing buffered `fs/readFile` and `fs/writeFile` methods remain unchanged.
- Provide backpressure through bounded request/response chunks.
- Allow positional reads.
- Support cancellation and deterministic cleanup.
- Atomically replace destination files after successful streamed writes.
- Match the existing whole-file write behavior, including preserving the
identity and metadata of existing destination files.
## Read API
@@ -104,9 +105,9 @@ Response:
### `fs/writeFile/open`
Creates an empty temporary file in the destination directory. Its name starts
with `.codex-tmp-` so abandoned files are attributable and hidden on platforms
where dot-prefixed files are hidden.
Creates the destination if needed or truncates the existing file. Existing file
identity and metadata, including hardlinks and platform access controls, are
preserved in the same way as `fs/writeFile`.
```json
{
@@ -125,8 +126,9 @@ Response:
### `fs/writeFile/write`
Decodes and appends the complete chunk to the temporary file. A successful
response acknowledges the entire decoded chunk; partial success is not exposed.
Decodes and appends the complete chunk to the open destination file. A
successful response acknowledges the entire decoded chunk; partial success is
not exposed.
```json
{
@@ -141,29 +143,9 @@ Response:
{}
```
### `fs/writeFile/commit`
Flushes the completed file, atomically replaces the destination, and closes the
write handle.
```json
{
"handleId": "write-1"
}
```
Response:
```json
{
"sizeBytes": 5,
"modifiedAtMs": 1730910000000
}
```
### `fs/writeFile/close`
Closes the write handle and deletes the uncommitted temporary file.
Closes the write handle.
```json
{
@@ -193,16 +175,15 @@ Response:
round-trip latency without accumulating unbounded response data.
- Close operations are idempotent. App-server close requests bypass queued
operations and cancel an active open or chunk operation. A close received
while open is pending prevents that handle from becoming live. Once commit
starts, it runs to completion so the server never reports cancellation while
an atomic replacement may still publish. Exec-server handles bounded file
RPCs in request order, so close takes effect before the next file operation.
while open is pending prevents that handle from becoming live. Exec-server
handles bounded file RPCs in request order, so close takes effect before the
next file operation.
- Closing a connection closes all of its handles.
- Any filesystem or I/O error closes the affected handle. Protocol errors such
as an unknown handle do not affect other handles.
- Failed or cancelled writes delete their temporary files.
- Write commit replaces the destination, matching existing `fs/writeFile`
overwrite behavior.
- Streamed writes are visible as they complete. Opening truncates an existing
destination, and an I/O error, cancellation, or disconnect may leave a
partially written file.
- Errors use normal JSON-RPC error responses.
- App-server operations target the app-server host filesystem. Exec-server
exposes the same pull-based handle operations for remote filesystem clients.