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

+37 -29
View File
@@ -1735,12 +1735,14 @@ async fn live_app_server_invalid_thread_name_update_is_ignored() {
}
#[tokio::test]
async fn live_app_server_thread_name_update_shows_resume_hint() {
async fn live_app_server_manual_thread_name_updates_status_surfaces() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id =
ThreadId::from_string("123e4567-e89b-12d3-a456-426614174000").expect("thread id");
chat.thread_id = Some(thread_id);
chat.expect_manual_thread_name(thread_id, "review-fix".to_string());
chat.handle_server_notification(
ServerNotification::ThreadNameUpdated(
codex_app_server_protocol::ThreadNameUpdatedNotification {
@@ -1753,46 +1755,53 @@ async fn live_app_server_thread_name_update_shows_resume_hint() {
assert_eq!(chat.thread_name, Some("review-fix".to_string()));
let cells = drain_insert_history(&mut rx);
assert_eq!(cells.len(), 1);
let rendered = lines_to_single_string(&cells[0]);
assert_chatwidget_snapshot!("thread_name_update_resume_hint", rendered);
let transcript = cells
.iter()
.map(|lines| lines_to_single_string(lines))
.collect::<String>();
let footer = render_bottom_popup(&chat, /*width*/ 80);
let title = chat.last_terminal_title.as_deref().unwrap();
assert_chatwidget_snapshot!(
"manual_thread_name_status_surfaces",
normalize_snapshot_paths(format!(
"transcript:\n{transcript}\nfooter:\n{footer}\ntitle: {title}"
))
);
}
#[tokio::test]
async fn live_app_server_automatic_thread_name_update_is_silent() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id = ThreadId::new();
chat.thread_id = Some(thread_id);
for title in ["Provisional title", "Generated title"] {
chat.expect_automatic_thread_name(title.to_string());
assert_eq!(chat.thread_name(), Some(title.to_string()));
for replay_kind in [
None,
Some(ReplayKind::ThreadSnapshot),
Some(ReplayKind::ResumeInitialMessages),
] {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id = ThreadId::new();
let mut session = configured_thread_session(thread_id);
session.thread_name = Some("Generated title".to_string());
chat.handle_thread_session(session);
drain_insert_history(&mut rx);
chat.handle_server_notification(
ServerNotification::ThreadNameUpdated(
codex_app_server_protocol::ThreadNameUpdatedNotification {
thread_id: thread_id.to_string(),
thread_name: Some(title.to_string()),
thread_name: Some("Generated title".to_string()),
},
),
/*replay_kind*/ None,
replay_kind,
);
assert_eq!(chat.thread_name, Some(title.to_string()));
assert!(drain_insert_history(&mut rx).is_empty());
assert_eq!(chat.thread_name(), Some("Generated title".to_string()));
let transcript = drain_insert_history(&mut rx)
.iter()
.map(|lines| lines_to_single_string(lines))
.collect::<String>();
insta::allow_duplicates! {
assert_snapshot!(transcript, @"");
}
}
chat.handle_server_notification(
ServerNotification::ThreadNameUpdated(
codex_app_server_protocol::ThreadNameUpdatedNotification {
thread_id: thread_id.to_string(),
thread_name: Some("Manual title".to_string()),
},
),
/*replay_kind*/ None,
);
assert_eq!(chat.thread_name, Some("Manual title".to_string()));
assert_eq!(drain_insert_history(&mut rx).len(), 1);
}
#[tokio::test]
@@ -1800,7 +1809,6 @@ async fn live_app_server_manual_thread_name_is_visible_before_notification() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id = ThreadId::new();
chat.thread_id = Some(thread_id);
chat.expect_automatic_thread_name("Provisional title".to_string());
chat.expect_manual_thread_name(thread_id, "Manual title".to_string());
@@ -1818,7 +1826,7 @@ async fn live_app_server_manual_thread_name_is_visible_before_notification() {
);
assert_eq!(chat.thread_name(), Some("Manual title".to_string()));
assert_eq!(drain_insert_history(&mut rx).len(), 1);
assert!(drain_insert_history(&mut rx).is_empty());
}
#[tokio::test]
@@ -0,0 +1,23 @@
---
source: tui/src/chatwidget/tests/terminal_title.rs
expression: "rendered.join(\"\\n\\n\")"
---
name=None, pending=false
status: | 550e8400-e29b-41d4-a716-446655440000 | 550e8400-e29b-41d4-a716-446655440000
title: | 550e8400-e29b-41d4-a716-446655440000 | 550e8400-e29b-41d4-a716-44665...
default title: project
name=None, pending=true
status: renaming... ⠋ | 550e8400-e29b-41d4-a716-446655440000 ⠋ | 550e8400-e29b-41d4-a716-446655440000 ⠋
title: renaming... ⠋ | 550e8400-e29b-41d4-a716-446655440000 ⠋ | 550e8400-e29b-41d4-a716-44665... ⠋
default title: renaming... ⠋ | project
name=Some("Fix login timeout"), pending=true
status: Fix login timeout ⠋ | Fix login timeout ⠋ | 550e8400-e29b-41d4-a716-446655440000 ⠋
title: Fix login timeout ⠋ | Fix login timeout ⠋ | 550e8400-e29b-41d4-a716-44665... ⠋
default title: Fix login timeout ⠋ | project
name=Some("Fix login timeout"), pending=false
status: Fix login timeout | Fix login timeout | 550e8400-e29b-41d4-a716-446655440000
title: Fix login timeout | Fix login timeout | 550e8400-e29b-41d4-a716-44665...
default title: Fix login timeout | project
@@ -239,3 +239,125 @@ async fn terminal_title_activity_indicators_do_not_animate_when_animations_are_d
);
assert!(!chat.should_animate_terminal_title_action_required());
}
#[tokio::test]
async fn thread_title_progress_renders_names_and_fallbacks_on_both_surfaces() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::from_string("550e8400-e29b-41d4-a716-446655440000").unwrap());
chat.local_settings.tui.animations = false;
let mut rendered = Vec::new();
for (name, pending) in [
(None, false),
(None, true),
(Some("Fix login timeout"), true),
(Some("Fix login timeout"), false),
] {
chat.thread_name = name.map(str::to_owned);
chat.set_thread_title_generation_pending(pending);
chat.refresh_status_surfaces();
let status = [
StatusLineItem::ThreadName,
StatusLineItem::ThreadTitle,
StatusLineItem::SessionId,
]
.into_iter()
.map(|item| chat.status_line_value_for_item(item).unwrap_or_default())
.collect::<Vec<_>>()
.join(" | ");
let title = [
TerminalTitleItem::ThreadName,
TerminalTitleItem::Thread,
TerminalTitleItem::SessionId,
]
.into_iter()
.map(|item| {
chat.terminal_title_value_for_item(item, Instant::now())
.unwrap_or_default()
})
.collect::<Vec<_>>()
.join(" | ");
rendered.push(format!(
"name={name:?}, pending={pending}\nstatus: {status}\ntitle: {title}\ndefault title: {}",
chat.last_terminal_title.as_deref().unwrap()
));
}
insta::assert_snapshot!(
"thread_title_generation_status_surfaces",
rendered.join("\n\n")
);
chat.thread_name = Some(" ".to_string());
assert_eq!(
chat.status_line_value_for_item(StatusLineItem::ThreadName),
None
);
assert_eq!(
chat.terminal_title_value_for_item(TerminalTitleItem::ThreadName, Instant::now()),
None
);
}
#[tokio::test]
async fn thread_title_progress_animates_when_main_turn_is_idle() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let (frame_requester, mut draw_rx) = FrameRequester::test_channel();
chat.frame_requester = frame_requester;
chat.local_settings.tui.terminal_title = Some(Vec::new());
chat.local_settings.tui.status_line = Some(vec!["thread-name".to_string()]);
chat.bottom_pane.set_task_running(/*running*/ false);
chat.set_thread_title_generation_pending(/*pending*/ true);
assert!(chat.terminal_title_next_refresh.is_some());
while draw_rx.try_recv().is_ok() {}
let now = Instant::now();
chat.terminal_title_animation_origin = now;
assert_eq!(
chat.terminal_title_value_for_item(TerminalTitleItem::ThreadName, now),
Some("renaming... ⠋".to_string())
);
assert_eq!(
chat.terminal_title_value_for_item(
TerminalTitleItem::ThreadName,
now + Duration::from_millis(/*millis*/ 100)
),
Some("renaming... ⠙".to_string())
);
chat.refresh_thread_title_progress_for_time_tick();
assert!(draw_rx.try_recv().is_ok());
chat.local_settings.tui.status_line = Some(Vec::new());
chat.local_settings.tui.terminal_title = Some(vec!["thread-name".to_string()]);
chat.refresh_status_surfaces();
assert!(chat.terminal_title_next_refresh.is_some());
while draw_rx.try_recv().is_ok() {}
chat.refresh_thread_title_progress_for_time_tick();
assert!(draw_rx.try_recv().is_err());
chat.local_settings.tui.animations = false;
chat.refresh_status_surfaces();
assert!(chat.terminal_title_next_refresh.is_none());
assert_eq!(chat.last_terminal_title, Some("renaming... ⠋".to_string()));
chat.local_settings.tui.animations = true;
chat.set_thread_title_generation_pending(/*pending*/ false);
assert_eq!(chat.last_terminal_title, None);
assert!(chat.terminal_title_next_refresh.is_none());
}
#[tokio::test]
async fn thread_title_progress_preserves_suffix_after_truncation_and_in_default_footer() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.local_settings.tui.animations = false;
chat.set_thread_title_generation_pending(/*pending*/ true);
chat.show_welcome_banner = false;
assert_chatwidget_snapshot!(
"default_footer_generating_thread_title",
normalize_snapshot_paths(render_bottom_popup(&chat, /*width*/ 100))
);
chat.thread_name = Some("Long title ".repeat(/*n*/ 12));
for item in [TerminalTitleItem::ThreadName, TerminalTitleItem::Thread] {
let title = chat
.terminal_title_value_for_item(item, Instant::now())
.unwrap();
assert!(title.ends_with("... ⠋"), "{title}");
assert_eq!(title.chars().count(), 50);
}
}