tui/core: partially submit committed request_user_input answers on interrupt

This commit is contained in:
Charles Cunningham
2026-03-02 18:01:21 -08:00
parent 0bb152b01d
commit a5bcf4a7b8
10 changed files with 415 additions and 77 deletions
@@ -33,6 +33,7 @@ use crate::render::renderable::Renderable;
use codex_protocol::protocol::Op;
use codex_protocol::request_user_input::RequestUserInputAnswer;
use codex_protocol::request_user_input::RequestUserInputEvent;
use codex_protocol::request_user_input::RequestUserInputQuestion;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_protocol::user_input::TextElement;
use unicode_width::UnicodeWidthStr;
@@ -710,46 +711,87 @@ impl RequestUserInputOverlay {
}
}
fn answer_for_question(
&self,
idx: usize,
question: &RequestUserInputQuestion,
committed_only: bool,
) -> Option<RequestUserInputAnswer> {
let answer_state = &self.answers[idx];
if committed_only && !answer_state.answer_committed {
return None;
}
let options = question.options.as_ref();
// For option questions we may still produce no selection.
let selected_idx =
if options.is_some_and(|opts| !opts.is_empty()) && answer_state.answer_committed {
answer_state.options_state.selected_idx
} else {
None
};
// Notes are appended as extra answers. For freeform questions, only submit when
// the user explicitly committed the draft.
let notes = if answer_state.answer_committed {
answer_state.draft.text_with_pending().trim().to_string()
} else {
String::new()
};
let selected_label = selected_idx
.and_then(|selected_idx| Self::option_label_for_index(question, selected_idx));
let mut answer_list = selected_label.into_iter().collect::<Vec<_>>();
if !notes.is_empty() {
answer_list.push(format!("user_note: {notes}"));
}
Some(RequestUserInputAnswer {
answers: answer_list,
})
}
fn submit_committed_answers_for_interrupt(&mut self) {
self.confirm_unanswered = None;
let mut answers = HashMap::new();
for (idx, question) in self.request.questions.iter().enumerate() {
if let Some(answer) = self.answer_for_question(idx, question, true) {
answers.insert(question.id.clone(), answer);
}
}
self.app_event_tx
.send(AppEvent::CodexOp(Op::UserInputAnswer {
id: self.request.turn_id.clone(),
response: RequestUserInputResponse {
answers: answers.clone(),
interrupted: true,
},
}));
self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new(
history_cell::RequestUserInputResultCell {
questions: self.request.questions.clone(),
answers,
interrupted: true,
},
)));
}
/// Build the response payload and dispatch it to the app.
fn submit_answers(&mut self) {
self.confirm_unanswered = None;
self.save_current_draft();
let mut answers = HashMap::new();
for (idx, question) in self.request.questions.iter().enumerate() {
let answer_state = &self.answers[idx];
let options = question.options.as_ref();
// For option questions we may still produce no selection.
let selected_idx =
if options.is_some_and(|opts| !opts.is_empty()) && answer_state.answer_committed {
answer_state.options_state.selected_idx
} else {
None
};
// Notes are appended as extra answers. For freeform questions, only submit when
// the user explicitly committed the draft.
let notes = if answer_state.answer_committed {
answer_state.draft.text_with_pending().trim().to_string()
} else {
String::new()
};
let selected_label = selected_idx
.and_then(|selected_idx| Self::option_label_for_index(question, selected_idx));
let mut answer_list = selected_label.into_iter().collect::<Vec<_>>();
if !notes.is_empty() {
answer_list.push(format!("user_note: {notes}"));
if let Some(answer) = self.answer_for_question(idx, question, false) {
answers.insert(question.id.clone(), answer);
}
answers.insert(
question.id.clone(),
RequestUserInputAnswer {
answers: answer_list,
},
);
}
self.app_event_tx
.send(AppEvent::CodexOp(Op::UserInputAnswer {
id: self.request.turn_id.clone(),
response: RequestUserInputResponse {
answers: answers.clone(),
interrupted: false,
},
}));
self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new(
@@ -1005,9 +1047,7 @@ impl BottomPaneView for RequestUserInputOverlay {
self.clear_notes_and_focus_options();
return;
}
// TODO: Emit interrupted request_user_input results (including committed answers)
// once core supports persisting them reliably without follow-up turn issues.
self.app_event_tx.send(AppEvent::CodexOp(Op::Interrupt));
self.submit_committed_answers_for_interrupt();
self.done = true;
return;
}
@@ -1221,9 +1261,7 @@ impl BottomPaneView for RequestUserInputOverlay {
fn on_ctrl_c(&mut self) -> CancellationEvent {
if self.confirm_unanswered_active() {
self.close_unanswered_confirmation();
// TODO: Emit interrupted request_user_input results (including committed answers)
// once core supports persisting them reliably without follow-up turn issues.
self.app_event_tx.send(AppEvent::CodexOp(Op::Interrupt));
self.submit_committed_answers_for_interrupt();
self.done = true;
return CancellationEvent::Handled;
}
@@ -1232,9 +1270,7 @@ impl BottomPaneView for RequestUserInputOverlay {
return CancellationEvent::Handled;
}
// TODO: Emit interrupted request_user_input results (including committed answers)
// once core supports persisting them reliably without follow-up turn issues.
self.app_event_tx.send(AppEvent::CodexOp(Op::Interrupt));
self.submit_committed_answers_for_interrupt();
self.done = true;
CancellationEvent::Handled
}
@@ -1298,16 +1334,28 @@ mod tests {
(AppEventSender::new(tx_raw), rx)
}
fn expect_interrupt_only(rx: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>) {
let event = rx.try_recv().expect("expected interrupt AppEvent");
fn expect_partial_interrupt_submission(
rx: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
expected_turn_id: &str,
) -> RequestUserInputResponse {
let event = rx.try_recv().expect("expected partial answer AppEvent");
let AppEvent::CodexOp(op) = event else {
panic!("expected CodexOp");
};
assert_eq!(op, Op::Interrupt);
let Op::UserInputAnswer { id, response } = op else {
panic!("expected UserInputAnswer");
};
assert_eq!(id, expected_turn_id);
assert!(response.interrupted, "expected interrupted response");
let event = rx.try_recv().expect("expected history cell");
assert!(matches!(event, AppEvent::InsertHistoryCell(_)));
assert!(
rx.try_recv().is_err(),
"unexpected AppEvents before interrupt completion"
"unexpected AppEvents after interrupted submission"
);
response
}
fn question_with_options(id: &str, header: &str) -> RequestUserInputQuestion {
@@ -1529,7 +1577,8 @@ mod tests {
overlay.handle_key_event(KeyEvent::from(KeyCode::Esc));
assert!(overlay.done, "expected overlay to be done");
expect_interrupt_only(&mut rx);
let response = expect_partial_interrupt_submission(&mut rx, "turn-1");
assert!(response.answers.is_empty(), "expected no committed answers");
}
#[test]
@@ -1910,7 +1959,11 @@ mod tests {
overlay.handle_key_event(KeyEvent::from(KeyCode::Esc));
assert_eq!(overlay.done, true);
expect_interrupt_only(&mut rx);
let response = expect_partial_interrupt_submission(&mut rx, "turn-1");
assert!(
response.answers.is_empty(),
"expected no committed answers for empty freeform question"
);
}
#[test]
@@ -1927,7 +1980,8 @@ mod tests {
overlay.handle_key_event(KeyEvent::from(KeyCode::Esc));
assert_eq!(overlay.done, true);
expect_interrupt_only(&mut rx);
let response = expect_partial_interrupt_submission(&mut rx, "turn-1");
assert!(response.answers.is_empty(), "expected no committed answers");
}
#[test]
@@ -1988,7 +2042,7 @@ mod tests {
}
#[test]
fn esc_drops_committed_answers() {
fn esc_submits_only_committed_answers_before_interrupt() {
let (tx, mut rx) = test_sender();
let mut overlay = RequestUserInputOverlay::new(
request_event(
@@ -2012,7 +2066,37 @@ mod tests {
overlay.handle_key_event(KeyEvent::from(KeyCode::Esc));
expect_interrupt_only(&mut rx);
let response = expect_partial_interrupt_submission(&mut rx, "turn-1");
let answer = response
.answers
.get("q1")
.expect("missing committed answer");
assert_eq!(answer.answers, vec!["Option 1".to_string()]);
assert!(
!response.answers.contains_key("q2"),
"uncommitted freeform question should not be submitted"
);
}
#[test]
fn esc_does_not_submit_uncommitted_freeform_text() {
let (tx, mut rx) = test_sender();
let mut overlay = RequestUserInputOverlay::new(
request_event("turn-1", vec![question_without_options("q1", "Notes")]),
tx,
true,
false,
false,
);
overlay.handle_key_event(KeyEvent::from(KeyCode::Char('x')));
overlay.handle_key_event(KeyEvent::from(KeyCode::Esc));
let response = expect_partial_interrupt_submission(&mut rx, "turn-1");
assert!(
response.answers.is_empty(),
"uncommitted freeform text should not be submitted"
);
}
#[test]