Interrupt active code-mode cells with their turn (#37483)

## Why

Interrupting a turn should also stop code-mode work that the turn left running.

## What changed

- Add the disabled-by-default `code_mode_interrupt` feature.
- When an interrupted turn has the feature enabled, terminate all active code-mode cells and propagate cancellation through nested tool calls.
- Keep the reusable code-mode session alive so its stored state remains available to later turns.

## Testing

Add an integration test that interrupts a long-running nested tool, verifies that active and background cells are removed, and confirms that session state is preserved.

GitOrigin-RevId: 2b8634776a89b636318a39b9e9ad2eefc07cead8
This commit is contained in:
pakrym-oai
2026-08-07 18:22:42 +00:00
committed by copyberry
parent 511262b984
commit 509565820f
6 changed files with 268 additions and 5 deletions
+15 -5
View File
@@ -45,6 +45,15 @@ impl CodeModeDispatchBroker {
remove_dispatch_gate(&self.dispatch_gates, cell_id);
}
pub(super) fn active_cell_ids(&self) -> Vec<CellId> {
self.dispatch_gates
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.keys()
.cloned()
.collect()
}
pub(super) fn start_turn_worker(
&self,
exec: ExecContext,
@@ -105,12 +114,13 @@ impl CodeModeDispatchBroker {
}
let host = Arc::clone(&host);
tokio::spawn(async move {
let invocation =
host.invoke_tool(invocation, cancellation_token.clone());
tokio::pin!(invocation);
let response = tokio::select! {
response = host.invoke_tool(
invocation,
cancellation_token.clone(),
) => response,
_ = cancellation_token.cancelled() => return,
biased;
_ = cancellation_token.cancelled() => invocation.await,
response = &mut invocation => response,
};
let _ = response_tx.send(response);
});
+18
View File
@@ -18,6 +18,7 @@ use codex_code_mode::CodeModeSessionProvider;
use codex_code_mode::CodeModeToolKind;
use codex_code_mode::RuntimeResponse;
use codex_protocol::models::FunctionCallOutputContentItem;
use futures::future::join_all;
use serde_json::Value as JsonValue;
use tokio::sync::OnceCell;
use tokio_util::sync::CancellationToken;
@@ -141,6 +142,23 @@ impl CodeModeService {
self.session().await?.terminate(cell_id).await
}
pub(crate) async fn interrupt_active_cells(&self) {
let Some(session) = self.session.get() else {
return;
};
join_all(
self.dispatch_broker
.active_cell_ids()
.into_iter()
.map(|cell_id| async move {
if let Err(error) = session.terminate(cell_id.clone()).await {
tracing::warn!(%cell_id, %error, "failed to terminate interrupted code-mode cell");
}
}),
)
.await;
}
pub(crate) async fn shutdown(&self) -> Result<(), String> {
self.shutting_down.store(true, Ordering::Release);
// Join any initialization already in progress without initializing an unused service.