@@ -410,7 +410,9 @@ async fn finalize_sandbox_create_session(
410410 }
411411
412412 let names = [ sandbox_name. to_string ( ) ] ;
413- if let Err ( err) = sandbox_delete ( server, & names, false , workspace, tls, gateway) . await {
413+ if let Err ( err) = sandbox_delete (
414+ server, & names, false , workspace, tls, gateway, SandboxDeleteOptions :: default ( ) ,
415+ ) . await {
414416 if let Ok ( exit_code) = session_result. as_ref ( ) {
415417 return Err ( miette:: miette!(
416418 "sandbox command exited with status {exit_code}, but ephemeral cleanup failed: {err}"
@@ -3714,6 +3716,15 @@ fn labels_display(labels: &HashMap<String, String>) -> String {
37143716 pairs. join ( ", " )
37153717}
37163718
3719+ /// Controls whether sandbox deletion waits for durable absence.
3720+ #[ derive( Debug , Clone , Copy , Default ) ]
3721+ pub struct SandboxDeleteOptions {
3722+ /// Return after the gateway acknowledges an asynchronous delete.
3723+ pub no_wait : bool ,
3724+ /// Override the lifecycle timeout. The environment/default applies when unset.
3725+ pub timeout : Option < Duration > ,
3726+ }
3727+
37173728/// Delete a sandbox by name, or all sandboxes when `all` is true.
37183729pub async fn sandbox_delete (
37193730 server : & str ,
@@ -3722,6 +3733,7 @@ pub async fn sandbox_delete(
37223733 workspace : & str ,
37233734 tls : & TlsOptions ,
37243735 gateway : & str ,
3736+ options : SandboxDeleteOptions ,
37253737) -> Result < ( ) > {
37263738 let mut client = grpc_client ( server, tls) . await ?;
37273739
@@ -3791,12 +3803,23 @@ pub async fn sandbox_delete(
37913803 }
37923804 } ;
37933805
3794- match response. into_inner ( ) . outcome ( ) {
3806+ let deletion = response. into_inner ( ) ;
3807+ match deletion. outcome ( ) {
37953808 DeletionOutcome :: Completed => println ! ( "{} Deleted sandbox {name}" , "✓" . green( ) . bold( ) ) ,
3796- DeletionOutcome :: Accepted => println ! (
3809+ DeletionOutcome :: Accepted if options . no_wait => println ! (
37973810 "{} Sandbox {name} deletion accepted; cleanup is pending" ,
37983811 "✓" . green( ) . bold( )
37993812 ) ,
3813+ DeletionOutcome :: Accepted => {
3814+ if let Err ( err) = wait_for_sandbox_deleted (
3815+ & mut client, name, workspace, & deletion. sandbox_id , options. timeout ,
3816+ ) . await {
3817+ eprintln ! ( "{} Failed to verify sandbox {name} deletion: {err}" , "!" . red( ) . bold( ) ) ;
3818+ failures. push ( name. clone ( ) ) ;
3819+ continue ;
3820+ }
3821+ println ! ( "{} Deleted sandbox {name}" , "✓" . green( ) . bold( ) ) ;
3822+ }
38003823 DeletionOutcome :: AlreadyAbsent => {
38013824 println ! ( "{} Sandbox {name} already deleted" , "✓" . green( ) . bold( ) ) ;
38023825 }
@@ -3815,6 +3838,76 @@ pub async fn sandbox_delete(
38153838 aggregate_delete_failures ( "sandbox" , & failures)
38163839}
38173840
3841+ fn lifecycle_timeout ( explicit : Option < Duration > ) -> Duration {
3842+ explicit. unwrap_or_else ( || {
3843+ Duration :: from_secs (
3844+ std:: env:: var ( "OPENSHELL_LIFECYCLE_TIMEOUT" )
3845+ . ok ( )
3846+ . and_then ( |value| value. parse ( ) . ok ( ) )
3847+ . unwrap_or ( 300 ) ,
3848+ )
3849+ } )
3850+ }
3851+
3852+ async fn wait_for_sandbox_deleted (
3853+ client : & mut crate :: tls:: GrpcClient ,
3854+ name : & str ,
3855+ workspace : & str ,
3856+ expected_sandbox_id : & str ,
3857+ timeout_override : Option < Duration > ,
3858+ ) -> Result < ( ) > {
3859+ const INITIAL_POLL_INTERVAL : Duration = Duration :: from_millis ( 100 ) ;
3860+ const MAX_POLL_INTERVAL : Duration = Duration :: from_secs ( 2 ) ;
3861+
3862+ let timeout = lifecycle_timeout ( timeout_override) ;
3863+ let deadline = Instant :: now ( )
3864+ . checked_add ( timeout)
3865+ . ok_or_else ( || miette ! ( "sandbox deletion timeout is too large" ) ) ?;
3866+ let timeout_error = || {
3867+ miette ! (
3868+ "timed out after {}s waiting for sandbox {name} deletion to complete; deletion continues asynchronously" ,
3869+ timeout. as_secs( )
3870+ )
3871+ } ;
3872+ let mut poll_interval = INITIAL_POLL_INTERVAL ;
3873+
3874+ loop {
3875+ let remaining = deadline. saturating_duration_since ( Instant :: now ( ) ) ;
3876+ if remaining. is_zero ( ) {
3877+ return Err ( timeout_error ( ) ) ;
3878+ }
3879+
3880+ match tokio:: time:: timeout (
3881+ remaining,
3882+ client. get_sandbox ( GetSandboxRequest {
3883+ name : name. to_string ( ) ,
3884+ workspace_scope : Some ( openshell_core:: proto:: workspace_selector ( workspace) ) ,
3885+ } ) ,
3886+ )
3887+ . await
3888+ {
3889+ Ok ( Ok ( response) ) if response. into_inner ( ) . sandbox . as_ref ( ) . is_some_and ( |sandbox| {
3890+ !expected_sandbox_id. is_empty ( ) && sandbox. object_id ( ) != expected_sandbox_id
3891+ } ) => return Ok ( ( ) ) ,
3892+ Ok ( Ok ( _) ) => { }
3893+ Ok ( Err ( status) ) if status. code ( ) == Code :: NotFound => return Ok ( ( ) ) ,
3894+ Ok ( Err ( status) ) => {
3895+ return Err ( miette ! (
3896+ "failed to verify deletion of sandbox {name}: {status}"
3897+ ) ) ;
3898+ }
3899+ Err ( _) => return Err ( timeout_error ( ) ) ,
3900+ }
3901+
3902+ let remaining = deadline. saturating_duration_since ( Instant :: now ( ) ) ;
3903+ if remaining. is_zero ( ) {
3904+ return Err ( timeout_error ( ) ) ;
3905+ }
3906+ tokio:: time:: sleep ( poll_interval. min ( remaining) ) . await ;
3907+ poll_interval = poll_interval. saturating_mul ( 2 ) . min ( MAX_POLL_INTERVAL ) ;
3908+ }
3909+ }
3910+
38183911/// Stop a sandbox while retaining its persistent workspace.
38193912pub async fn sandbox_stop (
38203913 server : & str ,
0 commit comments