[2 of 2] Start fresh TUI thread in background (#23176)

## Why

After the terminal-probe work in #23175, fresh-session startup still
waits for `thread/start` before the chat input can become usable. The
chat widget already has the machinery to hold early submissions until a
session is configured, so fresh `thread/start` does not need to stay on
the input-ready hot path.

Refs #16335.

## What

This PR starts fresh app-server threads in a background task, reports
completion through a startup app event, and attaches the primary session
once `thread/start` returns. Resume and fork startup paths remain
synchronous.

## Benchmark

In the local pty startup benchmark, this PR's pre-optimization base
branch, #23175, measured about 152ms median from launch to accepted chat
input. The stacked result measured about 66ms median, for an approximate
additional savings of 85-95ms. For broader context, the original `main`
baseline before either startup optimization was about 250.5ms median. We
also measured Codex 0.117.0 on the same machine at about 64.6ms median,
so the stacked branch is back in the old-startup-time range.

## Stack

1. [#23175: [1 of 2] Optimize TUI startup terminal
probes](https://github.com/openai/codex/pull/23175) — base PR
2. [#23176: [2 of 2] Start fresh TUI thread in
background](https://github.com/openai/codex/pull/23176) — this PR

## Verification

- `cargo test -p codex-tui`
This commit is contained in:
Eric Traut authored and GitHub committed 2026-05-20 10:00:33 -07:00
1 parent d4f842f3b3
commit c0f7e1b99f
13 files changed
+329 -10

No files matched your search

+118
View File
@@ -1,4 +1,7 @@
use super::*;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use pretty_assertions::assert_eq;
#[test]
@@ -132,6 +135,121 @@ fn startup_waiting_gate_not_applied_for_resume_or_fork_session_selection() {
);
}
#[tokio::test]
async fn startup_thread_started_submits_queued_startup_input() {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
app.pending_startup_thread_start = true;
app.chat_widget
.set_queue_submissions_until_session_configured(/*queue*/ true);
app.chat_widget
.apply_external_edit("queued before startup completes".to_string());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert_eq!(
app.chat_widget.queued_user_message_texts(),
vec!["queued before startup completes".to_string()]
);
let mut app_server = Box::pin(crate::start_embedded_app_server_for_picker(
app.chat_widget.config_ref(),
))
.await
.expect("embedded app server");
let thread_id = ThreadId::new();
app.handle_startup_thread_started(
&mut app_server,
Ok(AppServerStartedThread {
session: test_thread_session(thread_id, test_path_buf("/tmp/project")),
turns: Vec::new(),
}),
)
.await
.expect("startup thread should attach");
match next_user_turn_op(&mut op_rx) {
Op::UserTurn { items, .. } => assert_eq!(
items,
vec![UserInput::Text {
text: "queued before startup completes".to_string(),
text_elements: Vec::new(),
}]
),
other => panic!("expected queued startup input submission, got {other:?}"),
}
}
#[tokio::test]
async fn startup_thread_start_failure_returns_error() {
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
app.pending_startup_thread_start = true;
let mut app_server = Box::pin(crate::start_embedded_app_server_for_picker(
app.chat_widget.config_ref(),
))
.await
.expect("embedded app server");
let err = app
.handle_startup_thread_started(&mut app_server, Err("boom".to_string()))
.await
.expect_err("startup thread failure should exit instead of leaving chat unconfigured");
assert!(
err.to_string()
.contains("Failed to start a fresh session through the app server: boom")
);
assert!(!app.pending_startup_thread_start);
assert_eq!(app.primary_thread_id, None);
}
#[test]
fn stale_startup_thread_started_removes_local_routing_state() -> Result<()> {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.thread_stack_size(8 * 1024 * 1024)
.enable_all()
.build()?
.block_on(async {
let mut app = make_test_app().await;
let mut app_server =
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
let primary_thread_id = ThreadId::new();
let stale_thread_id = ThreadId::new();
app.primary_thread_id = Some(primary_thread_id);
app.thread_event_channels.insert(
primary_thread_id,
ThreadEventChannel::new(THREAD_EVENT_CHANNEL_CAPACITY),
);
app.activate_thread_channel(primary_thread_id).await;
app.thread_event_channels.insert(
stale_thread_id,
ThreadEventChannel::new(THREAD_EVENT_CHANNEL_CAPACITY),
);
app.agent_navigation.upsert(
stale_thread_id,
/*agent_nickname*/ None,
/*agent_role*/ None,
/*is_closed*/ false,
);
assert!(app.thread_event_channels.contains_key(&stale_thread_id));
assert!(app.agent_navigation.get(&stale_thread_id).is_some());
app.handle_startup_thread_started(
&mut app_server,
Ok(AppServerStartedThread {
session: test_thread_session(stale_thread_id, test_path_buf("/tmp/project")),
turns: Vec::new(),
}),
)
.await?;
assert!(!app.thread_event_channels.contains_key(&stale_thread_id));
assert_eq!(app.agent_navigation.get(&stale_thread_id), None);
assert_eq!(app.active_thread_id, Some(primary_thread_id));
Ok(())
})
}
#[tokio::test]
async fn ignore_same_thread_resume_reports_noop_for_current_thread() {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;