mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Route unified exec and filesystem tools through exec-server
Add exec-server filesystem RPCs and a core-side remote filesystem client, then route unified-exec and filesystem-backed tools through that backend when enabled by config. Also add Docker-backed remote exec integration coverage for the local codex-exec CLI. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -15,6 +15,7 @@ workspace = true
|
||||
base64 = { workspace = true }
|
||||
clap = { workspace = true, features = ["derive"] }
|
||||
codex-app-server-protocol = { workspace = true }
|
||||
codex-environment = { workspace = true }
|
||||
codex-utils-pty = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
|
||||
@@ -8,6 +8,20 @@ use std::sync::atomic::AtomicI64;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCopyResponse;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryParams;
|
||||
use codex_app_server_protocol::FsReadDirectoryResponse;
|
||||
use codex_app_server_protocol::FsReadFileParams;
|
||||
use codex_app_server_protocol::FsReadFileResponse;
|
||||
use codex_app_server_protocol::FsRemoveParams;
|
||||
use codex_app_server_protocol::FsRemoveResponse;
|
||||
use codex_app_server_protocol::FsWriteFileParams;
|
||||
use codex_app_server_protocol::FsWriteFileResponse;
|
||||
use codex_app_server_protocol::JSONRPCError;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
@@ -44,6 +58,13 @@ use crate::protocol::ExecExitedNotification;
|
||||
use crate::protocol::ExecOutputDeltaNotification;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::FS_COPY_METHOD;
|
||||
use crate::protocol::FS_CREATE_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_GET_METADATA_METHOD;
|
||||
use crate::protocol::FS_READ_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_READ_FILE_METHOD;
|
||||
use crate::protocol::FS_REMOVE_METHOD;
|
||||
use crate::protocol::FS_WRITE_FILE_METHOD;
|
||||
use crate::protocol::INITIALIZE_METHOD;
|
||||
use crate::protocol::INITIALIZED_METHOD;
|
||||
use crate::protocol::InitializeParams;
|
||||
@@ -165,6 +186,13 @@ enum PendingRequest {
|
||||
Read(oneshot::Sender<Result<ReadResponse, JSONRPCErrorError>>),
|
||||
Write(oneshot::Sender<Result<WriteResponse, JSONRPCErrorError>>),
|
||||
Terminate(oneshot::Sender<Result<TerminateResponse, JSONRPCErrorError>>),
|
||||
FsReadFile(oneshot::Sender<Result<FsReadFileResponse, JSONRPCErrorError>>),
|
||||
FsWriteFile(oneshot::Sender<Result<FsWriteFileResponse, JSONRPCErrorError>>),
|
||||
FsCreateDirectory(oneshot::Sender<Result<FsCreateDirectoryResponse, JSONRPCErrorError>>),
|
||||
FsGetMetadata(oneshot::Sender<Result<FsGetMetadataResponse, JSONRPCErrorError>>),
|
||||
FsReadDirectory(oneshot::Sender<Result<FsReadDirectoryResponse, JSONRPCErrorError>>),
|
||||
FsRemove(oneshot::Sender<Result<FsRemoveResponse, JSONRPCErrorError>>),
|
||||
FsCopy(oneshot::Sender<Result<FsCopyResponse, JSONRPCErrorError>>),
|
||||
}
|
||||
|
||||
impl PendingRequest {
|
||||
@@ -185,6 +213,27 @@ impl PendingRequest {
|
||||
PendingRequest::Terminate(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsReadFile(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsWriteFile(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsCreateDirectory(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsGetMetadata(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsReadDirectory(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsRemove(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
PendingRequest::FsCopy(tx) => {
|
||||
let _ = tx.send(Ok(serde_json::from_value(result)?));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -206,6 +255,27 @@ impl PendingRequest {
|
||||
PendingRequest::Terminate(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsReadFile(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsWriteFile(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsCreateDirectory(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsGetMetadata(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsReadDirectory(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsRemove(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
PendingRequest::FsCopy(tx) => {
|
||||
let _ = tx.send(Err(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -479,6 +549,99 @@ impl ExecServerClient {
|
||||
self.terminate_session(process_id).await
|
||||
}
|
||||
|
||||
pub async fn fs_read_file(
|
||||
&self,
|
||||
params: FsReadFileParams,
|
||||
) -> Result<FsReadFileResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_read_file(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(FS_READ_FILE_METHOD, ¶ms, PendingRequest::FsReadFile)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_write_file(
|
||||
&self,
|
||||
params: FsWriteFileParams,
|
||||
) -> Result<FsWriteFileResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_write_file(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(FS_WRITE_FILE_METHOD, ¶ms, PendingRequest::FsWriteFile)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_create_directory(
|
||||
&self,
|
||||
params: FsCreateDirectoryParams,
|
||||
) -> Result<FsCreateDirectoryResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_create_directory(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(
|
||||
FS_CREATE_DIRECTORY_METHOD,
|
||||
¶ms,
|
||||
PendingRequest::FsCreateDirectory,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_get_metadata(
|
||||
&self,
|
||||
params: FsGetMetadataParams,
|
||||
) -> Result<FsGetMetadataResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_get_metadata(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(
|
||||
FS_GET_METADATA_METHOD,
|
||||
¶ms,
|
||||
PendingRequest::FsGetMetadata,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_read_directory(
|
||||
&self,
|
||||
params: FsReadDirectoryParams,
|
||||
) -> Result<FsReadDirectoryResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_read_directory(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(
|
||||
FS_READ_DIRECTORY_METHOD,
|
||||
¶ms,
|
||||
PendingRequest::FsReadDirectory,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_remove(
|
||||
&self,
|
||||
params: FsRemoveParams,
|
||||
) -> Result<FsRemoveResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_remove(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(FS_REMOVE_METHOD, ¶ms, PendingRequest::FsRemove)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn fs_copy(&self, params: FsCopyParams) -> Result<FsCopyResponse, ExecServerError> {
|
||||
if let ClientBackend::InProcess { handler } = &self.inner.backend {
|
||||
return server_result_to_client(handler.lock().await.fs_copy(params).await);
|
||||
}
|
||||
|
||||
self.send_pending_request(FS_COPY_METHOD, ¶ms, PendingRequest::FsCopy)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn initialize(
|
||||
&self,
|
||||
options: ExecServerClientConnectOptions,
|
||||
@@ -505,19 +668,7 @@ impl ExecServerClient {
|
||||
return server_result_to_client(handler.lock().await.exec(params).await);
|
||||
}
|
||||
|
||||
let request_id = self.next_request_id();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.inner
|
||||
.pending
|
||||
.lock()
|
||||
.await
|
||||
.insert(request_id.clone(), PendingRequest::Exec(response_tx));
|
||||
let ClientBackend::JsonRpc { write_tx } = &self.inner.backend else {
|
||||
unreachable!("in-process exec requests return before JSON-RPC setup");
|
||||
};
|
||||
let send_result =
|
||||
send_jsonrpc_request(write_tx, request_id.clone(), EXEC_METHOD, ¶ms).await;
|
||||
self.finish_request(request_id, send_result, response_rx)
|
||||
self.send_pending_request(EXEC_METHOD, ¶ms, PendingRequest::Exec)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -526,19 +677,7 @@ impl ExecServerClient {
|
||||
return server_result_to_client(handler.lock().await.write(params).await);
|
||||
}
|
||||
|
||||
let request_id = self.next_request_id();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.inner
|
||||
.pending
|
||||
.lock()
|
||||
.await
|
||||
.insert(request_id.clone(), PendingRequest::Write(response_tx));
|
||||
let ClientBackend::JsonRpc { write_tx } = &self.inner.backend else {
|
||||
unreachable!("in-process write requests return before JSON-RPC setup");
|
||||
};
|
||||
let send_result =
|
||||
send_jsonrpc_request(write_tx, request_id.clone(), EXEC_WRITE_METHOD, ¶ms).await;
|
||||
self.finish_request(request_id, send_result, response_rx)
|
||||
self.send_pending_request(EXEC_WRITE_METHOD, ¶ms, PendingRequest::Write)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -547,19 +686,7 @@ impl ExecServerClient {
|
||||
return server_result_to_client(handler.lock().await.read(params).await);
|
||||
}
|
||||
|
||||
let request_id = self.next_request_id();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.inner
|
||||
.pending
|
||||
.lock()
|
||||
.await
|
||||
.insert(request_id.clone(), PendingRequest::Read(response_tx));
|
||||
let ClientBackend::JsonRpc { write_tx } = &self.inner.backend else {
|
||||
unreachable!("in-process read requests return before JSON-RPC setup");
|
||||
};
|
||||
let send_result =
|
||||
send_jsonrpc_request(write_tx, request_id.clone(), EXEC_READ_METHOD, ¶ms).await;
|
||||
self.finish_request(request_id, send_result, response_rx)
|
||||
self.send_pending_request(EXEC_READ_METHOD, ¶ms, PendingRequest::Read)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -574,20 +701,7 @@ impl ExecServerClient {
|
||||
return server_result_to_client(handler.lock().await.terminate(params).await);
|
||||
}
|
||||
|
||||
let request_id = self.next_request_id();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.inner
|
||||
.pending
|
||||
.lock()
|
||||
.await
|
||||
.insert(request_id.clone(), PendingRequest::Terminate(response_tx));
|
||||
let ClientBackend::JsonRpc { write_tx } = &self.inner.backend else {
|
||||
unreachable!("in-process terminate requests return before JSON-RPC setup");
|
||||
};
|
||||
let send_result =
|
||||
send_jsonrpc_request(write_tx, request_id.clone(), EXEC_TERMINATE_METHOD, ¶ms)
|
||||
.await;
|
||||
self.finish_request(request_id, send_result, response_rx)
|
||||
self.send_pending_request(EXEC_TERMINATE_METHOD, ¶ms, PendingRequest::Terminate)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -624,26 +738,38 @@ impl ExecServerClient {
|
||||
return server_result_to_client(handler.lock().await.initialize());
|
||||
}
|
||||
|
||||
self.send_pending_request(INITIALIZE_METHOD, ¶ms, PendingRequest::Initialize)
|
||||
.await
|
||||
}
|
||||
|
||||
fn next_request_id(&self) -> RequestId {
|
||||
RequestId::Integer(self.inner.next_request_id.fetch_add(1, Ordering::SeqCst))
|
||||
}
|
||||
|
||||
async fn send_pending_request<P, T>(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &P,
|
||||
build_pending: impl FnOnce(oneshot::Sender<Result<T, JSONRPCErrorError>>) -> PendingRequest,
|
||||
) -> Result<T, ExecServerError>
|
||||
where
|
||||
P: Serialize,
|
||||
{
|
||||
let request_id = self.next_request_id();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.inner
|
||||
.pending
|
||||
.lock()
|
||||
.await
|
||||
.insert(request_id.clone(), PendingRequest::Initialize(response_tx));
|
||||
.insert(request_id.clone(), build_pending(response_tx));
|
||||
let ClientBackend::JsonRpc { write_tx } = &self.inner.backend else {
|
||||
unreachable!("in-process initialize requests return before JSON-RPC setup");
|
||||
unreachable!("in-process requests return before JSON-RPC setup");
|
||||
};
|
||||
let send_result =
|
||||
send_jsonrpc_request(write_tx, request_id.clone(), INITIALIZE_METHOD, ¶ms).await;
|
||||
let send_result = send_jsonrpc_request(write_tx, request_id.clone(), method, params).await;
|
||||
self.finish_request(request_id, send_result, response_rx)
|
||||
.await
|
||||
}
|
||||
|
||||
fn next_request_id(&self) -> RequestId {
|
||||
RequestId::Integer(self.inner.next_request_id.fetch_add(1, Ordering::SeqCst))
|
||||
}
|
||||
|
||||
async fn finish_request<T>(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
|
||||
@@ -175,9 +175,10 @@ async fn connect_in_process_read_returns_retained_output_and_exit_state() {
|
||||
Err(err) => panic!("failed to start in-process child: {err}"),
|
||||
};
|
||||
|
||||
let process_id = response.process_id.clone();
|
||||
let read = match client
|
||||
.read(ReadParams {
|
||||
process_id: response.process_id,
|
||||
process_id: process_id.clone(),
|
||||
after_seq: None,
|
||||
max_bytes: None,
|
||||
wait_ms: Some(1000),
|
||||
@@ -193,6 +194,22 @@ async fn connect_in_process_read_returns_retained_output_and_exit_state() {
|
||||
assert_eq!(read.chunks[0].stream, ExecOutputStream::Stdout);
|
||||
assert_eq!(read.chunks[0].chunk.clone().into_inner(), b"hello".to_vec());
|
||||
assert_eq!(read.next_seq, 2);
|
||||
let read = if read.exited {
|
||||
read
|
||||
} else {
|
||||
match client
|
||||
.read(ReadParams {
|
||||
process_id,
|
||||
after_seq: Some(read.next_seq - 1),
|
||||
max_bytes: None,
|
||||
wait_ms: Some(1000),
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(read) => read,
|
||||
Err(err) => panic!("failed to wait for in-process child exit: {err}"),
|
||||
}
|
||||
};
|
||||
assert!(read.exited);
|
||||
assert_eq!(read.exit_code, Some(0));
|
||||
}
|
||||
|
||||
@@ -13,6 +13,13 @@ pub const EXEC_WRITE_METHOD: &str = "process/write";
|
||||
pub const EXEC_TERMINATE_METHOD: &str = "process/terminate";
|
||||
pub const EXEC_OUTPUT_DELTA_METHOD: &str = "process/output";
|
||||
pub const EXEC_EXITED_METHOD: &str = "process/exited";
|
||||
pub const FS_READ_FILE_METHOD: &str = "fs/readFile";
|
||||
pub const FS_WRITE_FILE_METHOD: &str = "fs/writeFile";
|
||||
pub const FS_CREATE_DIRECTORY_METHOD: &str = "fs/createDirectory";
|
||||
pub const FS_GET_METADATA_METHOD: &str = "fs/getMetadata";
|
||||
pub const FS_READ_DIRECTORY_METHOD: &str = "fs/readDirectory";
|
||||
pub const FS_REMOVE_METHOD: &str = "fs/remove";
|
||||
pub const FS_COPY_METHOD: &str = "fs/copy";
|
||||
pub const PROTOCOL_VERSION: &str = "exec-server.v0";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod filesystem;
|
||||
mod handler;
|
||||
mod processor;
|
||||
mod routing;
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
|
||||
use base64::Engine as _;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCopyResponse;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryEntry;
|
||||
use codex_app_server_protocol::FsReadDirectoryParams;
|
||||
use codex_app_server_protocol::FsReadDirectoryResponse;
|
||||
use codex_app_server_protocol::FsReadFileParams;
|
||||
use codex_app_server_protocol::FsReadFileResponse;
|
||||
use codex_app_server_protocol::FsRemoveParams;
|
||||
use codex_app_server_protocol::FsRemoveResponse;
|
||||
use codex_app_server_protocol::FsWriteFileParams;
|
||||
use codex_app_server_protocol::FsWriteFileResponse;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_environment::CopyOptions;
|
||||
use codex_environment::CreateDirectoryOptions;
|
||||
use codex_environment::Environment;
|
||||
use codex_environment::ExecutorFileSystem;
|
||||
use codex_environment::RemoveOptions;
|
||||
|
||||
use crate::server::routing::internal_error;
|
||||
use crate::server::routing::invalid_request;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ExecServerFileSystem {
|
||||
file_system: Arc<dyn ExecutorFileSystem>,
|
||||
}
|
||||
|
||||
impl Default for ExecServerFileSystem {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
file_system: Environment::default().get_filesystem(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExecServerFileSystem {
|
||||
pub(crate) async fn read_file(
|
||||
&self,
|
||||
params: FsReadFileParams,
|
||||
) -> Result<FsReadFileResponse, JSONRPCErrorError> {
|
||||
let bytes = self
|
||||
.file_system
|
||||
.read_file(¶ms.path)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsReadFileResponse {
|
||||
data_base64: STANDARD.encode(bytes),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn write_file(
|
||||
&self,
|
||||
params: FsWriteFileParams,
|
||||
) -> Result<FsWriteFileResponse, JSONRPCErrorError> {
|
||||
let bytes = STANDARD.decode(params.data_base64).map_err(|err| {
|
||||
invalid_request(format!(
|
||||
"fs/writeFile requires valid base64 dataBase64: {err}"
|
||||
))
|
||||
})?;
|
||||
self.file_system
|
||||
.write_file(¶ms.path, bytes)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsWriteFileResponse {})
|
||||
}
|
||||
|
||||
pub(crate) async fn create_directory(
|
||||
&self,
|
||||
params: FsCreateDirectoryParams,
|
||||
) -> Result<FsCreateDirectoryResponse, JSONRPCErrorError> {
|
||||
self.file_system
|
||||
.create_directory(
|
||||
¶ms.path,
|
||||
CreateDirectoryOptions {
|
||||
recursive: params.recursive.unwrap_or(true),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsCreateDirectoryResponse {})
|
||||
}
|
||||
|
||||
pub(crate) async fn get_metadata(
|
||||
&self,
|
||||
params: FsGetMetadataParams,
|
||||
) -> Result<FsGetMetadataResponse, JSONRPCErrorError> {
|
||||
let metadata = self
|
||||
.file_system
|
||||
.get_metadata(¶ms.path)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsGetMetadataResponse {
|
||||
is_directory: metadata.is_directory,
|
||||
is_file: metadata.is_file,
|
||||
created_at_ms: metadata.created_at_ms,
|
||||
modified_at_ms: metadata.modified_at_ms,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn read_directory(
|
||||
&self,
|
||||
params: FsReadDirectoryParams,
|
||||
) -> Result<FsReadDirectoryResponse, JSONRPCErrorError> {
|
||||
let entries = self
|
||||
.file_system
|
||||
.read_directory(¶ms.path)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsReadDirectoryResponse {
|
||||
entries: entries
|
||||
.into_iter()
|
||||
.map(|entry| FsReadDirectoryEntry {
|
||||
file_name: entry.file_name,
|
||||
is_directory: entry.is_directory,
|
||||
is_file: entry.is_file,
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn remove(
|
||||
&self,
|
||||
params: FsRemoveParams,
|
||||
) -> Result<FsRemoveResponse, JSONRPCErrorError> {
|
||||
self.file_system
|
||||
.remove(
|
||||
¶ms.path,
|
||||
RemoveOptions {
|
||||
recursive: params.recursive.unwrap_or(true),
|
||||
force: params.force.unwrap_or(true),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsRemoveResponse {})
|
||||
}
|
||||
|
||||
pub(crate) async fn copy(
|
||||
&self,
|
||||
params: FsCopyParams,
|
||||
) -> Result<FsCopyResponse, JSONRPCErrorError> {
|
||||
self.file_system
|
||||
.copy(
|
||||
¶ms.source_path,
|
||||
¶ms.destination_path,
|
||||
CopyOptions {
|
||||
recursive: params.recursive,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsCopyResponse {})
|
||||
}
|
||||
}
|
||||
|
||||
fn map_fs_error(err: io::Error) -> JSONRPCErrorError {
|
||||
if err.kind() == io::ErrorKind::InvalidInput {
|
||||
invalid_request(err.to_string())
|
||||
} else {
|
||||
internal_error(err.to_string())
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,20 @@ use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCopyResponse;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryParams;
|
||||
use codex_app_server_protocol::FsReadDirectoryResponse;
|
||||
use codex_app_server_protocol::FsReadFileParams;
|
||||
use codex_app_server_protocol::FsReadFileResponse;
|
||||
use codex_app_server_protocol::FsRemoveParams;
|
||||
use codex_app_server_protocol::FsRemoveResponse;
|
||||
use codex_app_server_protocol::FsWriteFileParams;
|
||||
use codex_app_server_protocol::FsWriteFileResponse;
|
||||
use codex_utils_pty::ExecCommandSession;
|
||||
use codex_utils_pty::TerminalSize;
|
||||
use tokio::sync::Mutex;
|
||||
@@ -21,6 +35,7 @@ use crate::protocol::ProcessOutputChunk;
|
||||
use crate::protocol::ReadResponse;
|
||||
use crate::protocol::TerminateResponse;
|
||||
use crate::protocol::WriteResponse;
|
||||
use crate::server::filesystem::ExecServerFileSystem;
|
||||
use crate::server::routing::ExecServerOutboundMessage;
|
||||
use crate::server::routing::ExecServerServerNotification;
|
||||
use crate::server::routing::internal_error;
|
||||
@@ -48,6 +63,7 @@ struct RunningProcess {
|
||||
|
||||
pub(crate) struct ExecServerHandler {
|
||||
outbound_tx: mpsc::Sender<ExecServerOutboundMessage>,
|
||||
file_system: ExecServerFileSystem,
|
||||
// Keyed by client-chosen logical `processId` scoped to this connection.
|
||||
// This is a protocol handle, not an OS pid.
|
||||
processes: Arc<Mutex<HashMap<String, RunningProcess>>>,
|
||||
@@ -59,6 +75,7 @@ impl ExecServerHandler {
|
||||
pub(crate) fn new(outbound_tx: mpsc::Sender<ExecServerOutboundMessage>) -> Self {
|
||||
Self {
|
||||
outbound_tx,
|
||||
file_system: ExecServerFileSystem::default(),
|
||||
processes: Arc::new(Mutex::new(HashMap::new())),
|
||||
initialize_requested: false,
|
||||
initialized: false,
|
||||
@@ -220,6 +237,62 @@ impl ExecServerHandler {
|
||||
Ok(ExecResponse { process_id })
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_read_file(
|
||||
&self,
|
||||
params: FsReadFileParams,
|
||||
) -> Result<FsReadFileResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.read_file(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_write_file(
|
||||
&self,
|
||||
params: FsWriteFileParams,
|
||||
) -> Result<FsWriteFileResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.write_file(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_create_directory(
|
||||
&self,
|
||||
params: FsCreateDirectoryParams,
|
||||
) -> Result<FsCreateDirectoryResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.create_directory(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_get_metadata(
|
||||
&self,
|
||||
params: FsGetMetadataParams,
|
||||
) -> Result<FsGetMetadataResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.get_metadata(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_read_directory(
|
||||
&self,
|
||||
params: FsReadDirectoryParams,
|
||||
) -> Result<FsReadDirectoryResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.read_directory(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_remove(
|
||||
&self,
|
||||
params: FsRemoveParams,
|
||||
) -> Result<FsRemoveResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.remove(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn fs_copy(
|
||||
&self,
|
||||
params: FsCopyParams,
|
||||
) -> Result<FsCopyResponse, codex_app_server_protocol::JSONRPCErrorError> {
|
||||
self.require_initialized()?;
|
||||
self.file_system.copy(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn read(
|
||||
&self,
|
||||
params: crate::protocol::ReadParams,
|
||||
@@ -389,6 +462,62 @@ impl ExecServerHandler {
|
||||
.map(crate::server::routing::ExecServerResponseMessage::Terminate),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsReadFile { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_read_file(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsReadFile),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsWriteFile { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_write_file(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsWriteFile),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsCreateDirectory { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_create_directory(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsCreateDirectory),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsGetMetadata { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_get_metadata(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsGetMetadata),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsReadDirectory { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_read_directory(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsReadDirectory),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsRemove { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_remove(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsRemove),
|
||||
)
|
||||
}
|
||||
crate::server::routing::ExecServerRequest::FsCopy { request_id, params } => {
|
||||
Self::request_outbound(
|
||||
request_id,
|
||||
self.fs_copy(params)
|
||||
.await
|
||||
.map(crate::server::routing::ExecServerResponseMessage::FsCopy),
|
||||
)
|
||||
}
|
||||
};
|
||||
self.outbound_tx
|
||||
.send(outbound)
|
||||
|
||||
@@ -113,6 +113,55 @@ async fn dispatch_to_handler(
|
||||
.await
|
||||
.map(ExecServerResponseMessage::Terminate),
|
||||
),
|
||||
ExecServerRequest::FsReadFile { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_read_file(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsReadFile),
|
||||
),
|
||||
ExecServerRequest::FsWriteFile { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_write_file(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsWriteFile),
|
||||
),
|
||||
ExecServerRequest::FsCreateDirectory { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_create_directory(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsCreateDirectory),
|
||||
),
|
||||
ExecServerRequest::FsGetMetadata { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_get_metadata(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsGetMetadata),
|
||||
),
|
||||
ExecServerRequest::FsReadDirectory { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_read_directory(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsReadDirectory),
|
||||
),
|
||||
ExecServerRequest::FsRemove { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_remove(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsRemove),
|
||||
),
|
||||
ExecServerRequest::FsCopy { request_id, params } => request_outbound(
|
||||
request_id,
|
||||
handler
|
||||
.fs_copy(params)
|
||||
.await
|
||||
.map(ExecServerResponseMessage::FsCopy),
|
||||
),
|
||||
};
|
||||
outgoing_tx
|
||||
.send(outbound)
|
||||
|
||||
@@ -17,6 +17,13 @@ use crate::protocol::ExecExitedNotification;
|
||||
use crate::protocol::ExecOutputDeltaNotification;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::FS_COPY_METHOD;
|
||||
use crate::protocol::FS_CREATE_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_GET_METADATA_METHOD;
|
||||
use crate::protocol::FS_READ_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_READ_FILE_METHOD;
|
||||
use crate::protocol::FS_REMOVE_METHOD;
|
||||
use crate::protocol::FS_WRITE_FILE_METHOD;
|
||||
use crate::protocol::INITIALIZE_METHOD;
|
||||
use crate::protocol::INITIALIZED_METHOD;
|
||||
use crate::protocol::InitializeParams;
|
||||
@@ -27,6 +34,20 @@ use crate::protocol::TerminateParams;
|
||||
use crate::protocol::TerminateResponse;
|
||||
use crate::protocol::WriteParams;
|
||||
use crate::protocol::WriteResponse;
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCopyResponse;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryParams;
|
||||
use codex_app_server_protocol::FsReadDirectoryResponse;
|
||||
use codex_app_server_protocol::FsReadFileParams;
|
||||
use codex_app_server_protocol::FsReadFileResponse;
|
||||
use codex_app_server_protocol::FsRemoveParams;
|
||||
use codex_app_server_protocol::FsRemoveResponse;
|
||||
use codex_app_server_protocol::FsWriteFileParams;
|
||||
use codex_app_server_protocol::FsWriteFileResponse;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) enum ExecServerInboundMessage {
|
||||
@@ -56,6 +77,34 @@ pub(crate) enum ExecServerRequest {
|
||||
request_id: RequestId,
|
||||
params: TerminateParams,
|
||||
},
|
||||
FsReadFile {
|
||||
request_id: RequestId,
|
||||
params: FsReadFileParams,
|
||||
},
|
||||
FsWriteFile {
|
||||
request_id: RequestId,
|
||||
params: FsWriteFileParams,
|
||||
},
|
||||
FsCreateDirectory {
|
||||
request_id: RequestId,
|
||||
params: FsCreateDirectoryParams,
|
||||
},
|
||||
FsGetMetadata {
|
||||
request_id: RequestId,
|
||||
params: FsGetMetadataParams,
|
||||
},
|
||||
FsReadDirectory {
|
||||
request_id: RequestId,
|
||||
params: FsReadDirectoryParams,
|
||||
},
|
||||
FsRemove {
|
||||
request_id: RequestId,
|
||||
params: FsRemoveParams,
|
||||
},
|
||||
FsCopy {
|
||||
request_id: RequestId,
|
||||
params: FsCopyParams,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -83,6 +132,13 @@ pub(crate) enum ExecServerResponseMessage {
|
||||
Read(ReadResponse),
|
||||
Write(WriteResponse),
|
||||
Terminate(TerminateResponse),
|
||||
FsReadFile(FsReadFileResponse),
|
||||
FsWriteFile(FsWriteFileResponse),
|
||||
FsCreateDirectory(FsCreateDirectoryResponse),
|
||||
FsGetMetadata(FsGetMetadataResponse),
|
||||
FsReadDirectory(FsReadDirectoryResponse),
|
||||
FsRemove(FsRemoveResponse),
|
||||
FsCopy(FsCopyResponse),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -178,6 +234,27 @@ fn route_request(request: JSONRPCRequest) -> Result<RoutedExecServerMessage, Str
|
||||
EXEC_TERMINATE_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::Terminate { request_id, params }
|
||||
})),
|
||||
FS_READ_FILE_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsReadFile { request_id, params }
|
||||
})),
|
||||
FS_WRITE_FILE_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsWriteFile { request_id, params }
|
||||
})),
|
||||
FS_CREATE_DIRECTORY_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsCreateDirectory { request_id, params }
|
||||
})),
|
||||
FS_GET_METADATA_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsGetMetadata { request_id, params }
|
||||
})),
|
||||
FS_READ_DIRECTORY_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsReadDirectory { request_id, params }
|
||||
})),
|
||||
FS_REMOVE_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsRemove { request_id, params }
|
||||
})),
|
||||
FS_COPY_METHOD => Ok(parse_request_params(request, |request_id, params| {
|
||||
ExecServerRequest::FsCopy { request_id, params }
|
||||
})),
|
||||
other => Ok(RoutedExecServerMessage::ImmediateOutbound(
|
||||
ExecServerOutboundMessage::Error {
|
||||
request_id: request.id,
|
||||
@@ -224,6 +301,13 @@ fn serialize_response(
|
||||
ExecServerResponseMessage::Read(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::Write(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::Terminate(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsReadFile(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsWriteFile(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsCreateDirectory(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsGetMetadata(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsReadDirectory(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsRemove(response) => serde_json::to_value(response),
|
||||
ExecServerResponseMessage::FsCopy(response) => serde_json::to_value(response),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,125 @@ async fn exec_server_accepts_initialize_over_stdio() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn exec_server_accepts_explicit_none_sandbox_over_stdio() -> anyhow::Result<()> {
|
||||
let binary = cargo_bin("codex-exec-server")?;
|
||||
let mut child = Command::new(binary);
|
||||
child.stdin(Stdio::piped());
|
||||
child.stdout(Stdio::piped());
|
||||
child.stderr(Stdio::inherit());
|
||||
let mut child = child.spawn()?;
|
||||
|
||||
let mut stdin = child.stdin.take().expect("stdin");
|
||||
let stdout = child.stdout.take().expect("stdout");
|
||||
let mut stdout = BufReader::new(stdout).lines();
|
||||
|
||||
send_initialize_over_stdio(&mut stdin, &mut stdout).await?;
|
||||
|
||||
let exec = JSONRPCMessage::Request(JSONRPCRequest {
|
||||
id: RequestId::Integer(2),
|
||||
method: "process/start".to_string(),
|
||||
params: Some(serde_json::json!({
|
||||
"processId": "proc-1",
|
||||
"argv": ["printf", "sandbox-none"],
|
||||
"cwd": std::env::current_dir()?,
|
||||
"env": {},
|
||||
"tty": false,
|
||||
"arg0": null,
|
||||
"sandbox": {
|
||||
"mode": "none"
|
||||
}
|
||||
})),
|
||||
trace: None,
|
||||
});
|
||||
stdin
|
||||
.write_all(format!("{}\n", serde_json::to_string(&exec)?).as_bytes())
|
||||
.await?;
|
||||
|
||||
let response_line = timeout(Duration::from_secs(5), stdout.next_line()).await??;
|
||||
let response_line = response_line.expect("exec response line");
|
||||
let response: JSONRPCMessage = serde_json::from_str(&response_line)?;
|
||||
let JSONRPCMessage::Response(JSONRPCResponse { id, result }) = response else {
|
||||
panic!("expected process/start response");
|
||||
};
|
||||
assert_eq!(id, RequestId::Integer(2));
|
||||
assert_eq!(result, serde_json::json!({ "processId": "proc-1" }));
|
||||
|
||||
let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
|
||||
let mut saw_output = false;
|
||||
while !saw_output {
|
||||
let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
|
||||
let line = timeout(remaining, stdout.next_line()).await??;
|
||||
let line = line.context("missing process notification")?;
|
||||
let message: JSONRPCMessage = serde_json::from_str(&line)?;
|
||||
if let JSONRPCMessage::Notification(JSONRPCNotification { method, params }) = message
|
||||
&& method == "process/output"
|
||||
{
|
||||
let params = params.context("missing process/output params")?;
|
||||
assert_eq!(params["processId"], "proc-1");
|
||||
assert_eq!(params["stream"], "stdout");
|
||||
assert_eq!(params["chunk"], "c2FuZGJveC1ub25l");
|
||||
saw_output = true;
|
||||
}
|
||||
}
|
||||
|
||||
child.start_kill()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn exec_server_rejects_host_default_sandbox_over_stdio() -> anyhow::Result<()> {
|
||||
let binary = cargo_bin("codex-exec-server")?;
|
||||
let mut child = Command::new(binary);
|
||||
child.stdin(Stdio::piped());
|
||||
child.stdout(Stdio::piped());
|
||||
child.stderr(Stdio::inherit());
|
||||
let mut child = child.spawn()?;
|
||||
|
||||
let mut stdin = child.stdin.take().expect("stdin");
|
||||
let stdout = child.stdout.take().expect("stdout");
|
||||
let mut stdout = BufReader::new(stdout).lines();
|
||||
|
||||
send_initialize_over_stdio(&mut stdin, &mut stdout).await?;
|
||||
|
||||
let exec = JSONRPCMessage::Request(JSONRPCRequest {
|
||||
id: RequestId::Integer(2),
|
||||
method: "process/start".to_string(),
|
||||
params: Some(serde_json::json!({
|
||||
"processId": "proc-1",
|
||||
"argv": ["bash", "-lc", "true"],
|
||||
"cwd": std::env::current_dir()?,
|
||||
"env": {},
|
||||
"tty": false,
|
||||
"arg0": null,
|
||||
"sandbox": {
|
||||
"mode": "hostDefault"
|
||||
}
|
||||
})),
|
||||
trace: None,
|
||||
});
|
||||
stdin
|
||||
.write_all(format!("{}\n", serde_json::to_string(&exec)?).as_bytes())
|
||||
.await?;
|
||||
|
||||
let response_line = timeout(Duration::from_secs(5), stdout.next_line()).await??;
|
||||
let response_line = response_line.expect("exec error line");
|
||||
let response: JSONRPCMessage = serde_json::from_str(&response_line)?;
|
||||
let JSONRPCMessage::Error(codex_app_server_protocol::JSONRPCError { id, error }) = response
|
||||
else {
|
||||
panic!("expected process/start error");
|
||||
};
|
||||
assert_eq!(id, RequestId::Integer(2));
|
||||
assert_eq!(error.code, -32600);
|
||||
assert_eq!(
|
||||
error.message,
|
||||
"sandbox mode `hostDefault` is not supported by exec-server yet"
|
||||
);
|
||||
|
||||
child.start_kill()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn exec_server_client_streams_output_and_accepts_writes() -> anyhow::Result<()> {
|
||||
let mut env = std::collections::HashMap::new();
|
||||
@@ -279,6 +398,48 @@ where
|
||||
Ok(websocket_url.to_string())
|
||||
}
|
||||
|
||||
async fn send_initialize_over_stdio<W, R>(
|
||||
stdin: &mut W,
|
||||
stdout: &mut tokio::io::Lines<BufReader<R>>,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
W: tokio::io::AsyncWrite + Unpin,
|
||||
R: tokio::io::AsyncRead + Unpin,
|
||||
{
|
||||
let initialize = JSONRPCMessage::Request(JSONRPCRequest {
|
||||
id: RequestId::Integer(1),
|
||||
method: "initialize".to_string(),
|
||||
params: Some(serde_json::to_value(InitializeParams {
|
||||
client_name: "exec-server-test".to_string(),
|
||||
})?),
|
||||
trace: None,
|
||||
});
|
||||
stdin
|
||||
.write_all(format!("{}\n", serde_json::to_string(&initialize)?).as_bytes())
|
||||
.await?;
|
||||
|
||||
let response_line = timeout(Duration::from_secs(5), stdout.next_line()).await??;
|
||||
let response_line = response_line
|
||||
.ok_or_else(|| anyhow::anyhow!("missing initialize response line from stdio server"))?;
|
||||
let response: JSONRPCMessage = serde_json::from_str(&response_line)?;
|
||||
let JSONRPCMessage::Response(JSONRPCResponse { id, result }) = response else {
|
||||
panic!("expected initialize response");
|
||||
};
|
||||
assert_eq!(id, RequestId::Integer(1));
|
||||
let initialize_response: InitializeResponse = serde_json::from_value(result)?;
|
||||
assert_eq!(initialize_response.protocol_version, "exec-server.v0");
|
||||
|
||||
let initialized = JSONRPCMessage::Notification(JSONRPCNotification {
|
||||
method: "initialized".to_string(),
|
||||
params: Some(serde_json::json!({})),
|
||||
});
|
||||
stdin
|
||||
.write_all(format!("{}\n", serde_json::to_string(&initialized)?).as_bytes())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn recv_until_contains(
|
||||
events: &mut broadcast::Receiver<ExecServerEvent>,
|
||||
process_id: &str,
|
||||
|
||||
Reference in New Issue
Block a user