Support selectable answers for asynchronous TUI questions (#42894)

## Why

Asynchronous questions can include suggested answers, but the TUI previously displayed only a freeform input. Show those choices and require them to be fully visible before submission.

## What changed

- Render numbered, wrapped choices with a default selection, list navigation, and digit shortcuts. Submit the selected label as the answer.
- Block submission of clipped choices and prompt the user to expand the terminal. Limit option processing to the first 32 entries and discard labels over 512 bytes.
- Preserve freeform input when no usable choices remain, and add contextual submit, skip, and question navigation hints.
- Ignore repeated submit keys so holding a key cannot answer another question.

## Testing

Add state, snapshot, and chat widget tests covering wrapped and clipped choices, constrained layouts, option limits, long answer preservation, oversized answer rejection, key repeats, and conflicting keybindings.

GitOrigin-RevId: 5d3ebadf61da62a9680ba819e6dfcf8302c4e9da
This commit is contained in:
Ian MacLeod
2026-09-05 00:36:20 +00:00
committed by copyberry
parent 218e8df926
commit 07f18d5ff7
17 changed files with 729 additions and 74 deletions
@@ -80,6 +80,41 @@ fn question_count(chat: &ChatWidget) -> usize {
.unanswered_count()
}
#[tokio::test]
async fn selected_answers_preserve_long_labels_and_reject_oversized_submissions() {
for (length, snapshot) in [
(300, "named_question"),
(
codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS,
"oversized_question",
),
] {
let (mut chat, _rx, mut ops) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
let label = format!("{} but do not deploy", "x".repeat(length));
chat.add_async_questions(
"message",
&[question("What next?", Some(vec![label.clone()]))],
);
chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::ALT));
insta::assert_snapshot!(snapshot, render_bottom_popup(&chat, /*width*/ 80));
let saved = question_count(&chat);
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
if length > 512 {
assert_eq!(question_count(&chat), saved);
assert!(ops.try_recv().is_err());
chat.bottom_pane
.handle_paste("x".repeat(codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert_eq!(question_count(&chat), saved);
let rendered = render_bottom_popup(&chat, /*width*/ 80);
insta::assert_snapshot!(rendered.lines().find(|line| line.contains("Answer too long")).unwrap(), @" Answer too long; limit 1048562 characters");
} else {
assert_answer(ops.try_recv().unwrap(), &format!("> What next?\n\n{label}"));
}
}
}
fn open_questions(chat: &mut ChatWidget, options: Option<Vec<String>>) {
chat.thread_id = Some(ThreadId::new());
chat.on_agent_message_item_completed(
@@ -125,12 +160,30 @@ async fn question_queue_key_does_not_steer_the_running_turn() {
assert!(ops.try_recv().is_err());
}
#[tokio::test]
async fn question_key_repeats_do_not_consume_another_question() {
for key in [KeyCode::Enter, KeyCode::Char('1')] {
let (mut chat, _rx, mut ops) = make_chatwidget_manual(/*model_override*/ None).await;
open_questions(&mut chat, Some(vec!["First".into()]));
chat.handle_key_event(KeyEvent::from(key));
ops.try_recv().unwrap();
let mut repeat = KeyEvent::from(key);
repeat.kind = KeyEventKind::Repeat;
chat.handle_key_event(repeat);
assert!(ops.try_recv().is_err());
assert_eq!(question_count(&chat), 1);
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert!(ops.try_recv().is_err());
}
}
#[tokio::test]
async fn single_question_spacing_with_working_status() {
let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
chat.bottom_pane.set_task_running(/*running*/ true);
chat.add_async_questions("single", &[question("Only question?", /*options*/ None)]);
chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::ALT));
chat.bottom_pane.handle_paste("A typed answer".into());
let rendered = render_bottom_popup(&chat, /*width*/ 80);
let rows: Vec<_> = rendered.lines().collect();
let question = rows
@@ -139,6 +192,8 @@ async fn single_question_spacing_with_working_status() {
.unwrap();
assert!(rows[question - 2].contains("Working"));
assert!(rows[question - 1].is_empty());
assert_eq!(rows[question + 2].trim(), "A typed answer");
assert!(rows[question + 3].is_empty());
insta::assert_snapshot!("single_question_working_spacing", rendered);
}
@@ -7,3 +7,5 @@ expression: "render_bottom_popup(&chat, 80)"
Which way?
Type your answer
enter submit ctrl + ] skip ⌥ + ↓ main prompt ⌥ + ↑ next question
@@ -0,0 +1,14 @@
---
source: tui/src/chatwidget/tests/questions_tests.rs
expression: "render_bottom_popup(&chat, 80)"
---
What next?
› 1.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx but do not deploy
enter submit ctrl + ] skip ⌥ + ↓ main prompt
@@ -0,0 +1,9 @@
---
source: tui/src/chatwidget/tests/questions_tests.rs
expression: "render_bottom_popup(&chat, 80)"
---
What next?
Type your answer
enter submit ctrl + ] skip ⌥ + ↓ main prompt
@@ -6,4 +6,6 @@ expression: rendered
Only question?
Type your answer
A typed answer
enter submit ctrl + ] skip ⌥ + ↓ main prompt