mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
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:
@@ -1273,7 +1273,15 @@ impl ContextManager {
|
||||
mut cut_idx: usize,
|
||||
) -> usize {
|
||||
while cut_idx > first_instruction_turn_idx {
|
||||
match &snapshot[cut_idx - 1].item {
|
||||
let previous = &snapshot[cut_idx - 1];
|
||||
if previous
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
{
|
||||
break;
|
||||
}
|
||||
match &previous.item {
|
||||
ResponseItem::Message {
|
||||
role,
|
||||
content,
|
||||
@@ -1691,9 +1699,13 @@ pub(crate) fn is_user_turn_boundary(item: &ResponseItem) -> bool {
|
||||
|| (role == "assistant" && is_inter_agent_instruction_content(content))
|
||||
}
|
||||
|
||||
/// Evaluates an instruction boundary from annotated history.
|
||||
/// Session-end realtime input is saved context, not an instruction that started or steered a turn.
|
||||
pub(crate) fn is_instruction_turn_boundary(envelope: &ResponseItemEnvelope) -> bool {
|
||||
is_user_turn_boundary(&envelope.item)
|
||||
!envelope
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
&& is_user_turn_boundary(&envelope.item)
|
||||
}
|
||||
|
||||
fn is_inter_agent_instruction_content(content: &[ContentItem]) -> bool {
|
||||
|
||||
@@ -2161,6 +2161,46 @@ async fn drop_last_n_user_turns_preserves_prefix() {
|
||||
assert_eq!(serde_json::to_value(retained).unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_end_input_does_not_consume_a_rollback_turn() {
|
||||
let first = user_input_text_msg("first turn");
|
||||
// Even if its content happens to look like normal pre-turn context, a saved
|
||||
// hangup belongs to the preceding turn and must survive rollback of the next.
|
||||
let tail =
|
||||
user_input_text_msg("<environment_context>speech saved at hangup</environment_context>");
|
||||
let next = user_input_text_msg("next turn");
|
||||
let mut saved_tail = ResponseItemEnvelope::new(tail.clone());
|
||||
saved_tail.metadata = Some(CodexHarnessMetadata {
|
||||
realtime_session_end_input: true,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
for (mut items, expected) in [
|
||||
(
|
||||
vec![ResponseItemEnvelope::new(first.clone()), saved_tail.clone()],
|
||||
Vec::new(),
|
||||
),
|
||||
(
|
||||
vec![
|
||||
ResponseItemEnvelope::new(first.clone()),
|
||||
saved_tail,
|
||||
ResponseItemEnvelope::new(next),
|
||||
],
|
||||
vec![first, tail],
|
||||
),
|
||||
] {
|
||||
let mut live = ContextManager::default();
|
||||
live.record_annotated_items(&mut items, TruncationPolicy::Tokens(10_000));
|
||||
let mut checkpoint = ContextManager::default();
|
||||
checkpoint.replace_annotated(live.annotated_items().to_vec());
|
||||
|
||||
for mut history in [live, checkpoint] {
|
||||
history.drop_last_n_user_turns(/*num_turns*/ 1);
|
||||
assert_eq!(raw_items(&history), expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rollback_removes_assistant_sources_recorded_ahead_of_queued_input() {
|
||||
let mut history = root_history();
|
||||
|
||||
Reference in New Issue
Block a user