Improve automatic thread naming in the TUI (#42749)

## Why

Automatic title generation used a provisional thread name and only applied the
generated result while its thread remained active. Switching threads could
therefore leave the originating thread without its generated name.

## What changed

- Track title-generation requests by thread so results are persisted for the
  originating thread, while preserving any name set before generation finishes.
- Show a `renaming...` spinner instead of a provisional name, including across
  thread switches and failure paths.
- Add `thread-name` items to the status line and terminal title, and include the
  generated thread name in both default layouts.

## Testing

- Cover automatic naming, manual-name precedence, thread switching, failed
  requests, deduplication, and status-surface rendering and animation.

GitOrigin-RevId: a03ae4849bedec9f62f256fadad72c1685ce46e0
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-04 12:20:57 +00:00
1 parent 8e85265c39
commit a1294e57f1
32 files changed
+642 -181

No files matched your search

+9 -5
View File
@@ -2582,16 +2582,20 @@ impl App {
self.temporary_structured_requests
.remove(&temporary_thread_id);
self.finish_thread_title_generation(thread_id, destination);
match destination {
ThreadTitleDestination::Automatic { expected_title } => {
ThreadTitleDestination::Automatic => {
if let Ok(response) = result
&& let Some(title) = super::thread_title::parse_thread_title(&response)
&& self.chat_widget.thread_id() == Some(thread_id)
&& self.chat_widget.thread_name().as_deref()
== Some(expected_title.as_str())
&& let Ok(thread) = app_server
.thread_read(thread_id, /*include_turns*/ false)
.await
&& thread.name.is_none()
{
match app_server.thread_set_name(thread_id, title.clone()).await {
Ok(()) => self.chat_widget.expect_automatic_thread_name(title),
Ok(()) => self
.chat_widget
.on_thread_name_updated(thread_id, Some(title)),
Err(error) => {
tracing::debug!(%error, "failed to apply generated thread title");
}
+2
View File
@@ -265,6 +265,8 @@ impl App {
self.pending_plugin_enabled_writes.clear();
self.pending_hook_enabled_writes.clear();
self.temporary_structured_requests.clear();
self.pending_thread_titles.clear();
self.sync_thread_title_progress();
self.agents_overview.dispatched_requests.clear();
self.agents_overview.request_id = None;
self.agents_overview.refresh_pending = false;
+2
View File
@@ -552,6 +552,7 @@ See the Codex keymap documentation for supported actions and examples."
windows_sandbox: WindowsSandboxState::default(),
thread_event_channels: HashMap::new(),
temporary_structured_requests: HashMap::new(),
pending_thread_titles: HashSet::new(),
thread_event_listener_tasks: HashMap::new(),
agent_navigation: AgentNavigationState::default(),
agents_overview: Default::default(),
@@ -910,6 +911,7 @@ See the Codex keymap documentation for supported actions and examples."
}
} => {
app.chat_widget.refresh_goal_status_indicator_for_time_tick();
app.chat_widget.refresh_thread_title_progress_for_time_tick();
app.chat_widget.refresh_terminal_title();
AppRunControl::Continue
}
+1
View File
@@ -64,6 +64,7 @@ pub(super) async fn make_test_app() -> App {
windows_sandbox: WindowsSandboxState::default(),
thread_event_channels: HashMap::new(),
temporary_structured_requests: HashMap::new(),
pending_thread_titles: HashSet::new(),
thread_event_listener_tasks: HashMap::new(),
agent_navigation: AgentNavigationState::default(),
agents_overview: Default::default(),
+2
View File
@@ -5513,6 +5513,7 @@ async fn make_test_app() -> App {
windows_sandbox: WindowsSandboxState::default(),
thread_event_channels: HashMap::new(),
temporary_structured_requests: HashMap::new(),
pending_thread_titles: HashSet::new(),
thread_event_listener_tasks: HashMap::new(),
agent_navigation: AgentNavigationState::default(),
agents_overview: Default::default(),
@@ -5598,6 +5599,7 @@ pub(super) async fn make_test_app_with_channels() -> (
windows_sandbox: WindowsSandboxState::default(),
thread_event_channels: HashMap::new(),
temporary_structured_requests: HashMap::new(),
pending_thread_titles: HashSet::new(),
thread_event_listener_tasks: HashMap::new(),
agent_navigation: AgentNavigationState::default(),
agents_overview: Default::default(),
+10 -31
View File
@@ -1883,37 +1883,16 @@ impl App {
let had_active_view = self.chat_widget.has_active_view();
self.handle_thread_event_now_recovering_file_changes(event)
.await;
if let Some(user_message) = automatic_title_user_message {
let expected_title = user_message
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.chars()
.take(super::thread_title::THREAD_TITLE_MAX_CHARS)
.collect::<String>();
if !expected_title.is_empty()
&& let Some(thread_id) = self.active_thread_id
{
match app_server
.thread_set_name(thread_id, expected_title.clone())
.await
{
Ok(()) => {
self.chat_widget
.expect_automatic_thread_name(expected_title.clone());
self.generate_thread_title(
app_server,
thread_id,
ThreadTitleDestination::Automatic { expected_title },
super::thread_title::thread_title_prompt(&user_message),
);
}
Err(error) => {
tracing::debug!(%error, "failed to set provisional thread title");
}
}
}
if let Some(user_message) = automatic_title_user_message
&& !user_message.trim().is_empty()
&& let Some(thread_id) = self.active_thread_id
{
self.generate_thread_title(
app_server,
thread_id,
ThreadTitleDestination::Automatic,
super::thread_title::thread_title_prompt(&user_message),
);
}
if !had_active_view
&& self.chat_widget.has_active_view()
+28
View File
@@ -1,4 +1,7 @@
//! Structured-output schema and normalization for generated TUI thread titles.
//!
//! Automatic titles are persisted only after generation, including for inactive threads;
//! the originating thread's saved name takes precedence over an automatic result.
use super::App;
use super::thread_events::ThreadBufferedEvent;
@@ -31,6 +34,25 @@ struct GeneratedThreadTitle {
}
impl App {
pub(super) fn sync_thread_title_progress(&mut self) {
let pending = self.chat_widget.thread_id().is_some_and(|thread_id| {
self.pending_thread_titles
.iter()
.any(|(id, _)| *id == thread_id)
});
self.chat_widget
.set_thread_title_generation_pending(pending);
}
pub(super) fn finish_thread_title_generation(
&mut self,
thread_id: ThreadId,
destination: ThreadTitleDestination,
) {
self.pending_thread_titles.remove(&(thread_id, destination));
self.sync_thread_title_progress();
}
/// Start a hidden title-generation thread without blocking the UI loop.
pub(super) fn generate_thread_title(
&mut self,
@@ -39,6 +61,10 @@ impl App {
destination: ThreadTitleDestination,
prompt: String,
) {
if !self.pending_thread_titles.insert((thread_id, destination)) {
return;
}
self.sync_thread_title_progress();
let request_handle = app_server.request_handle();
let model = if self.chat_widget.config_ref().model_provider_id == "openai"
&& self.chat_widget.has_chatgpt_account()
@@ -96,6 +122,7 @@ impl App {
Ok(thread_id) => thread_id,
Err(error) => {
tracing::debug!(%error, "failed to start title-generation thread");
self.finish_thread_title_generation(thread_id, destination);
if let ThreadTitleDestination::RenameSuggestion { request_id } = destination {
self.chat_widget.apply_thread_name_suggestion(
thread_id, request_id, /*suggestion*/ None,
@@ -106,6 +133,7 @@ impl App {
};
let Ok(temporary_thread_id) = ThreadId::from_string(&temporary_thread_id_text) else {
self.finish_thread_title_generation(thread_id, destination);
if let ThreadTitleDestination::RenameSuggestion { request_id } = destination {
self.chat_widget
.apply_thread_name_suggestion(thread_id, request_id, /*suggestion*/ None);
+227 -34
View File
@@ -7,12 +7,16 @@ use super::recent_conversation_thread_title_prompt;
use super::thread_title_prompt;
use crate::app::session_lifecycle::ThreadAttachPresentation;
use crate::app::test_support::make_test_app;
use crate::app_command::AppCommand;
use crate::app::thread_events::ThreadBufferedEvent;
use crate::app_event::AppEvent;
use crate::app_event::ThreadTitleDestination;
use crate::app_event_sender::AppEventSender;
use crate::app_server_session::ResumeModelSettings;
use crate::chatwidget::tests::helpers::render_bottom_popup;
use crate::test_support::PathBufExt;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnStatus;
@@ -82,39 +86,97 @@ fn bounds_the_entire_title_prompt_for_dense_unicode() {
}
#[tokio::test]
async fn manual_rename_invalidates_pending_automatic_title_before_notification()
-> color_eyre::Result<()> {
let mut app = make_test_app().await;
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = app_server.start_thread(&app.config).await?;
let thread_id = started.session.thread_id;
app.enqueue_primary_thread_session(started.session, started.turns)
async fn automatic_thread_title_respects_origin_metadata_after_switching() -> color_eyre::Result<()>
{
for (switch_threads, manual_name) in [
(true, None),
(true, Some("Manual title")),
(false, Some("Manual title")),
] {
let mut app = make_test_app().await;
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = app_server.start_thread(&app.config).await?;
let thread_id = started.session.thread_id;
let user_message = serde_json::from_str(
r#"{"type":"message","role":"user","content":[{"type":"input_text","text":"Fix the login timeout"}]}"#,
)?;
app_server
.thread_inject_items(thread_id, vec![user_message])
.await?;
app.enqueue_primary_thread_session(started.session, started.turns)
.await?;
app.pending_thread_titles
.insert((thread_id, ThreadTitleDestination::Automatic));
let mut tui = crate::tui::test_support::make_test_tui()?;
if switch_threads {
let second = app_server.start_thread(&app.config).await?;
let second_id = second.session.thread_id;
app.ensure_thread_channel(second_id)
.store
.lock()
.await
.set_session(second.session, second.turns);
app.select_agent_thread(&mut tui, &mut app_server, second_id)
.await?;
}
if let Some(name) = manual_name {
// The saved name must win even before its notification reaches the widget.
app_server
.thread_set_name(thread_id, name.to_string())
.await?;
}
let displayed_thread = (app.chat_widget.thread_id(), app.chat_widget.thread_name());
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::GeneratedThreadTitle {
thread_id,
temporary_thread_id: codex_protocol::ThreadId::new(),
destination: ThreadTitleDestination::Automatic,
result: Ok(r#"{"title":"Generated title"}"#.to_string()),
},
)
.await?;
app_server
.thread_set_name(thread_id, "Provisional title".to_string())
.await?;
app.chat_widget
.expect_automatic_thread_name("Provisional title".to_string());
app.submit_thread_op(
&mut app_server,
thread_id,
AppCommand::set_thread_name("Manual title".to_string()),
)
.await?;
assert_eq!(
app.chat_widget.thread_name(),
Some("Manual title".to_string())
);
app_server.shutdown().await?;
let expected_name = Some(manual_name.unwrap_or("Generated title").to_string());
assert_eq!(
app_server
.thread_read(thread_id, /*include_turns*/ false)
.await?
.name,
expected_name
);
assert_eq!(
(app.chat_widget.thread_id(), app.chat_widget.thread_name()),
displayed_thread
);
assert!(app.pending_thread_titles.is_empty());
let resumed = app_server
.resume_thread(
&app.local_settings,
app.config.clone(),
thread_id,
ResumeModelSettings::PreserveExistingThread,
)
.await?;
assert_eq!(resumed.session.thread_name, expected_name);
app_server.shutdown().await?;
}
Ok(())
}
#[tokio::test]
async fn slash_rename_generates_editable_title_through_embedded_app_server()
-> color_eyre::Result<()> {
check_thread_title_generation(/*automatic*/ false).await
}
#[tokio::test]
async fn automatic_thread_title_generates_without_a_provisional_name() -> color_eyre::Result<()> {
check_thread_title_generation(/*automatic*/ true).await
}
async fn check_thread_title_generation(automatic: bool) -> color_eyre::Result<()> {
let server = wiremock::MockServer::start().await;
let response = responses::mount_sse_once(
&server,
@@ -186,10 +248,37 @@ async fn slash_rename_generates_editable_title_through_embedded_app_server()
});
while event_rx.try_recv().is_ok() {}
app.chat_widget.apply_external_edit("/rename".to_string());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Generating"));
if automatic {
for _ in 0..2 {
app.handle_active_thread_event(
&mut tui,
&mut app_server,
ThreadBufferedEvent::Notification(Box::new(ServerNotification::ItemCompleted(
ItemCompletedNotification {
thread_id: thread_id.to_string(),
turn_id: "existing-turn".to_string(),
item: title_user_message("user-message", "Fix the login timeout"),
completed_at_ms: 0,
},
))),
)
.await?;
}
assert_eq!(app.chat_widget.thread_name(), None);
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
assert_eq!(
app_server
.thread_read(thread_id, /*include_turns*/ false)
.await?
.name,
None
);
} else {
app.chat_widget.apply_external_edit("/rename".to_string());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Generating"));
}
enum TitleDriveEvent {
Ui(Box<AppEvent>),
@@ -220,10 +309,34 @@ async fn slash_rename_generates_editable_title_through_embedded_app_server()
}
}
let popup = render_bottom_popup(&app.chat_widget, /*width*/ 80);
assert!(popup.contains("Fix login timeout"));
assert!(!popup.contains("Generating a title suggestion"));
if automatic {
assert_eq!(
app.chat_widget.thread_name(),
Some("Fix login timeout".to_string())
);
assert_eq!(
app_server
.thread_read(thread_id, /*include_turns*/ false)
.await?
.name,
Some("Fix login timeout".to_string())
);
assert!(app.pending_thread_titles.is_empty());
} else {
let popup = render_bottom_popup(&app.chat_widget, /*width*/ 80);
assert!(popup.contains("Fix login timeout"));
assert!(!popup.contains("Generating a title suggestion"));
assert_eq!(
app_server
.thread_read(thread_id, /*include_turns*/ false)
.await?
.name,
None
);
}
assert!(app.temporary_structured_requests.is_empty());
assert!(app.pending_thread_titles.is_empty());
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
let request = response.single_request();
assert!(
@@ -482,3 +595,83 @@ fn title_agent_message(id: &str, text: &str, phase: Option<MessagePhase>) -> Thr
questions: None,
}
}
#[tokio::test]
async fn thread_title_progress_clears_failed_requests_and_follows_thread_switches()
-> color_eyre::Result<()> {
let mut app = make_test_app().await;
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = app_server.start_thread(&app.config).await?;
let thread_id = started.session.thread_id;
app.enqueue_primary_thread_session(started.session, started.turns)
.await?;
let suggestion = ThreadTitleDestination::RenameSuggestion {
request_id: uuid::Uuid::new_v4(),
};
app.pending_thread_titles.extend([
(thread_id, ThreadTitleDestination::Automatic),
(thread_id, suggestion),
]);
app.sync_thread_title_progress();
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
app.on_thread_title_started(
&app_server,
thread_id,
ThreadTitleDestination::Automatic,
"prompt".to_string(),
/*effort*/ None,
Err("startup failed".to_string()),
);
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
app.on_thread_title_started(
&app_server,
thread_id,
suggestion,
"prompt".to_string(),
/*effort*/ None,
Ok("invalid-thread-id".to_string()),
);
assert!(app.pending_thread_titles.is_empty());
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
let mut tui = crate::tui::test_support::make_test_tui()?;
app.pending_thread_titles
.insert((thread_id, ThreadTitleDestination::Automatic));
let second = app_server.start_thread(&app.config).await?;
let second_id = second.session.thread_id;
app.ensure_thread_channel(second_id)
.store
.lock()
.await
.set_session(second.session, second.turns);
app.select_agent_thread(&mut tui, &mut app_server, second_id)
.await?;
app.render_chat_widget_frame(
&mut tui,
ratatui::layout::Size::new(/*width*/ 120, /*height*/ 30),
)?;
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
app.select_agent_thread(&mut tui, &mut app_server, thread_id)
.await?;
app.render_chat_widget_frame(
&mut tui,
ratatui::layout::Size::new(/*width*/ 120, /*height*/ 30),
)?;
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
app.handle_event(
&mut tui,
&mut app_server,
AppEvent::GeneratedThreadTitle {
thread_id,
temporary_thread_id: codex_protocol::ThreadId::new(),
destination: ThreadTitleDestination::Automatic,
result: Err("generation failed".to_string()),
},
)
.await?;
assert!(app.pending_thread_titles.is_empty());
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 120).contains("renaming..."));
app_server.shutdown().await?;
Ok(())
}