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
This commit is contained in:
Felipe Coury
2026-11-20 17:45:15 +00:00
committed by copyberry
parent bb5b9c6240
commit d402acbeba
6 changed files with 42 additions and 18 deletions
+10 -2
View File
@@ -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<AppRunControl>> {
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,
+6 -1
View File
@@ -229,6 +229,10 @@ impl App {
self.begin_reconnect_inner(/*preserve_startup_input*/ true)
}
pub(super) fn reconnect_thread_id(&self) -> Option<ThreadId> {
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;
+17 -13
View File
@@ -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<ThreadId> {
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(());
};
+1 -1
View File
@@ -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()
+7
View File
@@ -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> {
+1 -1
View File
@@ -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),