fix(tui): bound startup terminal probes (#20654)

## Summary

Bound TUI startup terminal response probes so unsupported terminals
cannot stall startup for multiple seconds.

This replaces the Unix startup uses of crossterm's blocking response
probes with short `/dev/tty` probes that use nonblocking reads and
`poll` with a 100ms timeout. It covers the initial cursor-position
query, keyboard enhancement support detection, and OSC 10/11
default-color detection. The default-color probe uses one shared
deadline for foreground and background instead of allowing two
independent full waits.

The diagnostic mode/trace env vars from the investigation branch are
intentionally not included. The shipped behavior is simply bounded
probing by default, while non-Unix keeps the existing crossterm fallback
path.

## Details

- Add a private `terminal_probe` module for bounded Unix terminal probes
and response parsers.
- Let `custom_terminal::Terminal` accept a caller-provided initial
cursor position so startup can compute it before constructing the
terminal.
- Use bounded cursor, keyboard enhancement, and default-color probes on
Unix startup.
- Preserve default-color cache behavior so a failed attempted query does
not retry forever.

## Validation

- `cd codex-rs && just fmt`
- `cd codex-rs && cargo test -p codex-tui terminal_probe`
- `cd codex-rs && just fix -p codex-tui`
- `cd codex-rs && just argument-comment-lint`
- `git diff --check`
- `git diff --cached --check`

`cd codex-rs && cargo test -p codex-tui` still aborts on the
pre-existing local stack overflow in
`app::tests::discard_side_thread_keeps_local_state_when_server_close_fails`;
I reproduced that same focused failure on `main` before this PR work, so
it is not introduced by this change.

Manual validation in the VM showed the original crossterm path taking
about 2s per unanswered probe, while bounded probing returned in about
100ms per probe.
This commit is contained in:
Felipe Coury authored and GitHub committed 2026-05-02 01:20:57 +00:00
1 parent 9e905528bb
commit 127434cd8b
5 files changed
+676 -17

No files matched your search

+28 -12
View File
@@ -99,12 +99,6 @@ mod imp {
}
self.value
}
fn refresh_with(&mut self, mut init: impl FnMut() -> Option<T>) -> Option<T> {
self.value = init();
self.attempted = true;
self.value
}
}
fn default_colors_cache() -> &'static Mutex<Cache<DefaultColors>> {
@@ -115,7 +109,7 @@ mod imp {
pub(super) fn default_colors() -> Option<DefaultColors> {
let cache = default_colors_cache();
let mut cache = cache.lock().ok()?;
cache.get_or_init_with(|| query_default_colors().unwrap_or_default())
cache.get_or_init_with(query_default_colors)
}
pub(super) fn requery_default_colors() {
@@ -124,14 +118,36 @@ mod imp {
if cache.attempted && cache.value.is_none() {
return;
}
cache.refresh_with(|| query_default_colors().unwrap_or_default());
// Focus events arrive after crossterm's event stream is active. Requery through
// crossterm here so unrelated input stays in crossterm's skipped-event queue instead
// of being consumed by the bounded startup probe's direct tty reads.
let fg = query_foreground_color()
.ok()
.flatten()
.and_then(color_to_tuple);
let bg = query_background_color()
.ok()
.flatten()
.and_then(color_to_tuple);
cache.value = fg.zip(bg).map(|(fg, bg)| DefaultColors { fg, bg });
cache.attempted = true;
}
}
fn query_default_colors() -> std::io::Result<Option<DefaultColors>> {
let fg = query_foreground_color()?.and_then(color_to_tuple);
let bg = query_background_color()?.and_then(color_to_tuple);
Ok(fg.zip(bg).map(|(fg, bg)| DefaultColors { fg, bg }))
/// Queries terminal default colors through the bounded startup probe path.
///
/// The palette cache treats `None` as an attempted-but-unavailable result, so this function
/// collapses I/O errors and missing responses into the same fallback path used for terminals
/// that simply do not support OSC 10/11 queries.
fn query_default_colors() -> Option<DefaultColors> {
crate::terminal_probe::default_colors(crate::terminal_probe::DEFAULT_TIMEOUT)
.ok()
.flatten()
.map(|colors| DefaultColors {
fg: colors.fg,
bg: colors.bg,
})
}
fn color_to_tuple(color: CrosstermColor) -> Option<(u8, u8, u8)> {