@@ -51,7 +51,7 @@ impl RemoteControlCommand {
5151
5252#[ derive( Debug , Clone , Copy , clap:: Subcommand ) ]
5353enum RemoteControlSubcommand {
54- /// Start the app-server daemon with remote control enabled.
54+ /// Start the app-server daemon with remote control enabled and print a pairing code .
5555 Start ,
5656
5757 /// Stop the app-server daemon.
@@ -80,7 +80,9 @@ pub(crate) async fn run(
8080 "Starting app-server daemon with remote control enabled..." ,
8181 ) ?;
8282 let output = codex_app_server_daemon:: ensure_remote_control_ready ( ) . await ?;
83- print_remote_control_start_output ( & output, command. json ) ?;
83+ ensure_remote_control_startable ( & output. remote_control ) ?;
84+ let pairing = codex_app_server_daemon:: start_remote_control_pairing ( ) . await ?;
85+ print_remote_control_start_output ( & output, Some ( & pairing) , command. json ) ?;
8486 }
8587 Some ( RemoteControlSubcommand :: Stop ) => {
8688 print_remote_control_progress ( command. json , "Stopping remote control..." ) ?;
@@ -143,7 +145,7 @@ async fn run_foreground_remote_control(
143145
144146 let summary = match wait_for_foreground_remote_control_start (
145147 & mut app_server_task,
146- wait_for_foreground_remote_control_ready ( socket_path) ,
148+ wait_for_foreground_remote_control_ready ( socket_path. clone ( ) ) ,
147149 stop_rx. clone ( ) ,
148150 )
149151 . await
@@ -171,7 +173,26 @@ async fn run_foreground_remote_control(
171173 return Ok ( ( ) ) ;
172174 }
173175
174- if let Err ( error) = print_foreground_ready_output ( & summary, json) {
176+ if let Err ( error) = ensure_remote_control_startable ( & summary) {
177+ abort_foreground_app_server ( app_server_task) . await ;
178+ stop_signal_task. abort ( ) ;
179+ return Err ( error) ;
180+ }
181+
182+ let pairing = match codex_app_server_daemon:: start_remote_control_pairing_on_socket (
183+ socket_path. as_path ( ) ,
184+ )
185+ . await
186+ {
187+ Ok ( pairing) => pairing,
188+ Err ( error) => {
189+ abort_foreground_app_server ( app_server_task) . await ;
190+ stop_signal_task. abort ( ) ;
191+ return Err ( error) ;
192+ }
193+ } ;
194+
195+ if let Err ( error) = print_foreground_ready_output ( & summary, Some ( & pairing) , json) {
175196 abort_foreground_app_server ( app_server_task) . await ;
176197 stop_signal_task. abort ( ) ;
177198 return Err ( error) ;
@@ -278,19 +299,21 @@ async fn wait_for_foreground_remote_control_ready(
278299
279300fn print_remote_control_start_output (
280301 output : & AppServerRemoteControlReadyOutput ,
302+ pairing : Option < & RemoteControlPairingStartResponse > ,
281303 json : bool ,
282304) -> anyhow:: Result < ( ) > {
283305 ensure_remote_control_startable ( & output. remote_control ) ?;
284306 if json {
285307 println ! (
286308 "{}" ,
287- serde_json:: to_string( & RemoteControlStartJsonOutput :: daemon( output) ) ?
309+ serde_json:: to_string( & RemoteControlStartJsonOutput :: daemon( output, pairing ) ) ?
288310 ) ;
289311 return Ok ( ( ) ) ;
290312 }
291313
292314 for line in remote_control_start_human_lines (
293315 & output. remote_control ,
316+ pairing,
294317 RemoteControlHumanOutputMode :: Daemon ,
295318 ) ? {
296319 println ! ( "{line}" ) ;
@@ -303,19 +326,23 @@ fn print_remote_control_start_output(
303326
304327fn print_foreground_ready_output (
305328 summary : & AppServerRemoteControlReadyStatus ,
329+ pairing : Option < & RemoteControlPairingStartResponse > ,
306330 json : bool ,
307331) -> anyhow:: Result < ( ) > {
308332 if json {
309333 ensure_remote_control_startable ( summary) ?;
310334 println ! (
311335 "{}" ,
312- serde_json:: to_string( & RemoteControlStartJsonOutput :: foreground( summary) ) ?
336+ serde_json:: to_string( & RemoteControlStartJsonOutput :: foreground( summary, pairing ) ) ?
313337 ) ;
314338 return Ok ( ( ) ) ;
315339 }
316340
317- for line in remote_control_start_human_lines ( summary, RemoteControlHumanOutputMode :: Foreground ) ?
318- {
341+ for line in remote_control_start_human_lines (
342+ summary,
343+ pairing,
344+ RemoteControlHumanOutputMode :: Foreground ,
345+ ) ? {
319346 println ! ( "{line}" ) ;
320347 }
321348 Ok ( ( ) )
@@ -330,6 +357,8 @@ struct RemoteControlStartJsonOutput<'a> {
330357 environment_id : Option < & ' a str > ,
331358 timed_out : bool ,
332359 #[ serde( skip_serializing_if = "Option::is_none" ) ]
360+ pairing : Option < & ' a RemoteControlPairingStartResponse > ,
361+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
333362 daemon : Option < & ' a AppServerRemoteControlStartOutput > ,
334363}
335364
@@ -341,25 +370,33 @@ enum RemoteControlModeJson {
341370}
342371
343372impl < ' a > RemoteControlStartJsonOutput < ' a > {
344- fn foreground ( summary : & ' a AppServerRemoteControlReadyStatus ) -> Self {
373+ fn foreground (
374+ summary : & ' a AppServerRemoteControlReadyStatus ,
375+ pairing : Option < & ' a RemoteControlPairingStartResponse > ,
376+ ) -> Self {
345377 Self {
346378 mode : RemoteControlModeJson :: Foreground ,
347379 status : summary. status ,
348380 server_name : & summary. server_name ,
349381 environment_id : summary. environment_id . as_deref ( ) ,
350382 timed_out : summary. timed_out ,
383+ pairing,
351384 daemon : None ,
352385 }
353386 }
354387
355- fn daemon ( output : & ' a AppServerRemoteControlReadyOutput ) -> Self {
388+ fn daemon (
389+ output : & ' a AppServerRemoteControlReadyOutput ,
390+ pairing : Option < & ' a RemoteControlPairingStartResponse > ,
391+ ) -> Self {
356392 let remote_control = & output. remote_control ;
357393 Self {
358394 mode : RemoteControlModeJson :: Daemon ,
359395 status : remote_control. status ,
360396 server_name : & remote_control. server_name ,
361397 environment_id : remote_control. environment_id . as_deref ( ) ,
362398 timed_out : remote_control. timed_out ,
399+ pairing,
363400 daemon : Some ( & output. daemon ) ,
364401 }
365402 }
@@ -411,9 +448,13 @@ enum RemoteControlHumanOutputMode {
411448
412449fn remote_control_start_human_lines (
413450 summary : & AppServerRemoteControlReadyStatus ,
451+ pairing : Option < & RemoteControlPairingStartResponse > ,
414452 mode : RemoteControlHumanOutputMode ,
415453) -> anyhow:: Result < Vec < String > > {
416454 let mut lines = vec ! [ remote_control_start_human_message( summary) ?] ;
455+ if let Some ( pairing) = pairing {
456+ lines. push ( remote_control_pairing_human_message ( pairing) ?) ;
457+ }
417458 match mode {
418459 RemoteControlHumanOutputMode :: Foreground => {
419460 lines. push ( "Press Ctrl-C to stop." . to_string ( ) ) ;
@@ -476,6 +517,12 @@ fn format_remote_control_pairing_output(
476517 return Ok ( serde_json:: to_string ( output) ?) ;
477518 }
478519
520+ remote_control_pairing_human_message ( output)
521+ }
522+
523+ fn remote_control_pairing_human_message (
524+ output : & RemoteControlPairingStartResponse ,
525+ ) -> anyhow:: Result < String > {
479526 let manual_pairing_code = output
480527 . manual_pairing_code
481528 . as_deref ( )
@@ -585,19 +632,25 @@ mod tests {
585632 }
586633
587634 #[ test]
588- fn remote_control_human_lines_include_foreground_stop_hint_only ( ) {
635+ fn remote_control_human_lines_include_pairing_and_foreground_stop_hint ( ) {
589636 let summary = remote_control_status ( RemoteControlConnectionStatus :: Connected ) ;
637+ let pairing = pairing_response ( Some ( "ABCD-EFGH" ) ) ;
590638
591639 assert_eq ! (
592- remote_control_start_human_lines( & summary, RemoteControlHumanOutputMode :: Foreground )
593- . expect( "foreground lines" ) ,
640+ remote_control_start_human_lines(
641+ & summary,
642+ Some ( & pairing) ,
643+ RemoteControlHumanOutputMode :: Foreground ,
644+ )
645+ . expect( "foreground lines" ) ,
594646 vec![
595647 "This machine is available for remote control as owen-mbp." . to_string( ) ,
648+ "Pairing code: ABCD-EFGH" . to_string( ) ,
596649 "Press Ctrl-C to stop." . to_string( ) ,
597650 ]
598651 ) ;
599652 assert_eq ! (
600- remote_control_start_human_lines( & summary, RemoteControlHumanOutputMode :: Daemon )
653+ remote_control_start_human_lines( & summary, None , RemoteControlHumanOutputMode :: Daemon )
601654 . expect( "daemon lines" ) ,
602655 vec![ "This machine is available for remote control as owen-mbp." . to_string( ) ]
603656 ) ;
@@ -620,9 +673,11 @@ mod tests {
620673 #[ test]
621674 fn remote_control_json_output_marks_foreground_or_daemon ( ) {
622675 let foreground_summary = remote_control_status ( RemoteControlConnectionStatus :: Connected ) ;
676+ let pairing = pairing_response ( Some ( "ABCD-EFGH" ) ) ;
623677 assert_eq ! (
624678 serde_json:: to_value( RemoteControlStartJsonOutput :: foreground(
625- & foreground_summary
679+ & foreground_summary,
680+ Some ( & pairing) ,
626681 ) )
627682 . expect( "foreground JSON" ) ,
628683 json!( {
@@ -631,19 +686,34 @@ mod tests {
631686 "serverName" : "owen-mbp" ,
632687 "environmentId" : "env_test" ,
633688 "timedOut" : false ,
689+ "pairing" : {
690+ "pairingCode" : "pairing-code" ,
691+ "manualPairingCode" : "ABCD-EFGH" ,
692+ "environmentId" : "env_test" ,
693+ "expiresAt" : 1_700_000_000 ,
694+ } ,
634695 } )
635696 ) ;
636697
637698 let daemon_output = daemon_ready_output ( RemoteControlConnectionStatus :: Connected ) ;
638699 assert_eq ! (
639- serde_json:: to_value( RemoteControlStartJsonOutput :: daemon( & daemon_output) )
640- . expect( "daemon JSON" ) ,
700+ serde_json:: to_value( RemoteControlStartJsonOutput :: daemon(
701+ & daemon_output,
702+ Some ( & pairing) ,
703+ ) )
704+ . expect( "daemon JSON" ) ,
641705 json!( {
642706 "mode" : "daemon" ,
643707 "status" : "connected" ,
644708 "serverName" : "owen-mbp" ,
645709 "environmentId" : "env_test" ,
646710 "timedOut" : false ,
711+ "pairing" : {
712+ "pairingCode" : "pairing-code" ,
713+ "manualPairingCode" : "ABCD-EFGH" ,
714+ "environmentId" : "env_test" ,
715+ "expiresAt" : 1_700_000_000 ,
716+ } ,
647717 "daemon" : {
648718 "status" : "started" ,
649719 "pid" : 42 ,
@@ -662,7 +732,8 @@ mod tests {
662732 assert_eq ! (
663733 print_remote_control_start_output(
664734 & daemon_ready_output( RemoteControlConnectionStatus :: Errored ) ,
665- /*json*/ true
735+ /*pairing*/ None ,
736+ /*json*/ true ,
666737 )
667738 . expect_err( "errored daemon status should fail" )
668739 . to_string( ) ,
0 commit comments