mirror of
https://github.com/openai/codex.git
synced 2026-12-03 07:47:10 +00:00
Preserve tracing context for environment resolution (#39078)
## What changed - Add spans for environment resolution, readiness waits, and environment info, including fields for the selected environment and resolution state. - Propagate the active span and tracing subscriber into spawned resolution, startup, and shell-snapshot tasks. - Verify that blocking snapshots retain the expected nested environment spans. GitOrigin-RevId: 3c47e9ec1ebf45dbe1250b9ff76e2983d2a8df2a
This commit is contained in:
1 parent
e92627bf7e
commit
911335eed3
2 files changed
+96
-23
No files matched your search
@@ -27,6 +27,8 @@ use futures::future::BoxFuture;
|
|||||||
use futures::future::Shared;
|
use futures::future::Shared;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio_util::task::AbortOnDropHandle;
|
use tokio_util::task::AbortOnDropHandle;
|
||||||
|
use tracing::Instrument;
|
||||||
|
use tracing::instrument::WithSubscriber;
|
||||||
|
|
||||||
use crate::session::turn_context::ShellSnapshotTask;
|
use crate::session::turn_context::ShellSnapshotTask;
|
||||||
use crate::session::turn_context::TurnEnvironment;
|
use crate::session::turn_context::TurnEnvironment;
|
||||||
@@ -139,6 +141,11 @@ impl fmt::Debug for StartingTurnEnvironment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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>> {
|
pub(crate) async fn wait_until_ready(&self) -> Result<(), Arc<ExecServerError>> {
|
||||||
self.resolution.clone().await.map(|_| ())
|
self.resolution.clone().await.map(|_| ())
|
||||||
}
|
}
|
||||||
@@ -283,7 +290,9 @@ impl ThreadEnvironments {
|
|||||||
configuration_ready,
|
configuration_ready,
|
||||||
)
|
)
|
||||||
.remote_handle();
|
.remote_handle();
|
||||||
drop(tokio::spawn(resolution_task));
|
drop(tokio::spawn(
|
||||||
|
resolution_task.in_current_span().with_current_subscriber(),
|
||||||
|
));
|
||||||
let resolution = resolution.boxed().shared();
|
let resolution = resolution.boxed().shared();
|
||||||
let selected = SelectedTurnEnvironment {
|
let selected = SelectedTurnEnvironment {
|
||||||
selection: selected_environment,
|
selection: selected_environment,
|
||||||
@@ -477,14 +486,22 @@ impl ThreadEnvironments {
|
|||||||
self.environments.store(Arc::new(environments));
|
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,
|
selection: TurnEnvironmentSelection,
|
||||||
environment: Arc<Environment>,
|
environment: Arc<Environment>,
|
||||||
local_shell: Shell,
|
local_shell: Shell,
|
||||||
shell_snapshot: ShellSnapshot,
|
shell_snapshot: ShellSnapshot,
|
||||||
configuration_ready: Option<mpsc::Receiver<PendingConfigurationResult>>,
|
configuration_ready: Option<mpsc::Receiver<PendingConfigurationResult>>,
|
||||||
) -> BoxFuture<'static, TurnEnvironmentResult> {
|
) -> TurnEnvironmentResult {
|
||||||
async move {
|
|
||||||
let environment_id = &selection.environment_id;
|
let environment_id = &selection.environment_id;
|
||||||
if let EnvironmentConfigState::Failed(error) = &selection.config {
|
if let EnvironmentConfigState::Failed(error) = &selection.config {
|
||||||
return Err(Arc::new(ExecServerError::Protocol(error.clone())));
|
return Err(Arc::new(ExecServerError::Protocol(error.clone())));
|
||||||
@@ -524,9 +541,7 @@ impl ThreadEnvironments {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
tracing::warn!(
|
tracing::warn!("failed to get info for environment `{environment_id}`: {err}");
|
||||||
"failed to get info for environment `{environment_id}`: {err}"
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -537,7 +552,9 @@ impl ThreadEnvironments {
|
|||||||
.build(Arc::clone(&environment), selection.cwd, shell.clone())
|
.build(Arc::clone(&environment), selection.cwd, shell.clone())
|
||||||
.boxed()
|
.boxed()
|
||||||
.shared();
|
.shared();
|
||||||
drop(tokio::spawn(task.clone()));
|
drop(tokio::spawn(
|
||||||
|
task.clone().in_current_span().with_current_subscriber(),
|
||||||
|
));
|
||||||
Ok(ResolvedEnvironment {
|
Ok(ResolvedEnvironment {
|
||||||
environment,
|
environment,
|
||||||
shell,
|
shell,
|
||||||
@@ -545,10 +562,15 @@ impl ThreadEnvironments {
|
|||||||
installed_config,
|
installed_config,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
.boxed()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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 {
|
pub(crate) async fn snapshot(&self) -> TurnEnvironmentSnapshot {
|
||||||
let selected = self.environments.load_full();
|
let selected = self.environments.load_full();
|
||||||
let mut environments = Vec::with_capacity(selected.len());
|
let mut environments = Vec::with_capacity(selected.len());
|
||||||
@@ -563,19 +585,20 @@ impl ThreadEnvironments {
|
|||||||
environment.selection.config,
|
environment.selection.config,
|
||||||
EnvironmentConfigState::Pending
|
EnvironmentConfigState::Pending
|
||||||
);
|
);
|
||||||
let resolved = if self.non_blocking_snapshots || pending {
|
let starting = StartingTurnEnvironment {
|
||||||
environment.resolution.clone().now_or_never()
|
|
||||||
} else {
|
|
||||||
Some(environment.resolution.clone().await)
|
|
||||||
};
|
|
||||||
if let Some(environment) = TurnEnvironmentState::from_resolution(
|
|
||||||
StartingTurnEnvironment {
|
|
||||||
selection: environment.selection.clone(),
|
selection: environment.selection.clone(),
|
||||||
config_origin: environment.config_origin,
|
config_origin: environment.config_origin,
|
||||||
resolution: environment.resolution.clone(),
|
resolution: environment.resolution.clone(),
|
||||||
},
|
};
|
||||||
resolved,
|
let resolved = if self.non_blocking_snapshots || pending {
|
||||||
) {
|
starting.resolution.clone().now_or_never()
|
||||||
|
} else {
|
||||||
|
Some(match starting.wait_until_ready().await {
|
||||||
|
Ok(()) => starting.resolution.clone().await,
|
||||||
|
Err(error) => Err(error),
|
||||||
|
})
|
||||||
|
};
|
||||||
|
if let Some(environment) = TurnEnvironmentState::from_resolution(starting, resolved) {
|
||||||
environments.push(environment);
|
environments.push(environment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -779,6 +802,9 @@ mod tests {
|
|||||||
use tokio_tungstenite::WebSocketStream;
|
use tokio_tungstenite::WebSocketStream;
|
||||||
use tokio_tungstenite::accept_async;
|
use tokio_tungstenite::accept_async;
|
||||||
use tokio_tungstenite::tungstenite::Message;
|
use tokio_tungstenite::tungstenite::Message;
|
||||||
|
use tracing::Level;
|
||||||
|
use tracing_subscriber::fmt::format::FmtSpan;
|
||||||
|
use tracing_test::internal::MockWriter;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
@@ -1094,6 +1120,16 @@ url = "ws://127.0.0.1:8765"
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn blocking_snapshot_waits_for_starting_environment() {
|
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")
|
let listener = TcpListener::bind("127.0.0.1:0")
|
||||||
.await
|
.await
|
||||||
.expect("bind websocket listener");
|
.expect("bind websocket listener");
|
||||||
@@ -1139,6 +1175,27 @@ url = "ws://127.0.0.1:8765"
|
|||||||
assert!(snapshot.starting().next().is_none());
|
assert!(snapshot.starting().next().is_none());
|
||||||
assert_eq!(snapshot.to_selections(), vec![selection]);
|
assert_eq!(snapshot.to_selections(), vec![selection]);
|
||||||
server.await.expect("server task");
|
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]
|
#[tokio::test]
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ use crate::remote_process::RemoteProcess;
|
|||||||
use codex_utils_path_uri::PathUri;
|
use codex_utils_path_uri::PathUri;
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
use tokio_util::task::AbortOnDropHandle;
|
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_URL_ENV_VAR: &str = "CODEX_EXEC_SERVER_URL";
|
||||||
pub const CODEX_EXEC_SERVER_NOISE_REGISTRY_URL_ENV_VAR: &str =
|
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.
|
/// 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> {
|
pub async fn info(&self) -> Result<EnvironmentInfo, ExecServerError> {
|
||||||
match &self.remote_client {
|
match &self.remote_client {
|
||||||
Some(client) => client.environment_info().await,
|
Some(client) => client.environment_info().await,
|
||||||
@@ -1001,11 +1008,15 @@ impl Environment {
|
|||||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||||
if startup_task.is_none() {
|
if startup_task.is_none() {
|
||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
*startup_task = Some(AbortOnDropHandle::new(tokio::spawn(async move {
|
*startup_task = Some(AbortOnDropHandle::new(tokio::spawn(
|
||||||
|
async move {
|
||||||
if let Err(error) = client.wait_until_ready().await {
|
if let Err(error) = client.wait_until_ready().await {
|
||||||
tracing::debug!(%error, "exec-server environment startup failed");
|
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.
|
/// 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> {
|
pub async fn wait_until_ready(&self) -> Result<(), ExecServerError> {
|
||||||
match &self.remote_client {
|
match &self.remote_client {
|
||||||
Some(client) => client.wait_until_ready().await,
|
Some(client) => client.wait_until_ready().await,
|
||||||
|
|||||||
Reference in new issue
Block a user