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

+51 -21
View File
@@ -24,8 +24,9 @@ use codex_utils_sandbox_summary::summarize_permission_profile;
use super::status_state::TerminalTitleStatusKind;
/// Items shown in the terminal title when the user has not configured a
/// custom selection. Intentionally minimal: activity indicator + project name.
pub(super) const DEFAULT_TERMINAL_TITLE_ITEMS: [&str; 2] = ["activity", "project-name"];
/// custom selection. Unnamed threads omit the name until generation starts.
pub(super) const DEFAULT_TERMINAL_TITLE_ITEMS: [&str; 3] =
["activity", "thread-name", "project-name"];
/// Braille-pattern dot-spinner frames for the terminal title animation.
pub(super) const TERMINAL_TITLE_SPINNER_FRAMES: [&str; 10] =
@@ -388,6 +389,26 @@ impl ChatWidget {
&self,
selections: &StatusSurfaceSelections,
) -> Option<Duration> {
if self.local_settings.tui.animations
&& self.status_state.thread_title_generation_pending
&& (selections.status_line_items.iter().any(|item| {
matches!(
item,
StatusLineItem::ThreadName
| StatusLineItem::ThreadTitle
| StatusLineItem::SessionId
)
}) || selections.terminal_title_items.iter().any(|item| {
matches!(
item,
TerminalTitleItem::ThreadName
| TerminalTitleItem::Thread
| TerminalTitleItem::SessionId
)
}))
{
return Some(TERMINAL_TITLE_SPINNER_INTERVAL);
}
if self.local_settings.tui.animations
&& self.terminal_title_shows_action_required_with_selections(selections)
{
@@ -677,7 +698,7 @@ impl ChatWidget {
/// Returning `None` means "omit this item for now", not "configuration error". Callers rely on
/// this to keep partially available status lines readable while waiting for session, token, or
/// git metadata.
pub(super) fn status_line_value_for_item(&mut self, item: StatusLineItem) -> Option<String> {
pub(super) fn status_line_value(&mut self, item: StatusLineItem) -> Option<String> {
match item {
StatusLineItem::ModelName => Some(self.model_display_name().to_string()),
StatusLineItem::ModelWithReasoning => Some(self.model_with_reasoning_display_name()),
@@ -784,17 +805,14 @@ impl ChatWidget {
}
}),
StatusLineItem::RawOutput => self.raw_output_mode().then(|| "raw output".to_string()),
StatusLineItem::ThreadTitle => self.thread_name.as_ref().map_or_else(
|| self.thread_id.map(|id| id.to_string()),
|name| {
let trimmed = name.trim();
if trimmed.is_empty() {
self.thread_id.map(|id| id.to_string())
} else {
Some(trimmed.to_string())
}
},
),
StatusLineItem::ThreadName => {
self.thread_name.as_deref().and_then(normalize_thread_name)
}
StatusLineItem::ThreadTitle => self
.thread_name
.as_deref()
.and_then(normalize_thread_name)
.or_else(|| self.thread_id.map(|id| id.to_string())),
StatusLineItem::WorkspaceHeadline => self.status_line_workspace_headline.clone(),
StatusLineItem::TaskProgress => self.terminal_title_task_progress(),
}
@@ -819,6 +837,7 @@ impl ChatWidget {
StatusSurfacePreviewItem::TaskProgress => return self.terminal_title_task_progress(),
StatusSurfacePreviewItem::CurrentDir => StatusLineItem::CurrentDir,
StatusSurfacePreviewItem::Hostname => StatusLineItem::Hostname,
StatusSurfacePreviewItem::ThreadName => StatusLineItem::ThreadName,
StatusSurfacePreviewItem::ThreadTitle => StatusLineItem::ThreadTitle,
StatusSurfacePreviewItem::GitBranch => StatusLineItem::GitBranch,
StatusSurfacePreviewItem::PullRequestNumber => StatusLineItem::PullRequestNumber,
@@ -864,9 +883,17 @@ impl ChatWidget {
)),
TerminalTitleItem::Spinner => self.terminal_title_spinner_text_at(now),
TerminalTitleItem::Status => Some(self.run_state_status_text()),
TerminalTitleItem::Thread => self
.status_line_value_for_item(StatusLineItem::ThreadTitle)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 48)),
TerminalTitleItem::Thread | TerminalTitleItem::ThreadName => {
let item = if item == TerminalTitleItem::ThreadName {
StatusLineItem::ThreadName
} else {
StatusLineItem::ThreadTitle
};
let value = self
.status_line_value(item)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 48));
self.with_thread_title_progress(value, now)
}
TerminalTitleItem::GitBranch => self.status_line_branch.as_ref().map(|branch| {
Self::truncate_terminal_title_part(branch.clone(), /*max_chars*/ 32)
}),
@@ -900,9 +927,12 @@ impl ChatWidget {
TerminalTitleItem::EstimatedThreadCost => self
.status_line_value_for_item(StatusLineItem::EstimatedThreadCost)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 32)),
TerminalTitleItem::SessionId => self
.status_line_value_for_item(StatusLineItem::SessionId)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 32)),
TerminalTitleItem::SessionId => {
let value = self
.status_line_value(StatusLineItem::SessionId)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 32));
self.with_thread_title_progress(value, now)
}
TerminalTitleItem::FastMode => self
.status_line_value_for_item(StatusLineItem::FastMode)
.map(|value| Self::truncate_terminal_title_part(value, /*max_chars*/ 32)),
@@ -982,7 +1012,7 @@ impl ChatWidget {
Some(self.terminal_title_spinner_frame_at(now).to_string())
}
fn terminal_title_spinner_frame_at(&self, now: Instant) -> &'static str {
pub(super) fn terminal_title_spinner_frame_at(&self, now: Instant) -> &'static str {
let elapsed = now.saturating_duration_since(self.terminal_title_animation_origin);
let frame_index =
(elapsed.as_millis() / TERMINAL_TITLE_SPINNER_INTERVAL.as_millis()) as usize;