@@ -989,6 +989,18 @@ impl ComputeRuntime {
989989 }
990990 } ) ?;
991991
992+ if let Err ( status) = Box :: pin ( crate :: grpc:: policy:: initialize_policy_history (
993+ self . store . as_ref ( ) ,
994+ & sandbox,
995+ crate :: grpc:: policy:: InitialPolicyHistoryStatus :: Pending ,
996+ ) )
997+ . await
998+ {
999+ let _ = self . store . delete ( Sandbox :: object_type ( ) , & sandbox_id) . await ;
1000+ self . sandbox_index . remove_sandbox ( & sandbox_id) ;
1001+ return Err ( status) ;
1002+ }
1003+
9921004 if let Some ( token) = sandbox_token
9931005 && let Some ( spec) = driver_sandbox. spec . as_mut ( )
9941006 {
@@ -1026,6 +1038,10 @@ impl ComputeRuntime {
10261038 Ok ( sandbox)
10271039 }
10281040 Err ( status) if status. code ( ) == Code :: AlreadyExists => {
1041+ let _ = self
1042+ . store
1043+ . delete_by_scope ( POLICY_OBJECT_TYPE , sandbox. object_id ( ) )
1044+ . await ;
10291045 let _ = self
10301046 . store
10311047 . delete ( Sandbox :: object_type ( ) , sandbox. object_id ( ) )
@@ -1034,6 +1050,10 @@ impl ComputeRuntime {
10341050 Err ( Status :: already_exists ( "sandbox already exists" ) )
10351051 }
10361052 Err ( status) if status. code ( ) == Code :: FailedPrecondition => {
1053+ let _ = self
1054+ . store
1055+ . delete_by_scope ( POLICY_OBJECT_TYPE , sandbox. object_id ( ) )
1056+ . await ;
10371057 let _ = self
10381058 . store
10391059 . delete ( Sandbox :: object_type ( ) , sandbox. object_id ( ) )
@@ -1042,6 +1062,10 @@ impl ComputeRuntime {
10421062 Err ( Status :: failed_precondition ( status. message ( ) . to_string ( ) ) )
10431063 }
10441064 Err ( err) => {
1065+ let _ = self
1066+ . store
1067+ . delete_by_scope ( POLICY_OBJECT_TYPE , sandbox. object_id ( ) )
1068+ . await ;
10451069 let _ = self
10461070 . store
10471071 . delete ( Sandbox :: object_type ( ) , sandbox. object_id ( ) )
@@ -4938,6 +4962,7 @@ pub async fn new_test_runtime_with_driver(
49384962#[ cfg( test) ]
49394963mod tests {
49404964 use super :: * ;
4965+ use crate :: policy_store:: PolicyStoreExt ;
49414966 use futures:: stream;
49424967 use openshell_core:: proto:: compute:: v1:: {
49434968 CreateSandboxResponse , DeleteSandboxResponse , GetCapabilitiesResponse , GetSandboxRequest ,
@@ -11240,6 +11265,36 @@ mod tests {
1124011265 ) ;
1124111266 }
1124211267
11268+ #[ tokio:: test]
11269+ async fn create_sandbox_persists_initial_policy_revision ( ) {
11270+ let runtime = test_runtime ( Arc :: new ( TestDriver :: default ( ) ) ) . await ;
11271+ let mut sandbox = sandbox_record (
11272+ "sb-initial-policy" ,
11273+ "initial-policy" ,
11274+ SandboxPhase :: Provisioning ,
11275+ ) ;
11276+ let policy = openshell_core:: proto:: SandboxPolicy :: default ( ) ;
11277+ sandbox. spec = Some ( SandboxSpec {
11278+ policy : Some ( policy. clone ( ) ) ,
11279+ ..Default :: default ( )
11280+ } ) ;
11281+
11282+ runtime. create_sandbox ( sandbox, None , false ) . await . unwrap ( ) ;
11283+
11284+ let revision = runtime
11285+ . store
11286+ . get_latest_policy ( "sb-initial-policy" )
11287+ . await
11288+ . unwrap ( )
11289+ . expect ( "initial policy revision" ) ;
11290+ assert_eq ! ( revision. version, 1 ) ;
11291+ assert_eq ! (
11292+ revision. policy_hash,
11293+ crate :: grpc:: policy:: deterministic_policy_hash( & policy)
11294+ ) ;
11295+ assert_eq ! ( revision. status, "pending" ) ;
11296+ }
11297+
1124311298 #[ tokio:: test]
1124411299 async fn created_sandbox_is_immediately_visible_to_label_selectors ( ) {
1124511300 let runtime = test_runtime ( Arc :: new ( TestDriver :: default ( ) ) ) . await ;
0 commit comments