mirror of
https://github.com/openai/codex.git
synced 2026-11-22 07:13:50 +00:00
[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:
1 parent
d4f842f3b3
commit
c0f7e1b99f
13 files changed
+329
-10
No files matched your search
@@ -355,6 +355,7 @@ pub(crate) struct ChatComposer {
|
||||
attachments: AttachmentState,
|
||||
placeholder_text: String,
|
||||
is_task_running: bool,
|
||||
queue_submissions: bool,
|
||||
/// Slash-command draft staged for local recall after application-level dispatch.
|
||||
///
|
||||
/// This slot is intentionally separate from `ChatComposerHistory` so inline slash commands can
|
||||
@@ -524,6 +525,7 @@ impl ChatComposer {
|
||||
attachments: AttachmentState::default(),
|
||||
placeholder_text,
|
||||
is_task_running: false,
|
||||
queue_submissions: false,
|
||||
pending_slash_command_history: None,
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
next_element_id: 0,
|
||||
@@ -2817,6 +2819,9 @@ impl ChatComposer {
|
||||
now: Instant,
|
||||
) -> (InputResult, bool) {
|
||||
if should_queue {
|
||||
if let Some(pasted) = self.draft.paste_burst.flush_before_modified_input() {
|
||||
self.handle_paste(pasted);
|
||||
}
|
||||
let raw_text = self.draft.textarea.text();
|
||||
let defer_slash_validation =
|
||||
self.should_parse_as_slash_on_dequeue_from_raw_text(raw_text);
|
||||
@@ -3227,13 +3232,13 @@ impl ChatComposer {
|
||||
self.footer.mode = reset_mode_after_activity(self.footer.mode);
|
||||
}
|
||||
if self.queue_keys.is_pressed(key_event)
|
||||
&& (self.is_task_running || !self.is_bang_shell_command())
|
||||
&& (self.is_task_running || self.queue_submissions || !self.is_bang_shell_command())
|
||||
{
|
||||
return self.handle_submission(self.is_task_running);
|
||||
return self.handle_submission(self.is_task_running || self.queue_submissions);
|
||||
}
|
||||
|
||||
if self.submit_keys.is_pressed(key_event) {
|
||||
return self.handle_submission(/*should_queue*/ false);
|
||||
return self.handle_submission(self.queue_submissions);
|
||||
}
|
||||
|
||||
if let KeyEvent {
|
||||
@@ -4143,6 +4148,10 @@ impl ChatComposer {
|
||||
self.is_task_running = running;
|
||||
}
|
||||
|
||||
pub(crate) fn set_queue_submissions(&mut self, queue_submissions: bool) {
|
||||
self.queue_submissions = queue_submissions;
|
||||
}
|
||||
|
||||
pub(crate) fn set_context_window(&mut self, percent: Option<i64>, used_tokens: Option<i64>) {
|
||||
if self.footer.context_window_percent == percent
|
||||
&& self.footer.context_window_used_tokens == used_tokens
|
||||
@@ -7064,6 +7073,49 @@ mod tests {
|
||||
assert_eq!(composer.draft.textarea.text(), "hi\nthere");
|
||||
}
|
||||
|
||||
/// Behavior: startup-pending submissions are queued immediately, so Enter should flush any
|
||||
/// buffered burst text into that queued message instead of turning into a draft newline.
|
||||
#[test]
|
||||
fn queued_submission_flushes_ascii_burst_instead_of_inserting_newline() {
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
|
||||
let (tx, _rx) = unbounded_channel::<AppEvent>();
|
||||
let sender = AppEventSender::new(tx);
|
||||
let mut composer = ChatComposer::new(
|
||||
/*has_input_focus*/ true,
|
||||
sender,
|
||||
/*enhanced_keys_supported*/ false,
|
||||
"Ask Codex to do anything".to_string(),
|
||||
/*disable_paste_burst*/ false,
|
||||
);
|
||||
|
||||
let mut now = Instant::now();
|
||||
let step = Duration::from_millis(1);
|
||||
for ch in ['h', 'i'] {
|
||||
let _ = composer.handle_input_basic_with_time(
|
||||
KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE),
|
||||
now,
|
||||
);
|
||||
now += step;
|
||||
}
|
||||
assert!(composer.is_in_paste_burst());
|
||||
|
||||
let (result, _) = composer.handle_submission_with_time(/*should_queue*/ true, now);
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
InputResult::Queued {
|
||||
text: "hi".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
action: QueuedInputAction::Plain,
|
||||
}
|
||||
);
|
||||
assert!(composer.draft.textarea.text().is_empty());
|
||||
assert!(!composer.is_in_paste_burst());
|
||||
}
|
||||
|
||||
/// Behavior: even if Enter suppression would normally be active for a burst, Enter should
|
||||
/// still dispatch a built-in slash command when the first line begins with `/`.
|
||||
#[test]
|
||||
@@ -8029,6 +8081,40 @@ mod tests {
|
||||
assert!(found_error, "expected error history cell to be sent");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_queues_when_queue_submissions_is_enabled() {
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
|
||||
let (tx, _rx) = unbounded_channel::<AppEvent>();
|
||||
let sender = AppEventSender::new(tx);
|
||||
let mut composer = ChatComposer::new(
|
||||
/*has_input_focus*/ true,
|
||||
sender,
|
||||
/*enhanced_keys_supported*/ false,
|
||||
"Ask Codex to do anything".to_string(),
|
||||
/*disable_paste_burst*/ false,
|
||||
);
|
||||
composer.set_queue_submissions(/*queue_submissions*/ true);
|
||||
composer
|
||||
.draft
|
||||
.textarea
|
||||
.set_text_clearing_elements("queued before session");
|
||||
|
||||
let (result, _needs_redraw) =
|
||||
composer.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
InputResult::Queued {
|
||||
text: "queued before session".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
action: QueuedInputAction::Plain,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_queues_slash_led_prompts_while_task_running_without_validation() {
|
||||
use crossterm::event::KeyCode;
|
||||
|
||||
@@ -999,6 +999,10 @@ impl BottomPane {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_queue_submissions(&mut self, queue_submissions: bool) {
|
||||
self.composer.set_queue_submissions(queue_submissions);
|
||||
}
|
||||
|
||||
/// Hide the status indicator while leaving task-running state untouched.
|
||||
pub(crate) fn hide_status_indicator(&mut self) {
|
||||
if self.status.take().is_some() {
|
||||
|
||||
Reference in new issue
Block a user