Fix turn interrupt reply ordering and shutdown handling (#62066)

## Why

A delayed terminal event from an earlier turn could acknowledge interrupts for a later turn. Interrupts could also remain pending after the thread listener shut down.

## What changed

- Associate pending `turn/interrupt` requests with their target turn and reply only when that turn completes or aborts.
- Track the current Core turn through stop hooks and resolve review delegate IDs to the owning turn when validating interrupts.
- Settle pending interrupts with an error on listener shutdown and reject further interrupts against the retired runtime.

## Testing

Add regression tests for delayed terminal events, queued turn starts, review delegates, and interrupt handling during thread revert and runtime replacement.

GitOrigin-RevId: 9695fc3f718baa6b792a14d54136bb87fc003100
This commit is contained in:
jif
2026-11-17 15:33:21 +00:00
committed by copyberry
parent 0cc88d0c74
commit 58627e6fea
9 changed files with 659 additions and 47 deletions
+11
View File
@@ -892,6 +892,17 @@ impl CodexThread {
self.session.active_turn.lock().await.is_some()
}
/// Returns the current turn ID, including while its stop hook runs.
pub async fn current_turn_id(&self) -> Option<String> {
self.session
.active_turn
.lock()
.await
.as_ref()?
.turn_id
.clone()
}
/// Returns the trusted root when the expected turn is currently active.
pub async fn active_turn_root(&self, expected_turn_id: &str) -> Option<String> {
let active = self.session.active_turn.lock().await;
+3
View File
@@ -36,6 +36,8 @@ use codex_protocol::protocol::TokenUsage;
/// Metadata about the currently running turn.
pub(crate) struct ActiveTurn {
/// The slot's owner, retained after its task detaches to emit the terminal event.
pub(crate) turn_id: Option<String>,
pub(crate) task: Option<RunningTask>,
pub(crate) sampling_interrupt: Option<SamplingInterrupt>,
/// Present when input admission is closed; cancelled when the final batch is recorded.
@@ -67,6 +69,7 @@ pub(crate) enum MailboxDeliveryPhase {
impl Default for ActiveTurn {
fn default() -> Self {
Self {
turn_id: None,
task: None,
sampling_interrupt: None,
finishing: None,
+1
View File
@@ -367,6 +367,7 @@ impl Session {
}
let turn = active.get_or_insert_with(ActiveTurn::default);
debug_assert!(turn.task.is_none());
turn.turn_id = Some(turn_context.sub_id.clone());
turn.finishing = None;
Arc::clone(&turn.turn_state)
};