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
@@ -6921,6 +6921,7 @@ async fn interrupt_without_active_turn_is_treated_as_handled() {
|
|||||||
app.enqueue_primary_thread_session(started.session, started.turns)
|
app.enqueue_primary_thread_session(started.session, started.turns)
|
||||||
.await
|
.await
|
||||||
.expect("primary thread should be registered");
|
.expect("primary thread should be registered");
|
||||||
|
app.backtrack.primed = true;
|
||||||
let op = AppCommand::interrupt();
|
let op = AppCommand::interrupt();
|
||||||
|
|
||||||
let handled = Box::pin(app.try_submit_active_thread_op_via_app_server(
|
let handled = Box::pin(app.try_submit_active_thread_op_via_app_server(
|
||||||
@@ -6932,6 +6933,7 @@ async fn interrupt_without_active_turn_is_treated_as_handled() {
|
|||||||
.expect("interrupt submission should not fail");
|
.expect("interrupt submission should not fail");
|
||||||
|
|
||||||
assert_eq!(handled, true);
|
assert_eq!(handled, true);
|
||||||
|
assert!(!app.backtrack.primed);
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,6 +149,121 @@ async fn wait_for_turn_completed(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn active_turn_interrupt_is_nonblocking_and_coalesces_repeated_requests() -> Result<()> {
|
||||||
|
let (chunks, release_response) =
|
||||||
|
gated_response_chunks("interrupt-response", ev_completed("interrupt-response"));
|
||||||
|
let (server, _completions) = start_streaming_sse_server(vec![chunks]).await;
|
||||||
|
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||||
|
let codex_home = tempdir()?;
|
||||||
|
std::fs::write(
|
||||||
|
codex_home.path().join("config.toml"),
|
||||||
|
format!(
|
||||||
|
r#"
|
||||||
|
model = "{CURRENT_MODEL}"
|
||||||
|
model_provider = "{MODEL_PROVIDER_ID}"
|
||||||
|
|
||||||
|
[model_providers.{MODEL_PROVIDER_ID}]
|
||||||
|
name = "Interrupt test"
|
||||||
|
base_url = "{}/v1"
|
||||||
|
wire_api = "responses"
|
||||||
|
request_max_retries = 0
|
||||||
|
stream_max_retries = 0
|
||||||
|
"#,
|
||||||
|
server.uri()
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
app.config.codex_home = codex_home.path().to_path_buf().abs();
|
||||||
|
app.config.sqlite_home = codex_home.path().to_path_buf();
|
||||||
|
app.config.model = Some(CURRENT_MODEL.to_string());
|
||||||
|
app.config.model_provider_id = MODEL_PROVIDER_ID.to_string();
|
||||||
|
app.config.model_provider = ModelProviderInfo {
|
||||||
|
name: "Interrupt test".to_string(),
|
||||||
|
base_url: Some(format!("{}/v1", server.uri())),
|
||||||
|
request_max_retries: Some(0),
|
||||||
|
stream_max_retries: Some(0),
|
||||||
|
..ModelProviderInfo::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||||
|
let mut app_server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
|
||||||
|
let started = app_server.start_thread(&app.config).await?;
|
||||||
|
let thread_id = started.session.thread_id;
|
||||||
|
app.replace_chat_widget_with_app_server_thread(
|
||||||
|
&mut tui,
|
||||||
|
started,
|
||||||
|
ThreadAttachPresentation::SessionLineage,
|
||||||
|
/*initial_user_message*/ None,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
while app_event_rx.try_recv().is_ok() {}
|
||||||
|
|
||||||
|
submit_prompt(&mut app, "Keep this turn running until interrupted");
|
||||||
|
let turn = next_user_turn_event(&mut app_event_rx);
|
||||||
|
app.submit_thread_op(&mut app_server, thread_id, turn)
|
||||||
|
.await?;
|
||||||
|
let turn_id = next_turn_started(&mut app, &mut app_server, thread_id).await;
|
||||||
|
drive_until_request_count(
|
||||||
|
&mut app,
|
||||||
|
&mut app_server,
|
||||||
|
&server,
|
||||||
|
/*expected_request_count*/ 1,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let interrupt = AppCommand::interrupt();
|
||||||
|
assert!(
|
||||||
|
app.try_submit_active_thread_op_via_app_server(&mut app_server, thread_id, &interrupt)
|
||||||
|
.await?
|
||||||
|
);
|
||||||
|
let AppServerRequestId::Integer(next_request_id) = app_server.next_request_id() else {
|
||||||
|
unreachable!("embedded app-server request IDs are integers");
|
||||||
|
};
|
||||||
|
assert!(
|
||||||
|
app.try_submit_active_thread_op_via_app_server(&mut app_server, thread_id, &interrupt)
|
||||||
|
.await?
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
app_server.next_request_id(),
|
||||||
|
AppServerRequestId::Integer(next_request_id + 1)
|
||||||
|
);
|
||||||
|
{
|
||||||
|
let store = app.thread_event_channels[&thread_id].store.lock().await;
|
||||||
|
assert_eq!(store.active_turn_id.as_deref(), Some(turn_id.as_str()));
|
||||||
|
assert_eq!(
|
||||||
|
store.pending_interrupt_turn_id.as_deref(),
|
||||||
|
Some(turn_id.as_str())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
while app_event_rx.try_recv().is_ok() {}
|
||||||
|
app.handle_thread_event_now(ThreadBufferedEvent::Notification(
|
||||||
|
ServerNotification::Warning(WarningNotification {
|
||||||
|
thread_id: Some(thread_id.to_string()),
|
||||||
|
message: "event handled while interrupt is pending".to_string(),
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
assert!(matches!(
|
||||||
|
app_event_rx.try_recv(),
|
||||||
|
Ok(AppEvent::InsertHistoryCell(_))
|
||||||
|
));
|
||||||
|
|
||||||
|
wait_for_turn_completed(&mut app, &mut app_server, thread_id).await;
|
||||||
|
assert_eq!(
|
||||||
|
app.thread_event_channels[&thread_id]
|
||||||
|
.store
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.pending_interrupt_turn_id,
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = release_response.send(());
|
||||||
|
app_server.shutdown().await?;
|
||||||
|
server.shutdown().await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn drive_until_request_count(
|
async fn drive_until_request_count(
|
||||||
app: &mut App,
|
app: &mut App,
|
||||||
app_server: &mut AppServerSession,
|
app_server: &mut AppServerSession,
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ pub(super) struct ThreadEventStore {
|
|||||||
pub(super) buffer: VecDeque<ThreadBufferedEvent>,
|
pub(super) buffer: VecDeque<ThreadBufferedEvent>,
|
||||||
pub(super) pending_interactive_replay: PendingInteractiveReplayState,
|
pub(super) pending_interactive_replay: PendingInteractiveReplayState,
|
||||||
pub(super) active_turn_id: Option<String>,
|
pub(super) active_turn_id: Option<String>,
|
||||||
|
pub(super) pending_interrupt_turn_id: Option<String>,
|
||||||
pub(super) input_state: Option<ThreadInputState>,
|
pub(super) input_state: Option<ThreadInputState>,
|
||||||
pub(super) capacity: usize,
|
pub(super) capacity: usize,
|
||||||
pub(super) active: bool,
|
pub(super) active: bool,
|
||||||
@@ -69,6 +70,7 @@ impl ThreadEventStore {
|
|||||||
buffer: VecDeque::new(),
|
buffer: VecDeque::new(),
|
||||||
pending_interactive_replay: PendingInteractiveReplayState::default(),
|
pending_interactive_replay: PendingInteractiveReplayState::default(),
|
||||||
active_turn_id: None,
|
active_turn_id: None,
|
||||||
|
pending_interrupt_turn_id: None,
|
||||||
input_state: None,
|
input_state: None,
|
||||||
capacity,
|
capacity,
|
||||||
active: false,
|
active: false,
|
||||||
@@ -120,13 +122,17 @@ impl ThreadEventStore {
|
|||||||
ServerNotification::TurnStarted(turn) => {
|
ServerNotification::TurnStarted(turn) => {
|
||||||
self.active_turn_id = Some(turn.turn.id.clone());
|
self.active_turn_id = Some(turn.turn.id.clone());
|
||||||
}
|
}
|
||||||
ServerNotification::TurnCompleted(turn)
|
ServerNotification::TurnCompleted(turn) => {
|
||||||
if self.active_turn_id.as_deref() == Some(turn.turn.id.as_str()) =>
|
if self.active_turn_id.as_deref() == Some(turn.turn.id.as_str()) {
|
||||||
{
|
self.active_turn_id = None;
|
||||||
self.active_turn_id = None;
|
}
|
||||||
|
if self.pending_interrupt_turn_id.as_deref() == Some(turn.turn.id.as_str()) {
|
||||||
|
self.pending_interrupt_turn_id = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ServerNotification::ThreadClosed(_) => {
|
ServerNotification::ThreadClosed(_) => {
|
||||||
self.active_turn_id = None;
|
self.active_turn_id = None;
|
||||||
|
self.pending_interrupt_turn_id = None;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ use super::session_lifecycle::ThreadAttachPresentation;
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::chatwidget::ThreadInputStateRestoreMode;
|
use crate::chatwidget::ThreadInputStateRestoreMode;
|
||||||
use crate::session_resume::read_session_model;
|
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 {
|
impl App {
|
||||||
pub(super) async fn shutdown_current_thread(&mut self, app_server: &mut AppServerSession) {
|
pub(super) async fn shutdown_current_thread(&mut self, app_server: &mut AppServerSession) {
|
||||||
@@ -567,41 +570,71 @@ impl App {
|
|||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
match op {
|
match op {
|
||||||
AppCommand::Interrupt => {
|
AppCommand::Interrupt => {
|
||||||
if let Some(turn_id) = self.active_turn_id_for_thread(thread_id).await {
|
let mut turn_id = self
|
||||||
let mut interrupt_turn_id = turn_id;
|
.active_turn_id_for_thread(thread_id)
|
||||||
for retried_after_turn_mismatch in [false, true] {
|
.await
|
||||||
match app_server
|
.unwrap_or_default();
|
||||||
.turn_interrupt(thread_id, interrupt_turn_id.clone())
|
let (thread_event_tx, thread_event_store) = {
|
||||||
.await
|
let channel = self.ensure_thread_channel(thread_id);
|
||||||
{
|
(channel.sender.clone(), Arc::clone(&channel.store))
|
||||||
Ok(()) => return Ok(true),
|
};
|
||||||
Err(error) if !retried_after_turn_mismatch => {
|
self.reset_backtrack_state();
|
||||||
let Some(actual_turn_id) = active_turn_interrupt_race(&error)
|
if !turn_id.is_empty() {
|
||||||
else {
|
let mut store = thread_event_store.lock().await;
|
||||||
return Err(error).wrap_err("turn/interrupt failed in TUI");
|
if store.pending_interrupt_turn_id.as_deref() == Some(turn_id.as_str()) {
|
||||||
};
|
return Ok(true);
|
||||||
if actual_turn_id == interrupt_turn_id {
|
}
|
||||||
return Err(error).wrap_err("turn/interrupt failed in TUI");
|
store.pending_interrupt_turn_id = Some(turn_id.clone());
|
||||||
}
|
}
|
||||||
// Review flows can swap the active turn before the TUI processes
|
let request_handle = app_server.request_handle();
|
||||||
// the corresponding notification. Retry once with the
|
let request_ids = [app_server.next_request_id(), app_server.next_request_id()];
|
||||||
// server-reported turn id so Ctrl+C/Esc do not fatally exit on that
|
tokio::spawn(async move {
|
||||||
// stale cache, but let lifecycle notifications own the cached
|
for (attempt, request_id) in request_ids.into_iter().enumerate() {
|
||||||
// active turn id.
|
let result = request_handle
|
||||||
interrupt_turn_id = actual_turn_id;
|
.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) => {
|
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)
|
Ok(true)
|
||||||
}
|
}
|
||||||
AppCommand::UserTurn {
|
AppCommand::UserTurn {
|
||||||
|
|||||||
@@ -1271,7 +1271,7 @@ impl AppServerSession {
|
|||||||
self.client.request_handle()
|
self.client.request_handle()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_request_id(&mut self) -> RequestId {
|
pub(crate) fn next_request_id(&mut self) -> RequestId {
|
||||||
let request_id = self.next_request_id;
|
let request_id = self.next_request_id;
|
||||||
self.next_request_id += 1;
|
self.next_request_id += 1;
|
||||||
RequestId::Integer(request_id)
|
RequestId::Integer(request_id)
|
||||||
|
|||||||
Reference in new issue
Block a user