Preserve pending realtime speech when a call ends (#62299)

## Why

A realtime call can end with completed speech that has yet to reach a
Codex turn. The next turn needs that context even if the call's transport
has already disconnected.

## What changed

- For V3 calls with realtime frame history enabled, save pending speech
  as context for the next turn. Restore it on thread reload and exclude
  it from the turn count used by rollback.
- Drain already received transcript events and settle pending handoffs
  before saving the call's closing marker. Report recovery required if
  shutdown cannot confirm that history was saved.
- Keep transport detach separate from ending a call. Reattaching restores
  saved pending speech, and an open call can be ended from persisted
  history after a reload. Use the call ID for TUI attachments so successive
  calls on one thread have distinct history.

## Testing

Add coverage for queued speech at hangup, reconnect and stop after
reload, retries after an uncertain write, and rollback across saved
session-end context.

GitOrigin-RevId: 657491f1d7e446a1a48e22189758810c8bdd82cb
This commit is contained in:
Zahan Malkani
2026-11-18 15:24:29 +00:00
committed by copyberry
parent bd3eaf7829
commit bd00a8a42e
39 changed files with 3687 additions and 256 deletions
@@ -1802,6 +1802,7 @@ async fn realtime_conversation_stop_emits_closed_notification(
.build_initialized_with_timeout(DEFAULT_TIMEOUT)
.await?;
login_with_api_key(&mut mcp, "sk-test-key").await?;
enable_local_realtime_frames(&mut mcp).await?;
let thread_start_request_id = mcp
.send_thread_start_request_with_auto_env(ThreadStartParams {
@@ -1849,7 +1850,7 @@ async fn realtime_conversation_stop_emits_closed_notification(
realtime_start_instructions: None,
realtime_end_instructions: None,
prompt: Some(Some("backend prompt".to_string())),
realtime_session_id: None,
realtime_session_id: Some("call:Voice/one".into()),
voice_session_id: None,
transport: None,
version: Some(RealtimeConversationVersion::V1),
@@ -1901,15 +1902,60 @@ async fn realtime_conversation_stop_emits_closed_notification(
.await?;
let items: ThreadItemsListResponse =
timeout(DEFAULT_TIMEOUT, mcp.read_response(request)).await??;
let tails = items.data.iter().filter(|entry| matches!(
let has_tail = items.data.iter().any(|entry| matches!(
&entry.item.payload, ThreadItemPayload::UserMessage { content, .. }
if content.iter().any(|part| matches!(part, V2UserInput::Text { text, .. }
if text.contains("<source>transcript_tail_flush</source>") && text.contains("user: final words")))
)).collect::<Vec<_>>();
assert_eq!(tails.len(), 1);
if let Some(id) = &running_turn_id {
assert_eq!(&tails[0].turn_id, id);
}
));
assert!(has_tail, "protocol V1 keeps its legacy carryover behavior");
let request = mcp
.send_request(
"thread/realtime/list",
Some(serde_json::json!({"threadId": started.thread_id})),
)
.await?;
let history: codex_app_server_protocol::ThreadRealtimeListResponse =
timeout(DEFAULT_TIMEOUT, mcp.read_response(request)).await??;
let start_id = "realtime-session-start:call:Voice/one";
let request = mcp
.send_request(
"thread/realtime/list",
Some(serde_json::json!({"threadId": started.thread_id, "eventId": start_id})),
)
.await?;
let start: codex_app_server_protocol::ThreadRealtimeListResponse =
timeout(DEFAULT_TIMEOUT, mcp.read_response(request)).await??;
let [start] = start.data.as_slice() else {
panic!("the known call ID should resolve its opening marker");
};
assert_eq!(start.item.id, start_id);
assert!(matches!(
start.item.content,
ThreadRealtimeItemContent::RealtimeSessionStarted
));
let end = history
.data
.iter()
.find(|entry| {
matches!(
entry.item.content,
ThreadRealtimeItemContent::RealtimeSessionClosed { .. }
)
})
.expect("a closed call has a closing marker");
assert!(start.position < end.position);
let realtime = history
.data
.iter()
.map(|entry| &entry.item)
.collect::<Vec<_>>();
assert!(
realtime.iter().any(|item| matches!(
&item.content, ThreadRealtimeItemContent::Frame { channels }
if channels.voice_transcript.text == "final words"
)),
"sealed speech is readable immediately after Closed"
);
let request = mcp
.send_thread_turns_list_request(ThreadTurnsListParams {
thread_id: started.thread_id.clone(),
@@ -1921,7 +1967,6 @@ async fn realtime_conversation_stop_emits_closed_notification(
.await?;
let turns: ThreadTurnsListResponse =
timeout(DEFAULT_TIMEOUT, mcp.read_response(request)).await??;
assert_eq!(tails[0].turn_id, turns.data[0].id);
turns.data
} else {
let request = mcp
@@ -1940,15 +1985,20 @@ async fn realtime_conversation_stop_emits_closed_notification(
history.thread.turns
};
assert_eq!(turns.len(), 1);
assert_eq!(
turns[0].status,
if running {
TurnStatus::InProgress
} else {
TurnStatus::Completed
if let Some(turn) = turns.first() {
assert_eq!(
turn.status,
if running {
TurnStatus::InProgress
} else {
TurnStatus::Completed
}
);
assert_eq!(Uuid::parse_str(&turn.id)?.get_version_num(), 7);
if let Some(id) = &running_turn_id {
assert_eq!(&turn.id, id);
}
);
assert_eq!(Uuid::parse_str(&turns[0].id)?.get_version_num(), 7);
}
response_tx.send(())?;
if running {