From d402acbebac03429169dec70eb977e3c1f5b6f40 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Fri, 20 Nov 2026 14:13:19 +0000 Subject: [PATCH] Reset TUI thread-switch state on reconnect (#62807) ## Why Reconnect replaces the app event queue, so queued thread-switch events can be discarded while their resets are still counted as pending. ## What changed - Clear the pending thread-switch reset count when replacing the event queue and use a shared accessor for the displayed thread during reconnect. - Box the app event dispatch future and extract helpers for choosing the side conversation target and rendering the composer pane. GitOrigin-RevId: 6bb14443952262b6a0df7fdf05db053038c7ce2d --- codex-rs/tui/src/app/event_dispatch.rs | 12 ++++++++-- codex-rs/tui/src/app/reconnect.rs | 7 +++++- codex-rs/tui/src/app/side.rs | 30 ++++++++++++++---------- codex-rs/tui/src/app/startup.rs | 2 +- codex-rs/tui/src/chatwidget/rendering.rs | 7 ++++++ codex-rs/tui/src/pager_overlay.rs | 2 +- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index dc4d5d80e7..c7a0fd2dec 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -30,9 +30,17 @@ use codex_app_server_protocol::WindowsSandboxSetupMode; pub(super) const SHUTDOWN_FIRST_EXIT_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 2); impl App { - // Keep the existing dispatcher layout until it can be formatted separately. + pub(crate) fn handle_event<'a>( + &'a mut self, + tui: &'a mut tui::Tui, + app_server: &'a mut AppServerSession, + event: AppEvent, + ) -> futures::future::LocalBoxFuture<'a, Result> { + Box::pin(self.dispatch_event(tui, app_server, event)) + } + #[rustfmt::skip] - pub(crate) async fn handle_event( + async fn dispatch_event( &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, diff --git a/codex-rs/tui/src/app/reconnect.rs b/codex-rs/tui/src/app/reconnect.rs index 7713faa969..2f633f3395 100644 --- a/codex-rs/tui/src/app/reconnect.rs +++ b/codex-rs/tui/src/app/reconnect.rs @@ -229,6 +229,10 @@ impl App { self.begin_reconnect_inner(/*preserve_startup_input*/ true) } + pub(super) fn reconnect_thread_id(&self) -> Option { + self.current_displayed_thread_id() + } + fn begin_reconnect_inner(&mut self, preserve_startup_input: bool) -> bool { if matches!(self.app_server_target, AppServerTarget::Embedded) { return false; @@ -406,7 +410,7 @@ impl App { .chat_widget .selected_index_for_present_view(agents_overview::AGENTS_OVERVIEW_VIEW_ID) .and_then(|index| self.agents_overview.visible_thread_ids.get(index).copied()); - let displayed = self.current_displayed_thread_id(); + let displayed = self.reconnect_thread_id(); let mut input = self.chat_widget.capture_thread_input_state(); if let Some(input) = input.as_mut() { input.reconnect_pending = true; @@ -418,6 +422,7 @@ impl App { self.app_event_tx = AppEventSender::new(tx); self.app_server_backend_id = Some(session.backend_id()); *app_event_rx = rx; + self.pending_thread_switch_resets = 0; for (parent, pane) in &mut self.sidebars { pane.focus_document_request = None; pane.agents.pending_link = None; diff --git a/codex-rs/tui/src/app/side.rs b/codex-rs/tui/src/app/side.rs index 3e20f832cf..41fb190ed9 100644 --- a/codex-rs/tui/src/app/side.rs +++ b/codex-rs/tui/src/app/side.rs @@ -427,9 +427,13 @@ impl App { if self.overlay.is_none() && self.chat_widget.no_modal_or_popup_active() && self.chat_widget.composer_is_empty() - && let Some(target) = self.current_displayed_thread_id() - && let Some(side) = self.side_threads.get(&target) { + let Some(target) = self.current_displayed_thread_id() else { + return false; + }; + let Some(side) = self.side_threads.get(&target) else { + return false; + }; if matches!(key.code, KeyCode::Char(c) if c.eq_ignore_ascii_case(&'c')) && self.side_retention_enabled(tui) && self.sidebar_chooser_available() @@ -471,6 +475,16 @@ impl App { (target_thread_id == state.parent_thread_id).then_some(active_thread_id) } + pub(super) fn side_conversation_target(&self, active: ThreadId) -> Option { + if let Some((owner, agent)) = self.docked_agent() { + Some(if active == agent { owner } else { agent }) + } else if let Some(state) = self.side_threads.get(&active) { + Some(state.parent_thread_id) + } else { + self.side_thread_for_parent(active) + } + } + pub(super) async fn toggle_side_conversation( &mut self, tui: &mut tui::Tui, @@ -479,17 +493,7 @@ impl App { let Some(active_thread_id) = self.current_displayed_thread_id() else { return Ok(()); }; - let target_thread_id = if let Some((owner, agent)) = self.docked_agent() { - if active_thread_id == agent { - owner - } else { - agent - } - } else if let Some(state) = self.side_threads.get(&active_thread_id) { - state.parent_thread_id - } else if let Some(side_thread_id) = self.side_thread_for_parent(active_thread_id) { - side_thread_id - } else { + let Some(target_thread_id) = self.side_conversation_target(active_thread_id) else { return Ok(()); }; diff --git a/codex-rs/tui/src/app/startup.rs b/codex-rs/tui/src/app/startup.rs index 87e3a862e2..e248c4ac53 100644 --- a/codex-rs/tui/src/app/startup.rs +++ b/codex-rs/tui/src/app/startup.rs @@ -1507,7 +1507,7 @@ See the Codex keymap documentation for supported actions and examples." app.app_server_target.clone(), app.config.clone(), app.local_settings.clone(), - app.current_displayed_thread_id(), + app.reconnect_thread_id(), backends .session_at(displayed_session) .remote_cwd_override() diff --git a/codex-rs/tui/src/chatwidget/rendering.rs b/codex-rs/tui/src/chatwidget/rendering.rs index a32dca44d7..c64ac1afd7 100644 --- a/codex-rs/tui/src/chatwidget/rendering.rs +++ b/codex-rs/tui/src/chatwidget/rendering.rs @@ -334,6 +334,13 @@ impl ChatWidget { /// Both transcript surfaces use this composition so read-only notices and cursor placement /// remain consistent. Owned transcripts reserve their shared hint row above the composer. pub(crate) fn bottom_pane_renderable<'a>( + &'a self, + options: crate::bottom_pane::ComposerRenderOptions<'a>, + ) -> RenderableItem<'a> { + self.composer_pane_renderable(options) + } + + fn composer_pane_renderable<'a>( &'a self, mut options: crate::bottom_pane::ComposerRenderOptions<'a>, ) -> RenderableItem<'a> { diff --git a/codex-rs/tui/src/pager_overlay.rs b/codex-rs/tui/src/pager_overlay.rs index 3fba14ed09..73733244fa 100644 --- a/codex-rs/tui/src/pager_overlay.rs +++ b/codex-rs/tui/src/pager_overlay.rs @@ -48,7 +48,7 @@ pub(crate) enum Overlay { } impl Overlay { - /// Use for input routing. Only the coding transcript should receive live thread history. + /// The transcript currently receiving input, if any. pub(crate) fn input_transcript(&self) -> Option<&TranscriptOverlay> { match self { Self::Transcript(view) => Some(view),