@@ -165,6 +165,7 @@ fn remote_control_handle_with_current_enrollment(
165165 ) ) ) ;
166166 RemoteControlHandle {
167167 enabled_tx : Arc :: new ( enabled_tx) ,
168+ reconnect_tx : Arc :: new ( watch:: channel ( 0 ) . 0 ) ,
168169 status_tx : Arc :: new ( status_tx) ,
169170 state_db_available : true ,
170171 state_db : None ,
@@ -943,6 +944,184 @@ async fn remote_control_handle_enable_disable_stops_and_restarts_connections() {
943944 let _ = remote_task. await ;
944945}
945946
947+ #[ tokio:: test]
948+ async fn remote_control_enable_reconnects_when_already_connected ( ) {
949+ let listener = TcpListener :: bind ( "127.0.0.1:0" )
950+ . await
951+ . expect ( "listener should bind" ) ;
952+ let remote_control_url = remote_control_url_for_listener ( & listener) ;
953+ let codex_home = TempDir :: new ( ) . expect ( "temp dir should create" ) ;
954+ let ( transport_event_tx, _transport_event_rx) =
955+ mpsc:: channel :: < TransportEvent > ( CHANNEL_CAPACITY ) ;
956+ let shutdown_token = CancellationToken :: new ( ) ;
957+ let ( remote_task, remote_handle) = start_remote_control (
958+ RemoteControlStartConfig {
959+ remote_control_url,
960+ installation_id : TEST_INSTALLATION_ID . to_string ( ) ,
961+ } ,
962+ Some ( remote_control_state_runtime ( & codex_home) . await ) ,
963+ remote_control_auth_manager ( ) ,
964+ transport_event_tx,
965+ shutdown_token. clone ( ) ,
966+ /*app_server_client_name_rx*/ None ,
967+ /*initial_enabled*/ true ,
968+ )
969+ . await
970+ . expect ( "remote control should start" ) ;
971+ let mut status_rx = remote_handle. status_receiver ( ) ;
972+
973+ let enroll_request = accept_http_request ( & listener) . await ;
974+ assert_eq ! (
975+ enroll_request. request_line,
976+ "POST /backend-api/wham/remote/control/server/enroll HTTP/1.1"
977+ ) ;
978+ respond_with_json (
979+ enroll_request. stream ,
980+ remote_control_server_token_response (
981+ "srv_e_test" ,
982+ "env_test" ,
983+ TEST_REMOTE_CONTROL_SERVER_TOKEN ,
984+ ) ,
985+ )
986+ . await ;
987+ let mut first_websocket = accept_remote_control_connection ( & listener) . await ;
988+ expect_remote_control_status_snapshot (
989+ & mut status_rx,
990+ RemoteControlStatusChangedNotification {
991+ status : RemoteControlConnectionStatus :: Connected ,
992+ server_name : test_server_name ( ) ,
993+ installation_id : TEST_INSTALLATION_ID . to_string ( ) ,
994+ environment_id : Some ( "env_test" . to_string ( ) ) ,
995+ } ,
996+ )
997+ . await ;
998+ assert_eq ! (
999+ remote_handle. enable( ) . expect( "enable should succeed" ) ,
1000+ RemoteControlStatusChangedNotification {
1001+ status: RemoteControlConnectionStatus :: Connecting ,
1002+ server_name: test_server_name( ) ,
1003+ installation_id: TEST_INSTALLATION_ID . to_string( ) ,
1004+ environment_id: Some ( "env_test" . to_string( ) ) ,
1005+ }
1006+ ) ;
1007+ let mut second_websocket = timeout (
1008+ Duration :: from_secs ( 1 ) ,
1009+ accept_remote_control_connection ( & listener) ,
1010+ )
1011+ . await
1012+ . expect ( "re-enabling connected remote control should reconnect" ) ;
1013+ let first_websocket_message = timeout ( Duration :: from_secs ( 1 ) , first_websocket. next ( ) )
1014+ . await
1015+ . expect ( "re-enabling connected remote control should close the websocket" ) ;
1016+ assert ! (
1017+ matches!(
1018+ first_websocket_message,
1019+ Some ( Ok ( tungstenite:: Message :: Close ( _) ) )
1020+ ) ,
1021+ "expected first websocket to receive close frame, got {first_websocket_message:?}"
1022+ ) ;
1023+ expect_remote_control_status (
1024+ & mut status_rx,
1025+ /*expected_status*/ None ,
1026+ Some ( "env_test" ) ,
1027+ )
1028+ . await ;
1029+ second_websocket
1030+ . close ( None )
1031+ . await
1032+ . expect ( "second websocket should close" ) ;
1033+
1034+ shutdown_token. cancel ( ) ;
1035+ timeout ( Duration :: from_secs ( 1 ) , remote_task)
1036+ . await
1037+ . expect ( "remote control task should stop after shutdown" )
1038+ . expect ( "remote control task should join" ) ;
1039+ }
1040+
1041+ #[ tokio:: test]
1042+ async fn remote_control_enable_reconnect_does_not_replay_after_disconnect_race ( ) {
1043+ let listener = TcpListener :: bind ( "127.0.0.1:0" )
1044+ . await
1045+ . expect ( "listener should bind" ) ;
1046+ let remote_control_url = remote_control_url_for_listener ( & listener) ;
1047+ let codex_home = TempDir :: new ( ) . expect ( "temp dir should create" ) ;
1048+ let ( transport_event_tx, _transport_event_rx) =
1049+ mpsc:: channel :: < TransportEvent > ( CHANNEL_CAPACITY ) ;
1050+ let shutdown_token = CancellationToken :: new ( ) ;
1051+ let ( remote_task, remote_handle) = start_remote_control (
1052+ RemoteControlStartConfig {
1053+ remote_control_url,
1054+ installation_id : TEST_INSTALLATION_ID . to_string ( ) ,
1055+ } ,
1056+ Some ( remote_control_state_runtime ( & codex_home) . await ) ,
1057+ remote_control_auth_manager ( ) ,
1058+ transport_event_tx,
1059+ shutdown_token. clone ( ) ,
1060+ /*app_server_client_name_rx*/ None ,
1061+ /*initial_enabled*/ true ,
1062+ )
1063+ . await
1064+ . expect ( "remote control should start" ) ;
1065+ let mut status_rx = remote_handle. status_receiver ( ) ;
1066+
1067+ let enroll_request = accept_http_request ( & listener) . await ;
1068+ respond_with_json (
1069+ enroll_request. stream ,
1070+ remote_control_server_token_response (
1071+ "srv_e_test" ,
1072+ "env_test" ,
1073+ TEST_REMOTE_CONTROL_SERVER_TOKEN ,
1074+ ) ,
1075+ )
1076+ . await ;
1077+ let mut first_websocket = accept_remote_control_connection ( & listener) . await ;
1078+ expect_remote_control_status_snapshot (
1079+ & mut status_rx,
1080+ RemoteControlStatusChangedNotification {
1081+ status : RemoteControlConnectionStatus :: Connected ,
1082+ server_name : test_server_name ( ) ,
1083+ installation_id : TEST_INSTALLATION_ID . to_string ( ) ,
1084+ environment_id : Some ( "env_test" . to_string ( ) ) ,
1085+ } ,
1086+ )
1087+ . await ;
1088+
1089+ first_websocket
1090+ . close ( None )
1091+ . await
1092+ . expect ( "first websocket should close" ) ;
1093+ assert_eq ! (
1094+ remote_handle. enable( ) . expect( "enable should succeed" ) ,
1095+ RemoteControlStatusChangedNotification {
1096+ status: RemoteControlConnectionStatus :: Connecting ,
1097+ server_name: test_server_name( ) ,
1098+ installation_id: TEST_INSTALLATION_ID . to_string( ) ,
1099+ environment_id: Some ( "env_test" . to_string( ) ) ,
1100+ }
1101+ ) ;
1102+
1103+ let mut second_websocket = accept_remote_control_connection ( & listener) . await ;
1104+ expect_remote_control_status (
1105+ & mut status_rx,
1106+ /*expected_status*/ None ,
1107+ Some ( "env_test" ) ,
1108+ )
1109+ . await ;
1110+ timeout ( Duration :: from_millis ( 100 ) , listener. accept ( ) )
1111+ . await
1112+ . expect_err ( "stale reconnect request should not trigger a third connection" ) ;
1113+ second_websocket
1114+ . close ( None )
1115+ . await
1116+ . expect ( "second websocket should close" ) ;
1117+
1118+ shutdown_token. cancel ( ) ;
1119+ timeout ( Duration :: from_secs ( 1 ) , remote_task)
1120+ . await
1121+ . expect ( "remote control task should stop after shutdown" )
1122+ . expect ( "remote control task should join" ) ;
1123+ }
1124+
9461125#[ tokio:: test]
9471126async fn remote_control_transport_clears_outgoing_buffer_when_backend_acks ( ) {
9481127 let listener = TcpListener :: bind ( "127.0.0.1:0" )
0 commit comments