Lightweight coding agent that runs in your terminal
Rust
Preserve tracing context for environment resolution (#39078)
Ben Romano911335eed37a
diff --git a/codex-rs/core/src/environment_selection.rs b/codex-rs/core/src/environment_selection.rsindex 7de8bff965..cdaa5e6840 100644--- a/codex-rs/core/src/environment_selection.rs+++ b/codex-rs/core/src/environment_selection.rs@@ -27,6 +27,8 @@ use futures::future::BoxFuture;use futures::future::Shared;use tokio::sync::mpsc;use tokio_util::task::AbortOnDropHandle;+use tracing::Instrument;+use tracing::instrument::WithSubscriber;use crate::session::turn_context::ShellSnapshotTask;use crate::session::turn_context::TurnEnvironment;@@ -139,6 +141,11 @@ impl fmt::Debug for StartingTurnEnvironment {}impl StartingTurnEnvironment {+ #[tracing::instrument(+ name = "environments.wait_until_ready",+ skip_all,+ fields(environment_id = %self.selection.environment_id)+ )]pub(crate) async fn wait_until_ready(&self) -> Result<(), Arc<ExecServerError>> {self.resolution.clone().await.map(|_| ())}@@ -283,7 +290,9 @@ impl ThreadEnvironments {configuration_ready,).remote_handle();- drop(tokio::spawn(resolution_task));+ drop(tokio::spawn(+ resolution_task.in_current_span().with_current_subscriber(),+ ));let resolution = resolution.boxed().shared();let selected = SelectedTurnEnvironment {selection: selected_environment,@@ -477,78 +486,91 @@ impl ThreadEnvironments {self.environments.store(Arc::new(environments));}- fn resolve_environment(+ #[tracing::instrument(+ name = "environments.resolve",+ skip_all,+ fields(+ environment_id = %selection.environment_id,+ remote = environment.is_remote(),+ configuration_pending = configuration_ready.is_some(),+ )+ )]+ async fn resolve_environment(selection: TurnEnvironmentSelection,environment: Arc<Environment>,local_shell: Shell,shell_snapshot: ShellSnapshot,configuration_ready: Option<mpsc::Receiver<PendingConfigurationResult>>,- ) -> BoxFuture<'static, TurnEnvironmentResult> {- async move {- let environment_id = &selection.environment_id;- if let EnvironmentConfigState::Failed(error) = &selection.config {- return Err(Arc::new(ExecServerError::Protocol(error.clone())));- }+ ) -> TurnEnvironmentResult {+ let environment_id = &selection.environment_id;+ if let EnvironmentConfigState::Failed(error) = &selection.config {+ return Err(Arc::new(ExecServerError::Protocol(error.clone())));+ }- // Wait for the shared executor connection.- let connection_ready = async {- environment.wait_until_ready().await.map_err(|error| {- tracing::warn!("turn environment `{environment_id}` failed to start: {error}");- Arc::new(error)- })- };- // Wait for this thread attachment's owner configuration, if pending.- let configuration_ready = async move {- let Some(mut configuration_ready) = configuration_ready else {- return Ok(None);- };- match configuration_ready.recv().await {- Some(Ok(config)) => Ok(Some(config)),- Some(Err(error)) => Err(Arc::new(ExecServerError::Protocol(error))),- None => Err(Arc::new(ExecServerError::Protocol(- "environment configuration was canceled".to_string(),- ))),- }+ // Wait for the shared executor connection.+ let connection_ready = async {+ environment.wait_until_ready().await.map_err(|error| {+ tracing::warn!("turn environment `{environment_id}` failed to start: {error}");+ Arc::new(error)+ })+ };+ // Wait for this thread attachment's owner configuration, if pending.+ let configuration_ready = async move {+ let Some(mut configuration_ready) = configuration_ready else {+ return Ok(None);};- // Resolve the attachment only after both prerequisites are ready.- let ((), installed_config) = tokio::try_join!(connection_ready, configuration_ready)?;- let shell = if environment.is_remote() {- match environment.info().await {- Ok(info) => match Shell::from_environment_shell_info(info.shell) {- Ok(shell) => Some(shell),- Err(err) => {- tracing::warn!(- "failed to resolve shell for environment `{environment_id}`: {err}"- );- None- }- },+ match configuration_ready.recv().await {+ Some(Ok(config)) => Ok(Some(config)),+ Some(Err(error)) => Err(Arc::new(ExecServerError::Protocol(error))),+ None => Err(Arc::new(ExecServerError::Protocol(+ "environment configuration was canceled".to_string(),+ ))),+ }+ };+ // Resolve the attachment only after both prerequisites are ready.+ let ((), installed_config) = tokio::try_join!(connection_ready, configuration_ready)?;+ let shell = if environment.is_remote() {+ match environment.info().await {+ Ok(info) => match Shell::from_environment_shell_info(info.shell) {+ Ok(shell) => Some(shell),Err(err) => {tracing::warn!(- "failed to get info for environment `{environment_id}`: {err}"+ "failed to resolve shell for environment `{environment_id}`: {err}");None}+ },+ Err(err) => {+ tracing::warn!("failed to get info for environment `{environment_id}`: {err}");+ None}- } else {- Some(local_shell)- };- let task = shell_snapshot- .build(Arc::clone(&environment), selection.cwd, shell.clone())- .boxed()- .shared();- drop(tokio::spawn(task.clone()));- Ok(ResolvedEnvironment {- environment,- shell,- shell_snapshot: task,- installed_config,- })- }- .boxed()+ }+ } else {+ Some(local_shell)+ };+ let task = shell_snapshot+ .build(Arc::clone(&environment), selection.cwd, shell.clone())+ .boxed()+ .shared();+ drop(tokio::spawn(+ task.clone().in_current_span().with_current_subscriber(),+ ));+ Ok(ResolvedEnvironment {+ environment,+ shell,+ shell_snapshot: task,+ installed_config,+ })}- #[tracing::instrument(name = "environments.snapshot", skip_all)]+ #[tracing::instrument(+ name = "environments.snapshot",+ skip_all,+ fields(+ environment_count = self.environments.load().len(),+ non_blocking = self.non_blocking_snapshots,+ )+ )]pub(crate) async fn snapshot(&self) -> TurnEnvironmentSnapshot {let selected = self.environments.load_full();let mut environments = Vec::with_capacity(selected.len());@@ -563,19 +585,20 @@ impl ThreadEnvironments {environment.selection.config,EnvironmentConfigState::Pending);+ let starting = StartingTurnEnvironment {+ selection: environment.selection.clone(),+ config_origin: environment.config_origin,+ resolution: environment.resolution.clone(),+ };let resolved = if self.non_blocking_snapshots || pending {- environment.resolution.clone().now_or_never()+ starting.resolution.clone().now_or_never()} else {- Some(environment.resolution.clone().await)+ Some(match starting.wait_until_ready().await {+ Ok(()) => starting.resolution.clone().await,+ Err(error) => Err(error),+ })};- if let Some(environment) = TurnEnvironmentState::from_resolution(- StartingTurnEnvironment {- selection: environment.selection.clone(),- config_origin: environment.config_origin,- resolution: environment.resolution.clone(),- },- resolved,- ) {+ if let Some(environment) = TurnEnvironmentState::from_resolution(starting, resolved) {environments.push(environment);}}@@ -779,6 +802,9 @@ mod tests {use tokio_tungstenite::WebSocketStream;use tokio_tungstenite::accept_async;use tokio_tungstenite::tungstenite::Message;+ use tracing::Level;+ use tracing_subscriber::fmt::format::FmtSpan;+ use tracing_test::internal::MockWriter;use super::*;@@ -1094,6 +1120,16 @@ url = "ws://127.0.0.1:8765"#[tokio::test]async fn blocking_snapshot_waits_for_starting_environment() {+ let buffer: &'static std::sync::Mutex<Vec<u8>> =+ Box::leak(Box::new(std::sync::Mutex::new(Vec::new())));+ let subscriber = tracing_subscriber::fmt()+ .with_ansi(false)+ .with_max_level(Level::TRACE)+ .with_span_events(FmtSpan::NEW)+ .with_writer(MockWriter::new(buffer))+ .finish();+ let _subscriber_guard = tracing::subscriber::set_default(subscriber);+let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind websocket listener");@@ -1139,6 +1175,27 @@ url = "ws://127.0.0.1:8765"assert!(snapshot.starting().next().is_none());assert_eq!(snapshot.to_selections(), vec![selection]);server.await.expect("server task");++ let logs = String::from_utf8(buffer.lock().expect("buffer lock").clone())+ .expect("UTF-8 tracing output");+ assert!(+ logs.contains(+ "environments.snapshot{environment_count=1 non_blocking=false}:environments.wait_until_ready{environment_id=remote}"+ ),+ "blocking snapshot should contain its environment wait span: {logs}"+ );+ assert!(+ logs.contains(+ "environments.resolve{environment_id=remote remote=true configuration_pending=false}:exec_server.environment.wait_until_ready{remote=true}"+ ),+ "environment resolution should contain the executor connection wait span: {logs}"+ );+ assert!(+ logs.contains(+ "environments.resolve{environment_id=remote remote=true configuration_pending=false}:exec_server.environment.info{remote=true}"+ ),+ "environment resolution should contain the remote environment-info span: {logs}"+ );}#[tokio::test]diff --git a/codex-rs/exec-server/src/environment.rs b/codex-rs/exec-server/src/environment.rsindex 6f7105950f..1bca7c9408 100644--- a/codex-rs/exec-server/src/environment.rs+++ b/codex-rs/exec-server/src/environment.rs@@ -45,6 +45,8 @@ use crate::remote_process::RemoteProcess;use codex_utils_path_uri::PathUri;use tokio::sync::watch;use tokio_util::task::AbortOnDropHandle;+use tracing::Instrument;+use tracing::instrument::WithSubscriber;pub const CODEX_EXEC_SERVER_URL_ENV_VAR: &str = "CODEX_EXEC_SERVER_URL";pub const CODEX_EXEC_SERVER_NOISE_REGISTRY_URL_ENV_VAR: &str =@@ -879,6 +881,11 @@ impl Environment {}/// Returns environment information from the selected execution/filesystem environment.+ #[tracing::instrument(+ name = "exec_server.environment.info",+ skip_all,+ fields(remote = self.is_remote())+ )]pub async fn info(&self) -> Result<EnvironmentInfo, ExecServerError> {match &self.remote_client {Some(client) => client.environment_info().await,@@ -1001,11 +1008,15 @@ impl Environment {.unwrap_or_else(std::sync::PoisonError::into_inner);if startup_task.is_none() {let client = client.clone();- *startup_task = Some(AbortOnDropHandle::new(tokio::spawn(async move {- if let Err(error) = client.wait_until_ready().await {- tracing::debug!(%error, "exec-server environment startup failed");+ *startup_task = Some(AbortOnDropHandle::new(tokio::spawn(+ async move {+ if let Err(error) = client.wait_until_ready().await {+ tracing::debug!(%error, "exec-server environment startup failed");+ }}- })));+ .in_current_span()+ .with_current_subscriber(),+ )));}}@@ -1017,6 +1028,11 @@ impl Environment {}/// Waits for initial startup, retrying a previous transient failure when possible.+ #[tracing::instrument(+ name = "exec_server.environment.wait_until_ready",+ skip_all,+ fields(remote = self.is_remote())+ )]pub async fn wait_until_ready(&self) -> Result<(), ExecServerError> {match &self.remote_client {Some(client) => client.wait_until_ready().await,