Stream app-server fs/readFile responses over stdio (#62599)

## Why

Large file reads over stdio previously allocated both a base64 string
and a complete JSON response before writing to stdout.

## What changed

Keep file contents as bytes until the stdio writer encodes them
directly into the newline-delimited JSON response through a bounded
buffer. Clients still receive the same `dataBase64` field.

## Testing

Compare streamed output byte for byte with the eagerly encoded
response for empty files, inputs that require base64 padding, and
files larger than the write buffer. Check that JSON-RPC conversion
preserves the request ID and base64 payload.

GitOrigin-RevId: 730f201f0905fd15e32b6e9c5b2ef02ca21c7532
This commit is contained in:
Charlie Marsh
2026-11-19 21:41:45 +00:00
committed by copyberry
parent bc75380d6a
commit 665e0a9a65
10 changed files with 150 additions and 20 deletions
+1
View File
@@ -14,6 +14,7 @@ workspace = true
[dependencies]
anyhow = { workspace = true }
base64 = { workspace = true }
codex-experimental-api-macros = { workspace = true }
codex-app-server-protocol-noop-macros = { workspace = true }
codex-extension-items = { workspace = true }
@@ -339,6 +339,7 @@ macro_rules! client_request_definitions {
#[allow(clippy::large_enum_variant)]
pub enum ClientResponsePayload {
$( $variant($response), )*
FsReadFileBytes(v2::FsReadFileBytes),
InterruptConversation(v1::InterruptConversationResponse),
}
@@ -353,6 +354,10 @@ macro_rules! client_request_definitions {
})
}
)*
Self::FsReadFileBytes(file) => Some(ClientResponse::FsReadFile {
request_id,
response: file.into(),
}),
Self::InterruptConversation(_) => None,
}
}
@@ -41,6 +41,10 @@ fn interrupt_conversation_payload_stays_jsonrpc_only() -> Result<()> {
#[test]
fn client_response_jsonrpc_parts_preserve_payloads_and_request_ids() -> Result<()> {
let payloads = [
(
ClientResponsePayload::FsReadFileBytes(v2::FsReadFileBytes { data: vec![0, 255] }),
json!({ "dataBase64": "AP8=" }),
),
(
ClientResponsePayload::GetAuthStatus(v1::GetAuthStatusResponse {
auth_method: Some(AuthMode::Chatgpt),
@@ -1,8 +1,12 @@
use crate::JsonSchema;
use crate::TS;
use base64::Engine;
use base64::display::Base64Display;
use base64::engine::general_purpose::STANDARD;
use codex_utils_absolute_path::AbsolutePathBuf;
use serde::Deserialize;
use serde::Serialize;
use serde::Serializer;
/// Read a file from the host filesystem.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
@@ -22,6 +26,26 @@ pub struct FsReadFileResponse {
pub data_base64: String,
}
/// Outgoing `fs/readFile` payload that encodes the file as base64 during serialization.
#[derive(Serialize, Debug, Clone)]
pub struct FsReadFileBytes {
#[serde(rename = "dataBase64", serialize_with = "serialize_file_data")]
pub data: Vec<u8>,
}
fn serialize_file_data<S: Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
// serde_json writes Display fragments directly into the JSON string.
serializer.collect_str(&Base64Display::new(data, &STANDARD))
}
impl From<FsReadFileBytes> for FsReadFileResponse {
fn from(file: FsReadFileBytes) -> Self {
Self {
data_base64: STANDARD.encode(file.data),
}
}
}
/// Write a file on the host filesystem.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]