From fc135afd78a46cd158cc5d38ef6e34b67a4d6d6a Mon Sep 17 00:00:00 2001 From: starr-openai Date: Fri, 20 Mar 2026 17:24:49 -0700 Subject: [PATCH] Thread environment ids through app-server routing slice Derive stable environment bindings for standalone fs and command/exec paths while keeping the local executor path intact for the default environment. Co-authored-by: Codex --- codex-rs/app-server/src/command_exec.rs | 100 ++++++++++++++----- codex-rs/app-server/src/fs_api.rs | 44 ++++++++ codex-rs/app-server/src/message_processor.rs | 5 +- 3 files changed, 123 insertions(+), 26 deletions(-) diff --git a/codex-rs/app-server/src/command_exec.rs b/codex-rs/app-server/src/command_exec.rs index f761b18c96..bc7b9f0e97 100644 --- a/codex-rs/app-server/src/command_exec.rs +++ b/codex-rs/app-server/src/command_exec.rs @@ -67,9 +67,12 @@ struct ConnectionProcessId { #[derive(Clone)] enum CommandExecSession { Active { + environment_id: String, control_tx: mpsc::Sender, }, - UnsupportedWindowsSandbox, + UnsupportedWindowsSandbox { + environment_id: String, + }, } enum CommandControl { @@ -155,6 +158,7 @@ impl CommandExecManager { output_bytes_cap, size, } = params; + let environment_id = environment_id_from_cwd(exec_request.cwd.as_path()); if process_id.is_none() && (tty || stream_stdin || stream_stdout_stderr) { return Err(invalid_request( "command/exec tty or streaming requires a client-supplied processId".to_string(), @@ -195,7 +199,9 @@ impl CommandExecManager { } sessions.insert( process_key.clone(), - CommandExecSession::UnsupportedWindowsSandbox, + CommandExecSession::UnsupportedWindowsSandbox { + environment_id: environment_id.clone(), + }, ); } let sessions = Arc::clone(&self.sessions); @@ -259,9 +265,17 @@ impl CommandExecManager { } sessions.insert( process_key.clone(), - CommandExecSession::Active { control_tx }, + CommandExecSession::Active { + environment_id: environment_id.clone(), + control_tx, + }, ); } + tracing::debug!( + environment_id = %environment_id, + process_id = %process_key.process_id.error_repr(), + "command/exec start" + ); let spawned = if tty { codex_utils_pty::spawn_pty_process( program, @@ -389,13 +403,28 @@ impl CommandExecManager { }; for control in controls { - if let CommandExecSession::Active { control_tx } = control { - let _ = control_tx - .send(CommandControlRequest { - control: CommandControl::Terminate, - response_tx: None, - }) - .await; + match control { + CommandExecSession::Active { + environment_id, + control_tx, + } => { + tracing::debug!( + environment_id = %environment_id, + "command/exec connection closed" + ); + let _ = control_tx + .send(CommandControlRequest { + control: CommandControl::Terminate, + response_tx: None, + }) + .await; + } + CommandExecSession::UnsupportedWindowsSandbox { environment_id } => { + tracing::debug!( + environment_id = %environment_id, + "command/exec connection closed for windows sandbox" + ); + } } } } @@ -418,10 +447,26 @@ impl CommandExecManager { )) })? }; - let CommandExecSession::Active { control_tx } = session else { - return Err(invalid_request( - "command/exec/write, command/exec/terminate, and command/exec/resize are not supported for windows sandbox processes".to_string(), - )); + let control_tx = match session { + CommandExecSession::Active { + environment_id, + control_tx, + } => { + tracing::debug!( + environment_id = %environment_id, + "command/exec control" + ); + control_tx + } + CommandExecSession::UnsupportedWindowsSandbox { environment_id } => { + tracing::debug!( + environment_id = %environment_id, + "command/exec control rejected for windows sandbox" + ); + return Err(invalid_request( + "command/exec/write, command/exec/terminate, and command/exec/resize are not supported for windows sandbox processes".to_string(), + )); + } }; let (response_tx, response_rx) = oneshot::channel(); let request = CommandControlRequest { @@ -917,11 +962,12 @@ mod tests { connection_id: request_id.connection_id, process_id: InternalProcessId::Client("proc-11".to_string()), }; - manager - .sessions - .lock() - .await - .insert(process_id, CommandExecSession::UnsupportedWindowsSandbox); + manager.sessions.lock().await.insert( + process_id, + CommandExecSession::UnsupportedWindowsSandbox { + environment_id: "test-env".to_string(), + }, + ); let err = manager .write( @@ -953,11 +999,12 @@ mod tests { connection_id: request_id.connection_id, process_id: InternalProcessId::Client("proc-12".to_string()), }; - manager - .sessions - .lock() - .await - .insert(process_id, CommandExecSession::UnsupportedWindowsSandbox); + manager.sessions.lock().await.insert( + process_id, + CommandExecSession::UnsupportedWindowsSandbox { + environment_id: "test-env".to_string(), + }, + ); let err = manager .terminate( @@ -990,7 +1037,10 @@ mod tests { connection_id: request_id.connection_id, process_id: process_id.clone(), }, - CommandExecSession::Active { control_tx }, + CommandExecSession::Active { + environment_id: "test-env".to_string(), + control_tx, + }, ); tokio::spawn(async move { diff --git a/codex-rs/app-server/src/fs_api.rs b/codex-rs/app-server/src/fs_api.rs index 9baa2b1dce..d65b5a1597 100644 --- a/codex-rs/app-server/src/fs_api.rs +++ b/codex-rs/app-server/src/fs_api.rs @@ -28,12 +28,20 @@ use std::sync::Arc; #[derive(Clone)] pub(crate) struct FsApi { + environment_id: String, file_system: Arc, } impl Default for FsApi { fn default() -> Self { + Self::new(Environment::default_environment_id(None)) + } +} + +impl FsApi { + pub(crate) fn new(environment_id: String) -> Self { Self { + environment_id, file_system: Arc::new(Environment::default().get_filesystem()), } } @@ -44,6 +52,11 @@ impl FsApi { &self, params: FsReadFileParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/readFile" + ); let bytes = self .file_system .read_file(¶ms.path) @@ -58,6 +71,11 @@ impl FsApi { &self, params: FsWriteFileParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/writeFile" + ); let bytes = STANDARD.decode(params.data_base64).map_err(|err| { invalid_request(format!( "fs/writeFile requires valid base64 dataBase64: {err}" @@ -74,6 +92,11 @@ impl FsApi { &self, params: FsCreateDirectoryParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/createDirectory" + ); self.file_system .create_directory( ¶ms.path, @@ -90,6 +113,11 @@ impl FsApi { &self, params: FsGetMetadataParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/getMetadata" + ); let metadata = self .file_system .get_metadata(¶ms.path) @@ -107,6 +135,11 @@ impl FsApi { &self, params: FsReadDirectoryParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/readDirectory" + ); let entries = self .file_system .read_directory(¶ms.path) @@ -128,6 +161,11 @@ impl FsApi { &self, params: FsRemoveParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + path = %params.path, + "fs/remove" + ); self.file_system .remove( ¶ms.path, @@ -145,6 +183,12 @@ impl FsApi { &self, params: FsCopyParams, ) -> Result { + tracing::debug!( + environment_id = %self.environment_id, + source_path = %params.source_path, + destination_path = %params.destination_path, + "fs/copy" + ); self.file_system .copy( ¶ms.source_path, diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 3804c4f9b4..f607feb2ef 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -64,6 +64,7 @@ use codex_core::default_client::set_default_client_residency_requirement; use codex_core::default_client::set_default_originator; use codex_core::models_manager::collaboration_mode_presets::CollaborationModesConfig; use codex_feedback::CodexFeedback; +use codex_exec_server::Environment; use codex_protocol::ThreadId; use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::W3cTraceContext; @@ -255,7 +256,9 @@ impl MessageProcessor { analytics_events_client, ); let external_agent_config_api = ExternalAgentConfigApi::new(config.codex_home.clone()); - let fs_api = FsApi::default(); + let fs_api = FsApi::new(Environment::default_environment_id( + config.experimental_exec_server_url.as_deref(), + )); Self { outgoing,