Recover deferred environments after provisioning failure (#42388)

## What changed

- Allow a valid Ready report to replace a deferred environment's provisioning failure while preserving the same environment instance.
- Treat provisioning failures as retryable and reconnect when a completed or concurrently publishing startup attempt contains a stale failure.
- Report provisioning failures from `status()` without starting a connection, and retain Ready state when a late failure arrives.

## Testing

- Cover recovery after failed and invalid provisioning reports, capability-root resolution after recovery, and the race between Ready and publication of an earlier startup failure.

GitOrigin-RevId: cce419403248168da81bc5a6ab20f919aed81142
This commit is contained in:
Anton Panasenko authored and copyberry committed 2026-09-02 23:49:29 +00:00
1 parent 2b554fd3f9
commit e6249b5296
9 files changed
+243 -67

No files matched your search

+4
View File
@@ -9,6 +9,10 @@ exports_files(
codex_rust_crate( codex_rust_crate(
name = "exec-server", name = "exec-server",
crate_name = "codex_exec_server", crate_name = "codex_exec_server",
crate_srcs = glob([
"src/**/*.rs",
"tests/unit/**/*.rs",
]),
deps_extra = [ deps_extra = [
"@crates//:opentelemetry", "@crates//:opentelemetry",
"@crates//:opentelemetry_sdk", "@crates//:opentelemetry_sdk",
+5
View File
@@ -7,6 +7,11 @@ license.workspace = true
[lib] [lib]
doctest = false doctest = false
# Compiled through #[path] in src/client.rs; cargo-shear 1.11.2 does not normalize
# the ../tests path and incorrectly reports this linked unit-test module as unlinked.
[package.metadata.cargo-shear]
ignored-paths = ["tests/unit/client_provisioning_tests.rs"]
[lints] [lints]
workspace = true workspace = true
+32 -9
View File
@@ -129,6 +129,9 @@ use codex_http_client::HttpClientFactory;
pub(crate) mod accepted; pub(crate) mod accepted;
pub(crate) mod http_client; pub(crate) mod http_client;
mod network_policy_audit; mod network_policy_audit;
#[cfg(test)]
#[path = "../tests/unit/client_provisioning_tests.rs"]
mod provisioning_tests;
#[path = "client_recovery.rs"] #[path = "client_recovery.rs"]
mod recovery; mod recovery;
#[path = "client_refresh.rs"] #[path = "client_refresh.rs"]
@@ -426,6 +429,13 @@ impl LazyRemoteExecServerClient {
} }
pub(crate) async fn status(&self) -> crate::EnvironmentObservedStatus { pub(crate) async fn status(&self) -> crate::EnvironmentObservedStatus {
if let Some(ExecServerTransportParams::Deferred(deferred)) = &self.transport_params
&& let Some(Err(error)) = deferred.readiness.borrow().as_ref()
{
return crate::EnvironmentObservedStatus::Disconnected {
error: ExecServerError::ProvisioningFailed(error.clone()).to_string(),
};
}
// Fail-fast lookup preserves the non-mutating contract: never start or recover a client. // Fail-fast lookup preserves the non-mutating contract: never start or recover a client.
let client = match self.fail_fast().get().await { let client = match self.fail_fast().get().await {
Ok(client) => client, Ok(client) => client,
@@ -498,23 +508,34 @@ impl LazyRemoteExecServerClient {
} }
async fn initial_client(&self) -> Result<ExecServerClient, ExecServerError> { async fn initial_client(&self) -> Result<ExecServerClient, ExecServerError> {
if self.can_reconnect() let result = if self.can_reconnect()
&& (self.startup.cancelled.is_cancelled() && (self.startup.cancelled.is_cancelled()
|| self.startup.result.get().is_some_and(|result| { || self.startup.result.get().is_some_and(|result| {
result result
.as_ref() .as_ref()
.is_err_and(|error| recovery::is_retryable_recovery_error(error)) .is_err_and(|error| recovery::is_retryable_recovery_error(error))
})) })) {
Box::pin(self.reconnect()).await
} else {
self.startup
.result
.get_or_init(|| self.connect_once(&self.startup))
.await
.clone()
.map_err(ExecServerError::ConnectionAttempt)
};
// Ready may arrive before an older attempt publishes its provisioning failure.
if let Err(ExecServerError::ConnectionAttempt(error)) = &result
&& matches!(error.as_ref(), ExecServerError::ProvisioningFailed(_))
&& matches!(
&self.transport_params,
Some(ExecServerTransportParams::Deferred(deferred))
if matches!(*deferred.readiness.borrow(), Some(Ok(())))
)
{ {
return Box::pin(self.reconnect()).await; return Box::pin(self.reconnect()).await;
} }
result
self.startup
.result
.get_or_init(|| self.connect_once(&self.startup))
.await
.clone()
.map_err(ExecServerError::ConnectionAttempt)
} }
async fn reconnect(&self) -> Result<ExecServerClient, ExecServerError> { async fn reconnect(&self) -> Result<ExecServerClient, ExecServerError> {
@@ -618,6 +639,8 @@ pub enum ExecServerError {
Closed, Closed,
#[error("{0}")] #[error("{0}")]
Disconnected(String), Disconnected(String),
#[error("environment unavailable: {0}")]
ProvisioningFailed(String),
#[error("failed to serialize or deserialize exec-server JSON: {0}")] #[error("failed to serialize or deserialize exec-server JSON: {0}")]
Json(#[from] serde_json::Error), Json(#[from] serde_json::Error),
#[error("HTTP request failed: {0}")] #[error("HTTP request failed: {0}")]
+2 -1
View File
@@ -818,7 +818,8 @@ pub(crate) fn is_retryable_recovery_error(error: &ExecServerError) -> bool {
is_transport_closed_error(error) is_transport_closed_error(error)
|| matches!( || matches!(
error, error,
ExecServerError::WebSocketConnectTimeout { .. } ExecServerError::ProvisioningFailed(_)
| ExecServerError::WebSocketConnectTimeout { .. }
| ExecServerError::WebSocketConnect { .. } | ExecServerError::WebSocketConnect { .. }
| ExecServerError::InitializeTimedOut { .. } | ExecServerError::InitializeTimedOut { .. }
) )
+1 -3
View File
@@ -187,9 +187,7 @@ impl ExecServerClient {
.to_string(), .to_string(),
) )
})?; })?;
provisioning_result.map_err(|message| { provisioning_result.map_err(ExecServerError::ProvisioningFailed)?;
ExecServerError::Disconnected(format!("environment unavailable: {message}"))
})?;
} }
let (websocket_url, connect_timeout, initialize_timeout) = match transport_params { let (websocket_url, connect_timeout, initialize_timeout) = match transport_params {
+13 -25
View File
@@ -395,9 +395,9 @@ impl EnvironmentManager {
/// Ordinary environments are ignored. A provisioned environment keeps the same `Arc` from /// Ordinary environments are ignored. A provisioned environment keeps the same `Arc` from
/// Pending through Ready or Failed, and is created if the report arrives first. /// Pending through Ready or Failed, and is created if the report arrives first.
/// ///
/// Ready updates capability roots. Failed keeps the first error. Repeating the same result is /// Ready updates capability roots and can recover a failed provisioning attempt. Failed keeps
/// allowed, but changing between Ready and Failed is rejected. Invalid Ready information fails /// the first error until a Ready report arrives; a late failure cannot replace Ready. Invalid
/// an existing Pending environment but does not create a missing environment. /// Ready information fails an existing Pending environment but does not create a missing one.
/// ///
/// This only updates provisioning. The connection starts when the environment is selected. /// This only updates provisioning. The connection starts when the environment is selected.
pub fn report_environment_provisioning_status( pub fn report_environment_provisioning_status(
@@ -835,31 +835,19 @@ impl Environment {
return Ok(()); return Ok(());
}; };
let mut transition_error = None; let mut transition_error = None;
provisioning_status_tx.send_if_modified(|current| match current.as_ref() { provisioning_status_tx.send_if_modified(|current| {
Some(Err(error)) => { if let Err(error) = validate_environment_ready_info(environment_id, &ready_info) {
transition_error = Some(ExecServerError::Protocol(format!( let pending = current.is_none();
"environment `{environment_id}` provisioning already failed: {error}" if pending {
)));
false
}
None => {
if let Err(error) = validate_environment_ready_info(environment_id, &ready_info) {
*current = Some(Err(error.to_string())); *current = Some(Err(error.to_string()));
transition_error = Some(error);
} else {
self.ready_info.store(Some(Arc::new(ready_info.clone())));
*current = Some(Ok(()));
} }
true transition_error = Some(error);
} return pending;
Some(Ok(())) => {
if let Err(error) = validate_environment_ready_info(environment_id, &ready_info) {
transition_error = Some(error);
} else {
self.ready_info.store(Some(Arc::new(ready_info.clone())));
}
false
} }
self.ready_info.store(Some(Arc::new(ready_info.clone())));
let was_ready = matches!(current, Some(Ok(())));
*current = Some(Ok(()));
!was_ready
}); });
transition_error.map_or(Ok(()), Err) transition_error.map_or(Ok(()), Err)
@@ -136,7 +136,7 @@ async fn ordinary_environment_ignores_provisioning_reports() -> anyhow::Result<(
} }
#[tokio::test] #[tokio::test]
async fn failure_before_materialization_is_terminal_without_connecting() -> anyhow::Result<()> { async fn failure_before_materialization_is_reported_without_connecting() -> anyhow::Result<()> {
let manager = environment_manager_without_environments(); let manager = environment_manager_without_environments();
let provider = Arc::new(FailingNoiseConnectProvider::default()); let provider = Arc::new(FailingNoiseConnectProvider::default());
@@ -153,8 +153,15 @@ async fn failure_before_materialization_is_terminal_without_connecting() -> anyh
)?; )?;
assert!(Arc::ptr_eq(&failed, &materialized)); assert!(Arc::ptr_eq(&failed, &materialized));
assert_eq!(
failed.status().await,
codex_exec_server::EnvironmentObservedStatus::Disconnected {
error: "environment unavailable: provisioning failed".to_string(),
}
);
let error = failed.wait_until_ready().await.unwrap_err(); let error = failed.wait_until_ready().await.unwrap_err();
assert!(error.to_string().ends_with("provisioning failed")); assert!(error.to_string().ends_with("provisioning failed"));
assert!(failed.startup_finished());
assert_eq!(provider.calls(), 0); assert_eq!(provider.calls(), 0);
Ok(()) Ok(())
} }
@@ -183,7 +190,7 @@ async fn failure_releases_the_existing_pending_environment_without_connecting()
} }
#[tokio::test] #[tokio::test]
async fn repeated_failure_preserves_the_first_error_and_rejects_ready() -> anyhow::Result<()> { async fn repeated_failure_preserves_the_first_error_until_ready() -> anyhow::Result<()> {
let manager = environment_manager_without_environments(); let manager = environment_manager_without_environments();
let provider = Arc::new(FailingNoiseConnectProvider::default()); let provider = Arc::new(FailingNoiseConnectProvider::default());
let failed = manager let failed = manager
@@ -203,18 +210,39 @@ async fn repeated_failure_preserves_the_first_error_and_rejects_ready() -> anyho
.expect("repeated failure should be idempotent"); .expect("repeated failure should be idempotent");
assert!(Arc::ptr_eq(&failed, &repeated)); assert!(Arc::ptr_eq(&failed, &repeated));
let error = manager
.report_environment_provisioning_status(
"tools".to_string(),
Ok(ready_info("selected-root", "tools")?),
provider.clone(),
)
.unwrap_err();
assert!(error.to_string().contains("first failure"));
assert!(failed.selected_capability_roots().is_empty());
let error = failed.wait_until_ready().await.unwrap_err(); let error = failed.wait_until_ready().await.unwrap_err();
assert!(error.to_string().ends_with("first failure")); assert!(error.to_string().ends_with("first failure"));
assert_eq!(provider.calls(), 0); assert_eq!(provider.calls(), 0);
let invalid_ready_error = manager
.report_environment_provisioning_status(
"tools".to_string(),
Ok(ready_info("selected-root", "other")?),
provider.clone(),
)
.unwrap_err();
assert!(matches!(invalid_ready_error, ExecServerError::Protocol(_)));
assert_eq!(
failed.wait_until_ready().await.unwrap_err().to_string(),
error.to_string()
);
assert!(failed.selected_capability_roots().is_empty());
assert_eq!(provider.calls(), 0);
let selected = ready_info("selected-root", "tools")?;
let ready = manager
.report_environment_provisioning_status(
"tools".to_string(),
Ok(selected.clone()),
provider.clone(),
)?
.expect("successful provisioning should recover the same environment");
assert!(Arc::ptr_eq(&failed, &ready));
assert_eq!(
failed.selected_capability_roots(),
selected.selected_capability_roots
);
let error = failed.wait_until_ready().await.unwrap_err();
assert!(error.to_string().contains("test Noise provider called"));
assert_eq!(provider.calls(), 1);
Ok(()) Ok(())
} }
@@ -319,19 +347,19 @@ async fn invalid_ready_report_fails_the_provisioning_gate() -> anyhow::Result<()
assert!(environment.selected_capability_roots().is_empty()); assert!(environment.selected_capability_roots().is_empty());
assert_eq!(provider.calls(), 0); assert_eq!(provider.calls(), 0);
let later_ready_error = manager let selected = ready_info("selected-root", "tools")?;
let reported = manager
.report_environment_provisioning_status( .report_environment_provisioning_status(
"tools".to_string(), "tools".to_string(),
Ok(ready_info("selected-root", "tools")?), Ok(selected.clone()),
provider.clone(), provider.clone(),
) )?
.unwrap_err(); .expect("a corrected ready report should recover provisioning");
assert!( assert!(Arc::ptr_eq(&environment, &reported));
later_ready_error assert_eq!(
.to_string() environment.selected_capability_roots(),
.contains("provisioning already failed") selected.selected_capability_roots
); );
assert!(environment.selected_capability_roots().is_empty());
assert_eq!(provider.calls(), 0); assert_eq!(provider.calls(), 0);
Ok(()) Ok(())
} }
+62 -9
View File
@@ -94,7 +94,7 @@ impl NoiseRendezvousConnectProvider for FreshBundleNoiseConnectProvider {
#[tokio::test(flavor = "multi_thread", worker_threads = 4)] #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[serial_test::serial] #[serial_test::serial]
async fn pending_noise_environment_connects_and_reconnects_after_ready_report() -> Result<()> { async fn failed_noise_environment_recovers_and_reconnects_after_ready_report() -> Result<()> {
let listener = TcpListener::bind("127.0.0.1:0").await?; let listener = TcpListener::bind("127.0.0.1:0").await?;
let rendezvous_address = listener.local_addr()?; let rendezvous_address = listener.local_addr()?;
let environment_rendezvous_url = let environment_rendezvous_url =
@@ -194,21 +194,46 @@ async fn pending_noise_environment_connects_and_reconnects_after_ready_report()
executor_public_key: registered_executor_public_key(&registry).await?, executor_public_key: registered_executor_public_key(&registry).await?,
calls: AtomicUsize::new(0), calls: AtomicUsize::new(0),
}); });
let manager = EnvironmentManager::without_environments(http_client_factory); let manager = Arc::new(EnvironmentManager::without_environments(
http_client_factory,
));
let environment = manager let environment = manager
.materialize_pending_noise_environment(ENVIRONMENT_ID.to_string(), provider.clone())?; .materialize_pending_noise_environment(ENVIRONMENT_ID.to_string(), provider.clone())?;
let mut connection_state = environment let mut connection_state = environment
.subscribe_connection_state() .subscribe_connection_state()
.context("remote environment connection state")?; .context("remote environment connection state")?;
assert_eq!(provider.calls(), 0); let capability_root = TempDir::new()?;
let skill_file = capability_root.path().join("SKILL.md");
let skill_contents = b"# Recovered capability\n";
std::fs::write(&skill_file, skill_contents)?;
let selected_capability_roots = vec![SelectedCapabilityRoot { let selected_capability_roots = vec![SelectedCapabilityRoot {
id: "executor-plugin".to_string(), id: "executor-plugin".to_string(),
location: CapabilityRootLocation::Environment { location: CapabilityRootLocation::Environment {
environment_id: ENVIRONMENT_ID.to_string(), environment_id: ENVIRONMENT_ID.to_string(),
path: PathUri::parse("file:///plugins/executor-plugin")?, path: PathUri::from_host_native_path(capability_root.path())?,
}, },
}]; }];
assert!(
manager
.resolve_selected_capability_roots(&selected_capability_roots, &HashMap::new())
.await
.is_empty()
);
assert_eq!(provider.calls(), 0);
manager.report_environment_provisioning_status(
ENVIRONMENT_ID.to_string(),
Err("first provisioning attempt failed".to_string()),
provider.clone(),
)?;
timeout(TEST_TIMEOUT, async {
while !environment.startup_finished() {
tokio::task::yield_now().await;
}
})
.await
.expect("failed capability startup should record its completion");
assert_eq!(provider.calls(), 0);
let reported = manager let reported = manager
.report_environment_provisioning_status( .report_environment_provisioning_status(
ENVIRONMENT_ID.to_string(), ENVIRONMENT_ID.to_string(),
@@ -220,9 +245,15 @@ async fn pending_noise_environment_connects_and_reconnects_after_ready_report()
.context("ready report should apply to the pending environment")?; .context("ready report should apply to the pending environment")?;
assert!(Arc::ptr_eq(&environment, &reported)); assert!(Arc::ptr_eq(&environment, &reported));
assert_eq!(provider.calls(), 0); assert_eq!(provider.calls(), 0);
let initial_info = tokio::spawn({ // Capability resolution must retry on its own, without an explicit connection call.
let environment = Arc::clone(&environment); let resolved_roots = tokio::spawn({
async move { environment.info().await } let manager = Arc::clone(&manager);
let selected_capability_roots = selected_capability_roots.clone();
async move {
manager
.resolve_selected_capability_roots(&selected_capability_roots, &HashMap::new())
.await
}
}); });
let harness_websocket = accept_websocket(&listener, "harness").await?; let harness_websocket = accept_websocket(&listener, "harness").await?;
assert_eq!( assert_eq!(
@@ -234,9 +265,31 @@ async fn pending_noise_environment_connects_and_reconnects_after_ready_report()
harness_websocket, harness_websocket,
Arc::new(Mutex::new(Vec::new())), Arc::new(Mutex::new(Vec::new())),
)); ));
let initial_info = timeout(TEST_TIMEOUT, initial_info) let resolved_roots = timeout(TEST_TIMEOUT, resolved_roots)
.await .await
.context("pending Noise environment should become ready")???; .context("capability resolution should recover after Ready")??;
assert_eq!(
resolved_roots
.iter()
.map(|root| root.selected_root().clone())
.collect::<Vec<_>>(),
selected_capability_roots
);
let [resolved_root] = resolved_roots.as_slice() else {
anyhow::bail!("the recovered capability root should resolve");
};
assert!(Arc::ptr_eq(resolved_root.environment(), &environment));
let recovered_skill = resolved_root
.environment()
.get_filesystem()
.read_file(
&PathUri::from_host_native_path(skill_file)?,
Default::default(),
/*sandbox*/ None,
)
.await?;
assert_eq!(recovered_skill, skill_contents.to_vec());
let initial_info = environment.info().await?;
assert_eq!( assert_eq!(
environment.selected_capability_roots(), environment.selected_capability_roots(),
selected_capability_roots selected_capability_roots
@@ -0,0 +1,76 @@
//! Exercises provisioning recovery while startup is publishing its previous failure.
use super::*;
use crate::client_api::Deferred;
use crate::noise_channel::NoiseChannelIdentity;
use futures::poll;
use tokio::sync::oneshot;
#[test_case::test_case(false; "initial startup")]
#[test_case::test_case(true; "subsequent attempt")]
#[tokio::test]
async fn caller_after_ready_retries_an_unpublished_provisioning_failure(reconnecting: bool) {
let (readiness, readiness_rx) = watch::channel(Some(Err("first failure".to_string())));
let client = LazyRemoteExecServerClient::new(
ExecServerTransportParams::Deferred(Box::new(Deferred {
readiness: readiness_rx,
transport: ExecServerTransportParams::NoiseRendezvous {
provider: Arc::new(FailingProvider),
identity: NoiseChannelIdentity::generate().expect("Noise identity"),
},
})),
HttpClientFactory::new(codex_http_client::OutboundProxyPolicy::ReqwestDefault),
);
let (failed_tx, failed_rx) = oneshot::channel();
let (publish_tx, publish_rx) = oneshot::channel();
let attempt = if reconnecting {
assert!(client.wait_until_ready().await.is_err());
let attempt = Arc::new(ConnectionAttempt::default());
*client.reconnect.lock().expect("reconnect lock") = Some(Arc::clone(&attempt));
attempt
} else {
Arc::clone(&client.startup)
};
let startup_client = client.clone();
let startup = tokio::spawn(async move {
attempt
.result
.get_or_init(|| async {
let result = startup_client.connect_once(&attempt).await;
assert!(result.is_err());
failed_tx.send(()).expect("failure observed");
// Suspend at the publication boundary, as another executor thread could.
publish_rx.await.expect("publish startup result");
result
})
.await
.clone()
});
failed_rx.await.expect("startup consumed the Failed report");
readiness.send_replace(Some(Ok(())));
let mut after_ready = Box::pin(client.wait_until_ready());
assert!(poll!(&mut after_ready).is_pending());
publish_tx.send(()).expect("release startup");
let error = after_ready.await.unwrap_err();
assert!(
error.to_string().contains("provider reached after Ready"),
"post-Ready caller should retry the stale failure: {error}"
);
assert!(startup.await.expect("startup task").is_err());
}
struct FailingProvider;
impl crate::NoiseRendezvousConnectProvider for FailingProvider {
fn connect_bundle(
&self,
_: crate::NoiseChannelPublicKey,
) -> BoxFuture<'_, Result<crate::NoiseRendezvousConnectBundle, ExecServerError>> {
Box::pin(async {
Err(ExecServerError::Protocol(
"provider reached after Ready".to_string(),
))
})
}
}