Avoid cloning turn items for app-server active turn lookups (#46305)

## Why

App-server callers that only need the active turn ID or its presence currently create a full turn snapshot, unnecessarily cloning its items.

## What changed

Expose `ThreadState::active_turn_id()` and use it for interrupt validation, elicitation turn ID fallback, and teardown and shutdown logging, preserving the existing turn selection behavior.

## Testing

Update the interrupt integration test to wait for `turn/started` instead of a fixed delay and verify that an incorrect turn ID is rejected before successfully interrupting the active turn.

GitOrigin-RevId: 4a030655fbc4d229809cf13f80c3d7f788f19c13
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-09-17 21:25:34 +00:00
1 parent 7a3c5a83e4
commit 55db7e8c88
4 files changed
+38 -9

No files matched your search

@@ -9,6 +9,7 @@ use app_test_support::create_mock_responses_server_sequence;
use app_test_support::create_mock_responses_server_sequence_unchecked;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::JSONRPCError;
use codex_app_server_protocol::JSONRPCErrorError;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerRequest;
use codex_app_server_protocol::ServerRequestResolvedNotification;
@@ -19,9 +20,11 @@ use codex_app_server_protocol::TurnInterruptParams;
use codex_app_server_protocol::TurnInterruptResponse;
use codex_app_server_protocol::TurnStartParams;
use codex_app_server_protocol::TurnStartResponse;
use codex_app_server_protocol::TurnStartedNotification;
use codex_app_server_protocol::TurnStatus;
use codex_app_server_protocol::UserInput as V2UserInput;
use core_test_support::skip_if_remote;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::time::timeout;
@@ -98,10 +101,32 @@ async fn turn_interrupt_aborts_running_turn() -> Result<()> {
.await?;
let turn_id = turn.id.clone();
// Give the command a brief moment to start.
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let started: TurnStartedNotification =
timeout(DEFAULT_READ_TIMEOUT, mcp.read_notification("turn/started")).await??;
assert_eq!(started.thread_id, thread.id);
assert_eq!(started.turn.id, turn_id);
let thread_id = thread.id.clone();
let interrupt_id = mcp
.send_turn_interrupt_request(TurnInterruptParams {
thread_id: thread_id.clone(),
turn_id: "wrong-turn".to_string(),
})
.await?;
let interrupt_err = timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(interrupt_id)),
)
.await??;
assert_eq!(
interrupt_err.error,
JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
message: format!("expected active turn id wrong-turn but found {turn_id}"),
data: None,
}
);
// Interrupt the in-progress turn by id (v2 API).
let _: TurnInterruptResponse = mcp
.request(|request_id| ClientRequest::TurnInterrupt {