@@ -4053,88 +4053,99 @@ impl App {
40534053 . await ?
40544054 {
40554055 SessionSelection :: Resume ( target_session) => {
4056- let current_cwd = self . config . cwd . to_path_buf ( ) ;
4057- let resume_cwd = if self . remote_app_server_url . is_some ( ) {
4058- current_cwd. clone ( )
4059- } else {
4060- match crate :: resolve_cwd_for_resume_or_fork (
4061- tui,
4062- & self . config ,
4063- & current_cwd,
4064- target_session. thread_id ,
4065- target_session. path . as_deref ( ) ,
4066- CwdPromptAction :: Resume ,
4067- /*allow_prompt*/ true ,
4068- )
4069- . await ?
4070- {
4071- crate :: ResolveCwdOutcome :: Continue ( Some ( cwd) ) => cwd,
4072- crate :: ResolveCwdOutcome :: Continue ( None ) => current_cwd. clone ( ) ,
4073- crate :: ResolveCwdOutcome :: Exit => {
4074- return Ok ( AppRunControl :: Exit ( ExitReason :: UserRequested ) ) ;
4075- }
4076- }
4077- } ;
4078- let mut resume_config = match self
4079- . rebuild_config_for_resume_or_fallback ( & current_cwd, resume_cwd)
4080- . await
4056+ // If the user selected the already-active thread,
4057+ // treat as a no-op: avoid shutdown_current_thread →
4058+ // unsubscribe → ThreadClosed → immediate-exit, and
4059+ // the unnecessary ChatWidget destroy-and-rebuild that
4060+ // would discard local UI state (composer drafts,
4061+ // pending steers, queued messages, etc.).
4062+ if self . chat_widget . thread_id ( ) . as_ref ( ) != Some ( & target_session. thread_id )
40814063 {
4082- Ok ( cfg) => cfg,
4083- Err ( err) => {
4084- self . chat_widget . add_error_message ( format ! (
4085- "Failed to rebuild configuration for resume: {err}"
4086- ) ) ;
4087- return Ok ( AppRunControl :: Continue ) ;
4088- }
4089- } ;
4090- self . apply_runtime_policy_overrides ( & mut resume_config) ;
4091- let summary = session_summary (
4092- self . chat_widget . token_usage ( ) ,
4093- self . chat_widget . thread_id ( ) ,
4094- self . chat_widget . thread_name ( ) ,
4095- ) ;
4096- match app_server
4097- . resume_thread ( resume_config. clone ( ) , target_session. thread_id )
4098- . await
4099- {
4100- Ok ( resumed) => {
4101- self . shutdown_current_thread ( app_server) . await ;
4102- self . config = resume_config;
4103- tui. set_notification_method ( self . config . tui_notification_method ) ;
4104- self . file_search
4105- . update_search_dir ( self . config . cwd . to_path_buf ( ) ) ;
4106- match self
4107- . replace_chat_widget_with_app_server_thread (
4108- tui, app_server, resumed,
4109- )
4110- . await
4064+ let current_cwd = self . config . cwd . to_path_buf ( ) ;
4065+ let resume_cwd = if self . remote_app_server_url . is_some ( ) {
4066+ current_cwd. clone ( )
4067+ } else {
4068+ match crate :: resolve_cwd_for_resume_or_fork (
4069+ tui,
4070+ & self . config ,
4071+ & current_cwd,
4072+ target_session. thread_id ,
4073+ target_session. path . as_deref ( ) ,
4074+ CwdPromptAction :: Resume ,
4075+ /*allow_prompt*/ true ,
4076+ )
4077+ . await ?
41114078 {
4112- Ok ( ( ) ) => {
4113- if let Some ( summary) = summary {
4114- let mut lines: Vec < Line < ' static > > =
4115- vec ! [ summary. usage_line. clone( ) . into( ) ] ;
4116- if let Some ( command) = summary. resume_command {
4117- let spans = vec ! [
4118- "To continue this session, run " . into( ) ,
4119- command. cyan( ) ,
4120- ] ;
4121- lines. push ( spans. into ( ) ) ;
4079+ crate :: ResolveCwdOutcome :: Continue ( Some ( cwd) ) => cwd,
4080+ crate :: ResolveCwdOutcome :: Continue ( None ) => current_cwd. clone ( ) ,
4081+ crate :: ResolveCwdOutcome :: Exit => {
4082+ return Ok ( AppRunControl :: Exit ( ExitReason :: UserRequested ) ) ;
4083+ }
4084+ }
4085+ } ;
4086+ let mut resume_config = match self
4087+ . rebuild_config_for_resume_or_fallback ( & current_cwd, resume_cwd)
4088+ . await
4089+ {
4090+ Ok ( cfg) => cfg,
4091+ Err ( err) => {
4092+ self . chat_widget . add_error_message ( format ! (
4093+ "Failed to rebuild configuration for resume: {err}"
4094+ ) ) ;
4095+ return Ok ( AppRunControl :: Continue ) ;
4096+ }
4097+ } ;
4098+ self . apply_runtime_policy_overrides ( & mut resume_config) ;
4099+ let summary = session_summary (
4100+ self . chat_widget . token_usage ( ) ,
4101+ self . chat_widget . thread_id ( ) ,
4102+ self . chat_widget . thread_name ( ) ,
4103+ ) ;
4104+ match app_server
4105+ . resume_thread ( resume_config. clone ( ) , target_session. thread_id )
4106+ . await
4107+ {
4108+ Ok ( resumed) => {
4109+ self . shutdown_current_thread ( app_server) . await ;
4110+ self . config = resume_config;
4111+ tui. set_notification_method (
4112+ self . config . tui_notification_method ,
4113+ ) ;
4114+ self . file_search
4115+ . update_search_dir ( self . config . cwd . to_path_buf ( ) ) ;
4116+ match self
4117+ . replace_chat_widget_with_app_server_thread (
4118+ tui, app_server, resumed,
4119+ )
4120+ . await
4121+ {
4122+ Ok ( ( ) ) => {
4123+ if let Some ( summary) = summary {
4124+ let mut lines: Vec < Line < ' static > > =
4125+ vec ! [ summary. usage_line. clone( ) . into( ) ] ;
4126+ if let Some ( command) = summary. resume_command {
4127+ let spans = vec ! [
4128+ "To continue this session, run " . into( ) ,
4129+ command. cyan( ) ,
4130+ ] ;
4131+ lines. push ( spans. into ( ) ) ;
4132+ }
4133+ self . chat_widget . add_plain_history_lines ( lines) ;
41224134 }
4123- self . chat_widget . add_plain_history_lines ( lines) ;
41244135 }
4125- }
4126- Err ( err) => {
4127- self . chat_widget . add_error_message ( format ! (
4136+ Err ( err) => {
4137+ self . chat_widget . add_error_message ( format ! (
41284138 "Failed to attach to resumed app-server thread: {err}"
41294139 ) ) ;
4140+ }
41304141 }
41314142 }
4132- }
4133- Err ( err ) => {
4134- let path_display = target_session . display_label ( ) ;
4135- self . chat_widget . add_error_message ( format ! (
4136- "Failed to resume session from {path_display}: {err}"
4137- ) ) ;
4143+ Err ( err ) => {
4144+ let path_display = target_session . display_label ( ) ;
4145+ self . chat_widget . add_error_message ( format ! (
4146+ "Failed to resume session from {path_display}: {err}"
4147+ ) ) ;
4148+ }
41384149 }
41394150 }
41404151 }
@@ -6215,6 +6226,7 @@ mod tests {
62156226 use crate :: multi_agents:: AgentPickerThreadEntry ;
62166227 use assert_matches:: assert_matches;
62176228
6229+ use codex_app_server_client:: AppServerEvent ;
62186230 use codex_app_server_protocol:: AdditionalFileSystemPermissions ;
62196231 use codex_app_server_protocol:: AdditionalNetworkPermissions ;
62206232 use codex_app_server_protocol:: AdditionalPermissionProfile ;
@@ -10756,6 +10768,55 @@ guardian_approval = true
1075610768 ) ;
1075710769 }
1075810770
10771+ /// Regression test: calling `shutdown_current_thread` on an app-server
10772+ /// thread that the chat_widget considers "current" eventually unsubscribes
10773+ /// the only subscriber, causing the app-server to unload the thread and
10774+ /// broadcast `ThreadClosed`. The same-thread guard in the `/resume` branch
10775+ /// prevents that path from running when the selected session is already
10776+ /// active.
10777+ #[ tokio:: test]
10778+ async fn shutdown_current_thread_emits_thread_closed_and_unloads_active_app_server_thread ( ) {
10779+ let mut app = make_test_app ( ) . await ;
10780+ let mut app_server =
10781+ crate :: start_embedded_app_server_for_picker ( app. chat_widget . config_ref ( ) )
10782+ . await
10783+ . expect ( "embedded app server" ) ;
10784+
10785+ let started = app_server
10786+ . start_thread ( app. chat_widget . config_ref ( ) )
10787+ . await
10788+ . expect ( "start thread" ) ;
10789+ let thread_id = started. session . thread_id ;
10790+ app. chat_widget . handle_thread_session ( started. session ) ;
10791+ assert_eq ! ( app. chat_widget. thread_id( ) , Some ( thread_id) ) ;
10792+
10793+ app. shutdown_current_thread ( & mut app_server) . await ;
10794+
10795+ let closed_thread_id = time:: timeout ( time:: Duration :: from_secs ( 1 ) , async {
10796+ loop {
10797+ match app_server. next_event ( ) . await {
10798+ Some ( AppServerEvent :: ServerNotification ( ServerNotification :: ThreadClosed (
10799+ notification,
10800+ ) ) ) => break notification. thread_id ,
10801+ Some ( _) => { }
10802+ None => panic ! ( "app server disconnected before thread/closed" ) ,
10803+ }
10804+ }
10805+ } )
10806+ . await
10807+ . expect ( "thread/closed notification should arrive after shutdown_current_thread" ) ;
10808+
10809+ assert_eq ! ( closed_thread_id, thread_id. to_string( ) ) ;
10810+
10811+ let read_result = app_server
10812+ . thread_read ( thread_id, /*include_turns*/ false )
10813+ . await ;
10814+ assert ! (
10815+ read_result. is_err( ) ,
10816+ "thread should have been unloaded after shutdown_current_thread"
10817+ ) ;
10818+ }
10819+
1075910820 #[ tokio:: test]
1076010821 async fn shutdown_first_exit_returns_immediate_exit_when_shutdown_submit_fails ( ) {
1076110822 let mut app = make_test_app ( ) . await ;
0 commit comments