@@ -19,8 +19,8 @@ use openshell_core::proto::{
1919 ExecSandboxInput , ExecSandboxRequest , ExecSandboxStderr , ExecSandboxStdout , GetSandboxRequest ,
2020 ListSandboxProvidersRequest , ListSandboxProvidersResponse , ListSandboxesRequest ,
2121 ListSandboxesResponse , Provider , RevokeSshSessionRequest , RevokeSshSessionResponse ,
22- SandboxResponse , SandboxStreamEvent , SshRelayTarget , TcpForwardFrame , TcpForwardInit ,
23- TcpRelayTarget , WatchSandboxRequest , relay_open, tcp_forward_init,
22+ SandboxResponse , SandboxSpec , SandboxStreamEvent , SshRelayTarget , TcpForwardFrame ,
23+ TcpForwardInit , TcpRelayTarget , WatchSandboxRequest , relay_open, tcp_forward_init,
2424} ;
2525use openshell_core:: proto:: { Sandbox , SandboxPhase , SandboxTemplate , SshSession } ;
2626use openshell_core:: telemetry:: {
@@ -100,7 +100,7 @@ fn emit_sandbox_create_telemetry(
100100 } ;
101101 openshell_core:: telemetry:: emit_sandbox_create (
102102 outcome,
103- spec. gpu ,
103+ effective_gpu_request ( spec) ,
104104 spec. providers . len ( ) as u64 ,
105105 spec. policy . is_some ( ) ,
106106 template_source,
@@ -114,6 +114,10 @@ fn telemetry_compute_driver(
114114 TelemetryComputeDriver :: from_driver_kind ( driver_kind)
115115}
116116
117+ fn effective_gpu_request ( spec : & SandboxSpec ) -> bool {
118+ spec. gpu || spec. gpu_count . is_some ( ) || !spec. gpu_device . is_empty ( )
119+ }
120+
117121async fn handle_create_sandbox_inner (
118122 state : & Arc < ServerState > ,
119123 request : Request < CreateSandboxRequest > ,
@@ -143,8 +147,10 @@ async fn handle_create_sandbox_inner(
143147 }
144148 validate_provider_environment_keys_unique ( state. store . as_ref ( ) , & spec. providers ) . await ?;
145149
146- // Ensure the template always carries the resolved image.
147150 let mut spec = spec;
151+ spec. gpu = effective_gpu_request ( & spec) ;
152+
153+ // Ensure the template always carries the resolved image.
148154 let template = spec. template . get_or_insert_with ( SandboxTemplate :: default) ;
149155 if template. image . is_empty ( ) {
150156 template. image = state. compute . default_image ( ) . to_string ( ) ;
@@ -2208,7 +2214,7 @@ mod tests {
22082214 labels : std:: iter:: once ( ( "team" . to_string ( ) , "agents" . to_string ( ) ) ) . collect ( ) ,
22092215 resource_version : 0 ,
22102216 } ) ,
2211- spec : Some ( openshell_core :: proto :: SandboxSpec {
2217+ spec : Some ( SandboxSpec {
22122218 log_level : "debug" . to_string ( ) ,
22132219 policy : Some ( openshell_core:: proto:: SandboxPolicy :: default ( ) ) ,
22142220 providers,
@@ -2572,7 +2578,7 @@ mod tests {
25722578 & state,
25732579 Request :: new ( CreateSandboxRequest {
25742580 name : "collision" . to_string ( ) ,
2575- spec : Some ( openshell_core :: proto :: SandboxSpec {
2581+ spec : Some ( SandboxSpec {
25762582 providers : vec ! [ "provider-a" . to_string( ) , "provider-b" . to_string( ) ] ,
25772583 ..Default :: default ( )
25782584 } ) ,
@@ -2588,6 +2594,72 @@ mod tests {
25882594 assert ! ( err. message( ) . contains( "provider-b" ) ) ;
25892595 }
25902596
2597+ #[ tokio:: test]
2598+ async fn create_sandbox_gpu_count_implies_gpu ( ) {
2599+ let state = test_server_state ( ) . await ;
2600+
2601+ let response = handle_create_sandbox (
2602+ & state,
2603+ Request :: new ( CreateSandboxRequest {
2604+ name : "gpu-count" . to_string ( ) ,
2605+ spec : Some ( SandboxSpec {
2606+ gpu_count : Some ( 2 ) ,
2607+ ..Default :: default ( )
2608+ } ) ,
2609+ labels : HashMap :: new ( ) ,
2610+ } ) ,
2611+ )
2612+ . await
2613+ . unwrap ( )
2614+ . into_inner ( ) ;
2615+
2616+ let sandbox = response. sandbox . unwrap ( ) ;
2617+ let spec = sandbox. spec . as_ref ( ) . unwrap ( ) ;
2618+ assert ! ( spec. gpu) ;
2619+ assert_eq ! ( spec. gpu_count, Some ( 2 ) ) ;
2620+
2621+ let stored = state
2622+ . store
2623+ . get_message_by_name :: < Sandbox > ( "gpu-count" )
2624+ . await
2625+ . unwrap ( )
2626+ . unwrap ( ) ;
2627+ assert ! ( stored. spec. as_ref( ) . unwrap( ) . gpu) ;
2628+ }
2629+
2630+ #[ tokio:: test]
2631+ async fn create_sandbox_gpu_device_implies_gpu ( ) {
2632+ let state = test_server_state ( ) . await ;
2633+
2634+ let response = handle_create_sandbox (
2635+ & state,
2636+ Request :: new ( CreateSandboxRequest {
2637+ name : "gpu-device" . to_string ( ) ,
2638+ spec : Some ( SandboxSpec {
2639+ gpu_device : "nvidia.com/gpu=0" . to_string ( ) ,
2640+ ..Default :: default ( )
2641+ } ) ,
2642+ labels : HashMap :: new ( ) ,
2643+ } ) ,
2644+ )
2645+ . await
2646+ . unwrap ( )
2647+ . into_inner ( ) ;
2648+
2649+ let sandbox = response. sandbox . unwrap ( ) ;
2650+ let spec = sandbox. spec . as_ref ( ) . unwrap ( ) ;
2651+ assert ! ( spec. gpu) ;
2652+ assert_eq ! ( spec. gpu_device, "nvidia.com/gpu=0" ) ;
2653+
2654+ let stored = state
2655+ . store
2656+ . get_message_by_name :: < Sandbox > ( "gpu-device" )
2657+ . await
2658+ . unwrap ( )
2659+ . unwrap ( ) ;
2660+ assert ! ( stored. spec. as_ref( ) . unwrap( ) . gpu) ;
2661+ }
2662+
25912663 #[ tokio:: test]
25922664 async fn attach_sandbox_provider_rejects_credential_key_collisions ( ) {
25932665 let state = test_server_state ( ) . await ;
0 commit comments