Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Clear pending async question notifications when turns end (#47424)

Ian MacLeod408a77dc1a1c
diff --git a/codex-rs/tui/src/chatwidget/protocol.rs b/codex-rs/tui/src/chatwidget/protocol.rs
index 66e1109ef4..ba0d2e4061 100644
--- a/codex-rs/tui/src/chatwidget/protocol.rs
+++ b/codex-rs/tui/src/chatwidget/protocol.rs
@@ -455,7 +455,7 @@ impl ChatWidget {
self.speak_completed_realtime_delegation(&notification.turn.id, item);
}
if replay_kind.is_none() {
- question_drafts = self.bottom_pane.take_question_drafts();
+ question_drafts = self.take_question_drafts();
}
self.last_non_retry_error = None;
let completion = self.completion_cell(&notification.turn, replay_kind);
@@ -467,7 +467,7 @@ impl ChatWidget {
}
TurnStatus::Interrupted => {
if replay_kind.is_none() {
- question_drafts = self.bottom_pane.take_question_drafts();
+ question_drafts = self.take_question_drafts();
}
self.last_non_retry_error = None;
let reason = if self
@@ -482,7 +482,7 @@ impl ChatWidget {
}
TurnStatus::Failed => {
if replay_kind.is_none() {
- question_drafts = self.bottom_pane.take_question_drafts();
+ question_drafts = self.take_question_drafts();
}
if let Some(error) = notification.turn.error {
if replay_kind.is_none()
diff --git a/codex-rs/tui/src/chatwidget/questions.rs b/codex-rs/tui/src/chatwidget/questions.rs
index 682ccf9423..37e32211dd 100644
--- a/codex-rs/tui/src/chatwidget/questions.rs
+++ b/codex-rs/tui/src/chatwidget/questions.rs
@@ -6,6 +6,16 @@ use crate::bottom_pane::QuestionSubmission;
use codex_protocol::items::AsyncUserInputQuestion;
impl ChatWidget {
+ pub(super) fn take_question_drafts(&mut self) -> Option<Vec<String>> {
+ if matches!(
+ self.pending_notification,
+ Some(Notification::AsyncQuestion { .. })
+ ) {
+ self.pending_notification = None;
+ }
+ self.bottom_pane.take_question_drafts()
+ }
+
pub(super) fn add_async_questions(
&mut self,
message_id: &str,
diff --git a/codex-rs/tui/src/chatwidget/tests/app_server.rs b/codex-rs/tui/src/chatwidget/tests/app_server.rs
index 809d3d59c1..c96ce55ca4 100644
--- a/codex-rs/tui/src/chatwidget/tests/app_server.rs
+++ b/codex-rs/tui/src/chatwidget/tests/app_server.rs
@@ -1590,6 +1590,10 @@ async fn live_app_server_turn_completion_repairs_dropped_message_deltas() {
.collect::<Vec<_>>();
assert_eq!(consolidations.len(), 1);
assert_eq!(chat.bottom_pane.question_editor().unanswered_count(), 0);
+ assert!(matches!(
+ chat.pending_notification,
+ Some(Notification::AgentTurnComplete { .. })
+ ));
}
#[tokio::test]
diff --git a/codex-rs/tui/src/chatwidget/tests/question_notifications_tests.rs b/codex-rs/tui/src/chatwidget/tests/question_notifications_tests.rs
index a47708f382..7f88c36863 100644
--- a/codex-rs/tui/src/chatwidget/tests/question_notifications_tests.rs
+++ b/codex-rs/tui/src/chatwidget/tests/question_notifications_tests.rs
@@ -98,3 +98,51 @@ async fn async_question_notification_summarizes_batches_and_bounds_long_titles()
@"Question: Which environment should we..."
);
}
+
+#[tokio::test]
+async fn async_question_notification_expires_when_turn_ends() {
+ let mut outcomes = Vec::new();
+ for (status, expected) in [
+ (AppServerTurnStatus::Completed, Some("Agent turn complete")),
+ (AppServerTurnStatus::Interrupted, None),
+ (AppServerTurnStatus::Failed, None),
+ ] {
+ let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
+ handle_turn_started(&mut chat, "turn");
+ chat.add_async_questions(
+ "question",
+ &[AsyncUserInputQuestion {
+ title: "Which way?".into(),
+ options: None,
+ }],
+ );
+ chat.handle_server_notification(
+ ServerNotification::TurnCompleted(TurnCompletedNotification {
+ thread_id: "thread".into(),
+ turn: app_server_turn(
+ "ended-turn",
+ status.clone(),
+ /*duration_ms*/ None,
+ /*error*/ None,
+ ),
+ }),
+ /*replay_kind*/ None,
+ );
+ assert_eq!(
+ (
+ chat.pending_notification
+ .as_ref()
+ .map(Notification::display),
+ chat.bottom_pane.question_editor().unanswered_count(),
+ ),
+ (expected.map(str::to_string), 0),
+ );
+ outcomes.push((
+ status,
+ chat.pending_notification
+ .as_ref()
+ .map(Notification::display),
+ ));
+ }
+ insta::assert_debug_snapshot!("async_question_turn_end_notifications", outcomes);
+}
diff --git a/codex-rs/tui/src/chatwidget/tests/question_turn_end_tests.rs b/codex-rs/tui/src/chatwidget/tests/question_turn_end_tests.rs
index 255330f065..10f26dacae 100644
--- a/codex-rs/tui/src/chatwidget/tests/question_turn_end_tests.rs
+++ b/codex-rs/tui/src/chatwidget/tests/question_turn_end_tests.rs
@@ -108,6 +108,10 @@ async fn question_turn_end_recovers_collapsed_drafts_on_completion_and_failure()
.queued_user_messages
.push_back(UserMessage::from("queued prompt").into());
handle_error(&mut chat, "Turn failed", /*codex_error_info*/ None);
+ insta::assert_debug_snapshot!(
+ chat.pending_notification.as_ref().map(Notification::display),
+ @"None"
+ );
assert!(matches!(ops.try_recv().unwrap(), Op::UserTurn { .. }));
assert_eq!(
chat.capture_thread_input_state()
diff --git a/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__question_notifications_tests__async_question_turn_end_notifications.snap b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__question_notifications_tests__async_question_turn_end_notifications.snap
new file mode 100644
index 0000000000..6fade921c9
--- /dev/null
+++ b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__question_notifications_tests__async_question_turn_end_notifications.snap
@@ -0,0 +1,21 @@
+---
+source: tui/src/chatwidget/tests/question_notifications_tests.rs
+assertion_line: 142
+expression: outcomes
+---
+[
+ (
+ Completed,
+ Some(
+ "Agent turn complete",
+ ),
+ ),
+ (
+ Interrupted,
+ None,
+ ),
+ (
+ Failed,
+ None,
+ ),
+]
diff --git a/codex-rs/tui/src/chatwidget/turn_runtime.rs b/codex-rs/tui/src/chatwidget/turn_runtime.rs
index c13da80028..bad721b9ca 100644
--- a/codex-rs/tui/src/chatwidget/turn_runtime.rs
+++ b/codex-rs/tui/src/chatwidget/turn_runtime.rs
@@ -479,7 +479,7 @@ impl ChatWidget {
let question_drafts = if self.thread_usage.replaying_turn_completion {
None
} else {
- self.bottom_pane.take_question_drafts()
+ self.take_question_drafts()
};
if codex_error_info == Some(AppServerCodexErrorInfo::MisalignmentPolicyViolation) {
self.on_misalignment_policy_violation();