Harden TUI startup input handling (#38641)

## Why

Terminal probes and other bootstrap work can leave keys or partial control
sequences buffered before an interactive startup screen is visible. Those
inputs must not accidentally select or confirm an action, while typeahead
intended for the composer should survive terminal initialization.

## What changed

- Replay user input consumed by Unix startup probes through Crossterm while
  filtering completed terminal color replies, and avoid consuming the Windows
  console input queue when detecting default colors.
- Drain decoded and unread input after rendering actionable startup screens,
  including onboarding trust, migration, update, provider, resume, and hooks
  prompts. Keep incomplete control sequences quarantined and fail closed when
  an input boundary cannot be resolved.
- Restore terminal modes if initialization exits early and bound terminal probe
  reads by time and byte limits.

## Testing

Add parser, boundary, onboarding, and PTY coverage for preserved typeahead,
split escape sequences, bracketed paste, delayed input, and fresh input after
an interactive screen becomes ready.

GitOrigin-RevId: 321198996cdd88fe9b43c4a762e750bff450482c
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-08-14 20:23:49 +00:00
1 parent 7e65e4f330
commit 58d2daba45
23 files changed
+939 -199

No files matched your search

@@ -25,6 +25,7 @@ pub(crate) enum ExternalAgentConfigMigrationFlowOutcome {
Started(Vec<Line<'static>>),
NoItems,
Cancelled,
TerminalError(std::io::Error),
}
struct DetectedExternalAgentConfigSource {
@@ -312,10 +313,11 @@ pub(crate) async fn handle_external_agent_config_migration_prompt(
.iter()
.map(|detected| detected.source)
.collect::<Vec<_>>();
let Some(source) = run_external_agent_config_source_prompt(tui, &sources).await else {
return Ok(ExternalAgentConfigMigrationFlowOutcome::Cancelled);
};
source
match run_external_agent_config_source_prompt(tui, &sources).await {
Ok(Some(source)) => source,
Ok(None) => return Ok(ExternalAgentConfigMigrationFlowOutcome::Cancelled),
Err(err) => return Ok(ExternalAgentConfigMigrationFlowOutcome::TerminalError(err)),
}
};
let Some(detected_source) = detected_sources
.into_iter()
@@ -337,7 +339,7 @@ pub(crate) async fn handle_external_agent_config_migration_prompt(
)
.await
{
ExternalAgentConfigMigrationOutcome::Proceed(items) => {
Ok(ExternalAgentConfigMigrationOutcome::Proceed(items)) => {
selected_items = items.clone();
match app_server
.external_agent_config_import(
@@ -367,9 +369,10 @@ pub(crate) async fn handle_external_agent_config_migration_prompt(
}
}
}
ExternalAgentConfigMigrationOutcome::Skip => {
Ok(ExternalAgentConfigMigrationOutcome::Skip) => {
return Ok(ExternalAgentConfigMigrationFlowOutcome::Cancelled);
}
Err(err) => return Ok(ExternalAgentConfigMigrationFlowOutcome::TerminalError(err)),
}
}
}
@@ -84,7 +84,7 @@ pub(crate) async fn run_external_agent_config_migration_prompt(
items: &[ExternalAgentConfigMigrationItem],
selected_items: &[ExternalAgentConfigMigrationItem],
error: Option<&str>,
) -> ExternalAgentConfigMigrationOutcome {
) -> std::io::Result<ExternalAgentConfigMigrationOutcome> {
let mut screen = ExternalAgentConfigMigrationScreen::new(
tui.frame_requester(),
items,
@@ -92,10 +92,14 @@ pub(crate) async fn run_external_agent_config_migration_prompt(
error.map(str::to_owned),
);
let _ = tui.draw(u16::MAX, |frame| {
if let Err(err) = tui.draw(u16::MAX, |frame| {
frame.render_widget_ref(&screen, frame.area());
});
}) {
tracing::warn!("failed to draw config migration prompt: {err}");
return Ok(ExternalAgentConfigMigrationOutcome::Skip);
}
tui.discard_pending_input_before_interactive_screen()?;
let events = tui.event_stream();
tokio::pin!(events);
@@ -117,7 +121,7 @@ pub(crate) async fn run_external_agent_config_migration_prompt(
}
}
screen.outcome()
Ok(screen.outcome())
}
struct ExternalAgentConfigMigrationScreen {
@@ -47,12 +47,16 @@ impl ExternalAgentConfigMigrationSource {
pub(crate) async fn run_external_agent_config_source_prompt(
tui: &mut Tui,
sources: &[ExternalAgentConfigMigrationSource],
) -> Option<ExternalAgentConfigMigrationSource> {
) -> std::io::Result<Option<ExternalAgentConfigMigrationSource>> {
let mut screen = ExternalAgentConfigSourceScreen::new(tui.frame_requester(), sources);
let _ = tui.draw(u16::MAX, |frame| {
if let Err(err) = tui.draw(u16::MAX, |frame| {
frame.render_widget_ref(&screen, frame.area());
});
}) {
tracing::warn!("failed to draw config migration source prompt: {err}");
return Ok(None);
}
tui.discard_pending_input_before_interactive_screen()?;
let events = tui.event_stream();
tokio::pin!(events);
@@ -73,7 +77,7 @@ pub(crate) async fn run_external_agent_config_source_prompt(
}
}
screen.selection()
Ok(screen.selection())
}
struct ExternalAgentConfigSourceScreen {