mirror of
https://github.com/openai/codex.git
synced 2026-11-18 23:02:23 +00:00
Make TUI turn interrupts nonblocking (#35000)
## What changed - Dispatch app-server turn interrupts in the background so the TUI can keep processing thread events while an interrupt is pending. - Coalesce repeated interrupt requests for the same active turn and clear the pending state when the turn completes or the thread closes. - Preserve the retry for stale active-turn IDs, surface interrupt failures as warning notifications instead of exiting the TUI, and reset backtrack state whenever an interrupt is handled. ## Testing - Add an app-server integration test covering nonblocking event handling, repeated-request coalescing, and pending-state cleanup. - Verify that interrupting without an active turn clears backtrack state. GitOrigin-RevId: 9ada247a739e2e0a63b425f75ebd6d054167d825
This commit is contained in:
1 parent
2c92af09cf
commit
62ba648136
5 files changed
+192
-36
No files matched your search
@@ -8,6 +8,9 @@ use super::session_lifecycle::ThreadAttachPresentation;
|
||||
use super::*;
|
||||
use crate::chatwidget::ThreadInputStateRestoreMode;
|
||||
use crate::session_resume::read_session_model;
|
||||
use codex_app_server_protocol::TurnInterruptParams;
|
||||
use codex_app_server_protocol::TurnInterruptResponse;
|
||||
use codex_app_server_protocol::WarningNotification;
|
||||
|
||||
impl App {
|
||||
pub(super) async fn shutdown_current_thread(&mut self, app_server: &mut AppServerSession) {
|
||||
@@ -567,41 +570,71 @@ impl App {
|
||||
) -> Result<bool> {
|
||||
match op {
|
||||
AppCommand::Interrupt => {
|
||||
if let Some(turn_id) = self.active_turn_id_for_thread(thread_id).await {
|
||||
let mut interrupt_turn_id = turn_id;
|
||||
for retried_after_turn_mismatch in [false, true] {
|
||||
match app_server
|
||||
.turn_interrupt(thread_id, interrupt_turn_id.clone())
|
||||
.await
|
||||
{
|
||||
Ok(()) => return Ok(true),
|
||||
Err(error) if !retried_after_turn_mismatch => {
|
||||
let Some(actual_turn_id) = active_turn_interrupt_race(&error)
|
||||
else {
|
||||
return Err(error).wrap_err("turn/interrupt failed in TUI");
|
||||
};
|
||||
if actual_turn_id == interrupt_turn_id {
|
||||
return Err(error).wrap_err("turn/interrupt failed in TUI");
|
||||
}
|
||||
// Review flows can swap the active turn before the TUI processes
|
||||
// the corresponding notification. Retry once with the
|
||||
// server-reported turn id so Ctrl+C/Esc do not fatally exit on that
|
||||
// stale cache, but let lifecycle notifications own the cached
|
||||
// active turn id.
|
||||
interrupt_turn_id = actual_turn_id;
|
||||
}
|
||||
let mut turn_id = self
|
||||
.active_turn_id_for_thread(thread_id)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
let (thread_event_tx, thread_event_store) = {
|
||||
let channel = self.ensure_thread_channel(thread_id);
|
||||
(channel.sender.clone(), Arc::clone(&channel.store))
|
||||
};
|
||||
self.reset_backtrack_state();
|
||||
if !turn_id.is_empty() {
|
||||
let mut store = thread_event_store.lock().await;
|
||||
if store.pending_interrupt_turn_id.as_deref() == Some(turn_id.as_str()) {
|
||||
return Ok(true);
|
||||
}
|
||||
store.pending_interrupt_turn_id = Some(turn_id.clone());
|
||||
}
|
||||
let request_handle = app_server.request_handle();
|
||||
let request_ids = [app_server.next_request_id(), app_server.next_request_id()];
|
||||
tokio::spawn(async move {
|
||||
for (attempt, request_id) in request_ids.into_iter().enumerate() {
|
||||
let result = request_handle
|
||||
.request_typed::<TurnInterruptResponse>(ClientRequest::TurnInterrupt {
|
||||
request_id,
|
||||
params: TurnInterruptParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: turn_id.clone(),
|
||||
},
|
||||
})
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(_) => break,
|
||||
Err(error) => {
|
||||
return Err(error).wrap_err("turn/interrupt failed in TUI");
|
||||
if attempt == 0
|
||||
&& let Some(actual_turn_id) = active_turn_interrupt_race(&error)
|
||||
&& actual_turn_id != turn_id
|
||||
{
|
||||
thread_event_store.lock().await.pending_interrupt_turn_id =
|
||||
Some(actual_turn_id.clone());
|
||||
turn_id = actual_turn_id;
|
||||
continue;
|
||||
}
|
||||
tracing::warn!(error = %error, "turn/interrupt failed in TUI");
|
||||
let notification =
|
||||
ServerNotification::Warning(WarningNotification {
|
||||
thread_id: Some(thread_id.to_string()),
|
||||
message: format!("Failed to interrupt turn: {error}"),
|
||||
});
|
||||
let should_send = {
|
||||
let mut store = thread_event_store.lock().await;
|
||||
store.push_notification_ref(¬ification);
|
||||
store.active
|
||||
};
|
||||
if should_send
|
||||
&& let Err(error) = thread_event_tx
|
||||
.send(ThreadBufferedEvent::Notification(notification))
|
||||
.await
|
||||
{
|
||||
tracing::warn!(error = %error, "thread event channel closed");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable!("interrupt retry loop should return");
|
||||
} else {
|
||||
app_server
|
||||
.startup_interrupt(thread_id)
|
||||
.await
|
||||
.wrap_err("turn/interrupt failed in TUI")?;
|
||||
}
|
||||
});
|
||||
Ok(true)
|
||||
}
|
||||
AppCommand::UserTurn {
|
||||
|
||||
Reference in new issue
Block a user