mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +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:
BIN
Binary file not shown.
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
//! Drives caller-owned existing calls without coupling inbound events to outbound backpressure.
|
||||
//! Detach stops writes and drains accepted inbound events before flushing the transcript.
|
||||
//! Caller transport closure arms the cleanup deadline even when the event queue is full.
|
||||
//! Pending writes are never replayed.
|
||||
//! Existing calls support v1/v3 only, so inbound handling does not issue v2 response-control writes.
|
||||
|
||||
@@ -14,14 +15,15 @@ use super::handle_handoff_output;
|
||||
use super::handle_realtime_server_event;
|
||||
use super::handle_text_input;
|
||||
use super::handle_user_audio_input;
|
||||
use super::realtime_transcript_delta;
|
||||
use super::report_realtime_transport_loss;
|
||||
use crate::context::RealtimeDelegation;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
pub(super) fn spawn_input_task(
|
||||
input: RealtimeInputTask,
|
||||
transcript_tail_flush: RealtimeTranscriptTailFlush,
|
||||
caller_commands: mpsc::Sender<String>,
|
||||
) -> JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let RealtimeInputTask {
|
||||
@@ -50,7 +52,7 @@ pub(super) fn spawn_input_task(
|
||||
.take_completed_transcript(&event, RealtimeDelegation::FIELD_BYTES)
|
||||
.await;
|
||||
handle_realtime_server_event(
|
||||
(event, realtime_transcript_delta(&completed)),
|
||||
(event, completed),
|
||||
&writer,
|
||||
&events_tx,
|
||||
&handoff_state,
|
||||
@@ -91,6 +93,14 @@ pub(super) fn spawn_input_task(
|
||||
let _ = read.await;
|
||||
Ok(())
|
||||
},
|
||||
_ = caller_commands.closed() => {
|
||||
// CCA drops both channel halves on terminal transport loss. Observe
|
||||
// that cutoff independently of the bounded fanout queue, then drain.
|
||||
stop_token.cancel();
|
||||
writer.close_sideband_input();
|
||||
let _ = read.await;
|
||||
Ok(())
|
||||
},
|
||||
result = &mut read => result,
|
||||
result = write => {
|
||||
writer.close_sideband_input();
|
||||
@@ -102,6 +112,7 @@ pub(super) fn spawn_input_task(
|
||||
// Dropping the last writer aborts the transport pump without waiting for
|
||||
// space in the host's outgoing queue (including for a close command).
|
||||
drop(writer);
|
||||
stop_token.cancel();
|
||||
if let Err(err) = result
|
||||
&& let RealtimeInputTaskExit::TransportLost { err, .. } =
|
||||
classify_realtime_input_error(err)
|
||||
|
||||
@@ -107,7 +107,7 @@ pub(super) fn spawn_webrtc_sideband_input_task(
|
||||
"failed to connect realtime sideband: {mapped_error}"
|
||||
);
|
||||
let _ = events_tx
|
||||
.send((RealtimeEvent::Error(mapped_error.to_string()), None))
|
||||
.send((RealtimeEvent::Error(mapped_error.to_string()), Vec::new()))
|
||||
.await;
|
||||
break;
|
||||
}
|
||||
@@ -187,6 +187,7 @@ pub(super) fn spawn_webrtc_sideband_input_task(
|
||||
// Cancellation may win before the task takes its already connected observer.
|
||||
close_sideband_transport(&connection).await;
|
||||
}
|
||||
stop_token.cancel();
|
||||
let transcript_tail = transcript_state.take_transcript_tail().await;
|
||||
flush_realtime_transcript_tail(&transcript_tail_flush, &transcript_tail).await;
|
||||
})
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
use crate::session::new_submission_id;
|
||||
use crate::session::session::Session;
|
||||
use codex_history::RealtimeSessionInputMetadata;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::items::UserMessageItem;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
@@ -14,11 +16,27 @@ use codex_thread_store::PersistContext;
|
||||
use codex_user_input::ResolvedUserInput;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) async fn record(session: &Session, text: String) -> std::io::Result<()> {
|
||||
record_input(session, text, /*metadata*/ None).await
|
||||
}
|
||||
|
||||
pub(super) async fn record_chunk(
|
||||
session: &Session,
|
||||
text: String,
|
||||
metadata: RealtimeSessionInputMetadata,
|
||||
) -> std::io::Result<()> {
|
||||
record_input(session, text, Some(metadata)).await
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "record history before the owning task can finish or a new task can start"
|
||||
)]
|
||||
pub(super) async fn record(session: &Session, text: String) -> std::io::Result<()> {
|
||||
async fn record_input(
|
||||
session: &Session,
|
||||
text: String,
|
||||
metadata: Option<RealtimeSessionInputMetadata>,
|
||||
) -> std::io::Result<()> {
|
||||
let mut recording_context = session.new_inject_items_context().await;
|
||||
Arc::get_mut(&mut recording_context)
|
||||
.ok_or_else(|| std::io::Error::other("realtime transcript context is shared"))?
|
||||
@@ -50,9 +68,20 @@ pub(super) async fn record(session: &Session, text: String) -> std::io::Result<(
|
||||
.await;
|
||||
}
|
||||
let item = session.response_item_from_user_input(std::slice::from_ref(&input));
|
||||
session
|
||||
.record_conversation_items(&turn, turn.model_info(), &[item])
|
||||
.await;
|
||||
if let Some(metadata) = metadata {
|
||||
let mut envelope = ResponseItemEnvelope::new(item);
|
||||
envelope
|
||||
.metadata
|
||||
.get_or_insert_default()
|
||||
.realtime_session_input = Some(Box::new(metadata));
|
||||
session
|
||||
.record_realtime_session_chunk(&turn, envelope)
|
||||
.await?;
|
||||
} else {
|
||||
session
|
||||
.record_conversation_items(&turn, turn.model_info(), &[item])
|
||||
.await;
|
||||
}
|
||||
|
||||
// ItemCompleted is the existing source for thread/items/list. Keep the legacy
|
||||
// equivalent too; the store selects the appropriate format for this thread.
|
||||
|
||||
@@ -286,6 +286,7 @@ async fn natural_close_finishes_before_successor_and_ignores_old_identity() {
|
||||
&old_identity,
|
||||
"A".to_owned(),
|
||||
RealtimeConversationEnd::TransportClosed,
|
||||
/*reconnectable_session*/ false,
|
||||
);
|
||||
tokio::pin!(close);
|
||||
assert!(futures::poll!(&mut close).is_pending());
|
||||
@@ -319,6 +320,7 @@ async fn natural_close_finishes_before_successor_and_ignores_old_identity() {
|
||||
&old_identity,
|
||||
"A".to_owned(),
|
||||
RealtimeConversationEnd::TransportClosed,
|
||||
/*reconnectable_session*/ false,
|
||||
)
|
||||
.await;
|
||||
let observed = std::iter::from_fn(|| events.try_recv().ok())
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//! transcript boundaries and deduplication state with the history reducer.
|
||||
//! The reducer selects when its effects are persisted relative to their source event.
|
||||
|
||||
#[expect(dead_code, reason = "Activated with hangup in the next PR")]
|
||||
pub(crate) mod handoff;
|
||||
mod presentation;
|
||||
|
||||
@@ -33,6 +32,7 @@ struct ActiveSegment {
|
||||
id: String,
|
||||
role: RealtimeTranscriptRole,
|
||||
text: String,
|
||||
sealed_prefix: String,
|
||||
observed_at_ms: Option<i64>,
|
||||
}
|
||||
|
||||
@@ -56,8 +56,9 @@ impl ActiveTranscriptSegments {
|
||||
if self.first_active_role == Some(role) {
|
||||
self.first_active_role = self
|
||||
.user
|
||||
.as_ref()
|
||||
.or(self.assistant.as_ref())
|
||||
.iter()
|
||||
.chain(self.assistant.iter())
|
||||
.find(|segment| !segment.text.is_empty())
|
||||
.map(|segment| segment.role);
|
||||
}
|
||||
segment
|
||||
@@ -208,9 +209,14 @@ impl RealtimeHistoryState {
|
||||
.clone()
|
||||
.unwrap_or_else(|| Uuid::now_v7().to_string());
|
||||
if self.active_session_id.as_deref() != Some(session_id.as_str()) {
|
||||
items.append(&mut self.pending_handoff_seals);
|
||||
self.seal_segments(&mut items, Continuation::Finish);
|
||||
self.pending_transcript.clear();
|
||||
self.preserve_transcript =
|
||||
event.version == codex_protocol::protocol::RealtimeConversationVersion::V3;
|
||||
self.active_session_id = Some(session_id.clone());
|
||||
self.failed = false;
|
||||
self.v3_utterances.clear();
|
||||
items.push(RealtimeItem {
|
||||
// The call ID lets a reader seek directly to its opening position.
|
||||
id: if self.frames_enabled() {
|
||||
@@ -245,8 +251,17 @@ impl RealtimeHistoryState {
|
||||
transcript_stream =
|
||||
self.finish_segment(&mut items, RealtimeTranscriptRole::Assistant, done);
|
||||
}
|
||||
RealtimeEvent::HandoffRequested(_) => {
|
||||
if let Some(session_id) = &self.active_session_id {
|
||||
RealtimeEvent::HandoffRequested(handoff) => {
|
||||
// The transport takes its transcript for every request, even if admission
|
||||
// fails. Rejected input remains pending for the next handoff.
|
||||
self.v3_utterances.clear();
|
||||
if !self.committed_handoff_ids.contains(&handoff.handoff_id)
|
||||
&& !self
|
||||
.handoff_inputs
|
||||
.get(&handoff.handoff_id)
|
||||
.is_some_and(|input| input.reserved)
|
||||
&& let Some(session_id) = &self.active_session_id
|
||||
{
|
||||
self.pending_handoffs.push_back(session_id.clone());
|
||||
}
|
||||
}
|
||||
@@ -312,6 +327,7 @@ impl RealtimeHistoryState {
|
||||
}
|
||||
EventMsg::RealtimeConversationClosed(_) => {
|
||||
if let Some(session_id) = self.active_session_id.take() {
|
||||
items.append(&mut self.pending_handoff_seals);
|
||||
self.seal_segments(&mut items, Continuation::Finish);
|
||||
items.push(RealtimeItem {
|
||||
id: Uuid::now_v7().to_string(),
|
||||
@@ -329,6 +345,7 @@ impl RealtimeHistoryState {
|
||||
},
|
||||
});
|
||||
self.failed = false;
|
||||
self.v3_utterances.clear();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -383,7 +400,10 @@ impl RealtimeHistoryState {
|
||||
delta: &RealtimeTranscriptDelta,
|
||||
) -> Option<RealtimeTranscriptStream> {
|
||||
let session_id = self.active_session_id.clone()?;
|
||||
self.active_segments.first_active_role.get_or_insert(role);
|
||||
if !delta.delta.is_empty() {
|
||||
let _ = self.v3_utterance(role, /*start*/ true);
|
||||
self.active_segments.first_active_role.get_or_insert(role);
|
||||
}
|
||||
let frames_enabled = self.frames_enabled();
|
||||
let segment = self
|
||||
.active_segments
|
||||
@@ -393,6 +413,7 @@ impl RealtimeHistoryState {
|
||||
id: Uuid::now_v7().to_string(),
|
||||
role,
|
||||
text: String::new(),
|
||||
sealed_prefix: String::new(),
|
||||
observed_at_ms: None,
|
||||
});
|
||||
let observed_at_ms = frames_enabled.then(|| {
|
||||
@@ -420,7 +441,20 @@ impl RealtimeHistoryState {
|
||||
role: RealtimeTranscriptRole,
|
||||
done: &RealtimeTranscriptDone,
|
||||
) -> Option<RealtimeTranscriptStream> {
|
||||
let Some(segment) = self.active_segments.take(role) else {
|
||||
let stream = self.finish_transcript_frame(items, role, done);
|
||||
if let Some(utterance) = self.v3_utterance(role, /*start*/ !done.text.is_empty()) {
|
||||
utterance.complete = true;
|
||||
}
|
||||
stream
|
||||
}
|
||||
|
||||
fn finish_transcript_frame(
|
||||
&mut self,
|
||||
items: &mut Vec<RealtimeItem>,
|
||||
role: RealtimeTranscriptRole,
|
||||
done: &RealtimeTranscriptDone,
|
||||
) -> Option<RealtimeTranscriptStream> {
|
||||
let Some(mut segment) = self.active_segments.take(role) else {
|
||||
if done.text.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -435,10 +469,35 @@ impl RealtimeHistoryState {
|
||||
}
|
||||
return stream;
|
||||
};
|
||||
// A split leaves an empty continuation; the upstream final may repeat
|
||||
// text that was already committed before the split.
|
||||
// A final may extend unsealed speech, but a delayed final must not replace
|
||||
// newer deltas or replay text already committed before a split.
|
||||
let suffix = done
|
||||
.text
|
||||
.strip_prefix(&segment.sealed_prefix)
|
||||
.and_then(|tail| tail.strip_prefix(&segment.text))
|
||||
.filter(|suffix| !suffix.is_empty())
|
||||
.map(str::to_owned);
|
||||
let stream = suffix.map(|delta| {
|
||||
let started_item = segment.text.is_empty().then(|| {
|
||||
segment.observed_at_ms = self
|
||||
.frames_enabled()
|
||||
.then(|| self.observation_clock.now_ms());
|
||||
RealtimeItem {
|
||||
id: segment.id.clone(),
|
||||
realtime_session_id: segment.session_id.clone(),
|
||||
observed_at_ms: segment.observed_at_ms,
|
||||
content: speech(self.frames_enabled(), role, String::new()),
|
||||
}
|
||||
});
|
||||
segment.text.push_str(&delta);
|
||||
RealtimeTranscriptStream {
|
||||
started_item,
|
||||
item_id: segment.id.clone(),
|
||||
delta,
|
||||
}
|
||||
});
|
||||
self.seal_active_segment(items, segment, Continuation::Finish);
|
||||
None
|
||||
stream
|
||||
}
|
||||
|
||||
fn seal_segments(&mut self, items: &mut Vec<RealtimeItem>, continuation: Continuation) {
|
||||
@@ -466,6 +525,7 @@ impl RealtimeHistoryState {
|
||||
segment: ActiveSegment,
|
||||
continuation: Continuation,
|
||||
) {
|
||||
let sealed_prefix = format!("{}{}", segment.sealed_prefix, segment.text);
|
||||
if !segment.text.is_empty() {
|
||||
let item = RealtimeItem {
|
||||
id: segment.id,
|
||||
@@ -482,14 +542,12 @@ impl RealtimeHistoryState {
|
||||
items.push(item);
|
||||
}
|
||||
if matches!(continuation, Continuation::Continue) {
|
||||
self.active_segments
|
||||
.first_active_role
|
||||
.get_or_insert(segment.role);
|
||||
*self.active_segments.slot_mut(segment.role) = Some(ActiveSegment {
|
||||
session_id: segment.session_id,
|
||||
id: Uuid::now_v7().to_string(),
|
||||
role: segment.role,
|
||||
text: String::new(),
|
||||
sealed_prefix,
|
||||
observed_at_ms: None,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ pub(crate) struct PreparedRealtimeInput {
|
||||
consumed_event_ids: Vec<String>,
|
||||
pub(crate) input: String,
|
||||
pub(crate) new_items: Vec<RealtimeItem>,
|
||||
pub(crate) receipt: Option<RealtimeItem>,
|
||||
pub(crate) reserved: bool,
|
||||
reserved_turn_id: Option<String>,
|
||||
pub(crate) cached_envelope: Option<ResponseItemEnvelope>,
|
||||
@@ -323,6 +324,7 @@ impl RealtimeHistoryState {
|
||||
consumed_event_ids,
|
||||
input,
|
||||
new_items: self.unassigned_handoff_seals(),
|
||||
receipt: None,
|
||||
reserved: false,
|
||||
reserved_turn_id: None,
|
||||
cached_envelope: None,
|
||||
@@ -332,12 +334,77 @@ impl RealtimeHistoryState {
|
||||
Some(prepared)
|
||||
}
|
||||
|
||||
pub(crate) fn restore_handoffs(&mut self, items: &[codex_history::RolloutItem]) {
|
||||
for handoff_id in items
|
||||
pub(crate) fn prepare_carryover(&mut self, instruction: &str) -> Option<PreparedRealtimeInput> {
|
||||
let session_id = self.active_session_id.clone()?;
|
||||
let handoff_id = format!("tail:{session_id}");
|
||||
if self.committed_handoff_ids.contains(&handoff_id) {
|
||||
return None;
|
||||
}
|
||||
if let Some(prepared) = self.handoff_inputs.get(&handoff_id) {
|
||||
return Some(prepared.clone());
|
||||
}
|
||||
let preserve_transcript = self.preserve_transcript;
|
||||
let entries = self.seal_available_transcripts();
|
||||
if entries.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let consumed_event_ids = entries
|
||||
.iter()
|
||||
.filter_map(codex_history::RolloutItem::realtime_handoff_id)
|
||||
{
|
||||
self.committed_handoff_ids.insert(handoff_id.to_string());
|
||||
.filter_map(|(id, _, _)| id.map(str::to_string))
|
||||
.collect();
|
||||
let (input, sources) = RealtimeDelegation::from_events(
|
||||
instruction,
|
||||
&entries,
|
||||
RealtimeDelegationSource::TranscriptTailFlush,
|
||||
preserve_transcript,
|
||||
)?;
|
||||
let id = Uuid::now_v7();
|
||||
let receipt = RealtimeItem {
|
||||
id: id.to_string(),
|
||||
realtime_session_id: session_id.clone(),
|
||||
observed_at_ms: Some(self.observation_clock.now_ms()),
|
||||
content: RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
source_event_ids: sources.clone(),
|
||||
input_item_id: codex_protocol::ResponseItemId::for_admitted_user_input(id)
|
||||
.to_string(),
|
||||
input: input.clone(),
|
||||
},
|
||||
};
|
||||
let prepared = PreparedRealtimeInput {
|
||||
id,
|
||||
session_id,
|
||||
handoff_id: handoff_id.clone(),
|
||||
sources,
|
||||
consumed_event_ids,
|
||||
input,
|
||||
new_items: self.unassigned_handoff_seals(),
|
||||
receipt: Some(receipt),
|
||||
reserved: false,
|
||||
reserved_turn_id: None,
|
||||
cached_envelope: None,
|
||||
};
|
||||
self.handoff_inputs.insert(handoff_id, prepared.clone());
|
||||
Some(prepared)
|
||||
}
|
||||
|
||||
pub(crate) fn restore_handoffs(&mut self, items: &[codex_history::RolloutItem]) {
|
||||
for item in items {
|
||||
if let Some(handoff_id) = item.realtime_handoff_id() {
|
||||
self.committed_handoff_ids.insert(handoff_id.to_string());
|
||||
} else if let codex_history::RolloutItem::RealtimeItem(RealtimeItem {
|
||||
realtime_session_id,
|
||||
content:
|
||||
RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
..
|
||||
},
|
||||
..
|
||||
}) = item
|
||||
{
|
||||
self.committed_handoff_ids
|
||||
.insert(format!("tail:{realtime_session_id}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -444,7 +444,7 @@ fn does_not_replay_a_final_transcript_after_a_promotion_split() {
|
||||
let done = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "Already spoken, with an upstream revision".to_string(),
|
||||
text: "Already spoken".to_string(),
|
||||
}),
|
||||
);
|
||||
assert!(done.items.is_empty());
|
||||
@@ -738,15 +738,27 @@ fn typed_input_seals_both_roles_but_realtime_delegation_does_not(
|
||||
},
|
||||
]
|
||||
);
|
||||
for event in [
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "assistant revised".to_string(),
|
||||
}),
|
||||
RealtimeEvent::InputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "user revised".to_string(),
|
||||
}),
|
||||
for (role, event) in [
|
||||
(
|
||||
RealtimeTranscriptRole::Assistant,
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "assistant revised".to_string(),
|
||||
}),
|
||||
),
|
||||
(
|
||||
RealtimeTranscriptRole::User,
|
||||
RealtimeEvent::InputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "user revised".to_string(),
|
||||
}),
|
||||
),
|
||||
] {
|
||||
assert!(observe_realtime(&mut state, event).items.is_empty());
|
||||
assert_eq!(
|
||||
contents(observe_realtime(&mut state, event)),
|
||||
vec![RealtimeItemContent::TranscriptSegment {
|
||||
role,
|
||||
text: " revised".to_string(),
|
||||
}],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -826,3 +838,306 @@ fn recovered_v3_speech_excludes_exact_consumed_frames_and_precedes_new_chunks()
|
||||
RecoveredSessionStatus::Ending
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn handoff_settlement_releases_rejected_sources_but_preserves_uncertain_seals() {
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::RealtimeHandoffRequested;
|
||||
let mut state = started_state();
|
||||
state.frame_enrollment = Some(true);
|
||||
let source = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::InputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "speech".into(),
|
||||
}),
|
||||
)
|
||||
.items
|
||||
.remove(0)
|
||||
.id;
|
||||
let request = RealtimeHandoffRequested {
|
||||
handoff_id: "handoff".into(),
|
||||
item_id: "item".into(),
|
||||
input_transcript: "speech".into(),
|
||||
active_transcript: Vec::new(),
|
||||
};
|
||||
let prepared = state.prepare_handoff(&request).unwrap();
|
||||
assert!(state.reserve_handoff(prepared.id));
|
||||
let handoff::HandoffSettlement::Wait(settlement) = state.pending_handoff_settlement().unwrap()
|
||||
else {
|
||||
panic!("reserved handoff should wait for settlement");
|
||||
};
|
||||
state.reject_handoff(prepared.id);
|
||||
settlement.notified().await;
|
||||
assert!(matches!(
|
||||
state.pending_handoff_settlement().unwrap(),
|
||||
handoff::HandoffSettlement::Settled
|
||||
));
|
||||
|
||||
let retry = state.prepare_handoff(&request).unwrap();
|
||||
assert_eq!(retry.sources, vec![source]);
|
||||
assert!(state.reserve_handoff(retry.id));
|
||||
state
|
||||
.cache_handoff_input(retry.id, ResponseItem::CompactionTrigger {}.into())
|
||||
.unwrap();
|
||||
state.reject_handoff(retry.id);
|
||||
assert!(state.pending_handoff_settlement().is_err());
|
||||
assert!(
|
||||
state.prepare_carryover("hangup").is_none(),
|
||||
"an uncertain seal must not release sources to carryover"
|
||||
);
|
||||
let same = state.prepare_handoff(&request).unwrap();
|
||||
assert_eq!(same.id, retry.id);
|
||||
assert!(state.reserve_handoff(same.id));
|
||||
state.finish_handoff(same.id, /*turn_id*/ None);
|
||||
assert!(matches!(
|
||||
state.pending_handoff_settlement().unwrap(),
|
||||
handoff::HandoffSettlement::Settled
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handoff_seals_stable_prefix_and_rejection_recomputes_sources() {
|
||||
use codex_protocol::protocol::RealtimeHandoffRequested;
|
||||
let mut state = started_state();
|
||||
state.frame_enrollment = Some(true);
|
||||
let stream = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::InputTranscriptDelta(RealtimeTranscriptDelta {
|
||||
delta: "partial speech".into(),
|
||||
}),
|
||||
)
|
||||
.transcript_stream
|
||||
.unwrap();
|
||||
let request = RealtimeHandoffRequested {
|
||||
handoff_id: "handoff".into(),
|
||||
item_id: "upstream".into(),
|
||||
input_transcript: "".into(),
|
||||
active_transcript: Vec::new(),
|
||||
};
|
||||
let prepared = state.prepare_handoff(&request).unwrap();
|
||||
assert_eq!(prepared.sources, vec![stream.item_id.clone()]);
|
||||
assert_eq!(prepared.new_items[0].id, stream.item_id);
|
||||
assert!(prepared.input.contains("user: partial speech"));
|
||||
assert!(state.reserve_handoff(prepared.id));
|
||||
assert!(!state.reserve_handoff(prepared.id));
|
||||
// An in-flight input reserves its source; later deltas form a new event.
|
||||
let continued = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::InputTranscriptDelta(RealtimeTranscriptDelta {
|
||||
delta: " more".into(),
|
||||
}),
|
||||
)
|
||||
.transcript_stream
|
||||
.unwrap();
|
||||
assert_ne!(continued.item_id, stream.item_id);
|
||||
let overlapping = state
|
||||
.prepare_handoff(&RealtimeHandoffRequested {
|
||||
handoff_id: "overlapping".into(),
|
||||
..request.clone()
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(overlapping.sources, vec![continued.item_id]);
|
||||
// A definite rejection frees the sources; a retry builds a fresh snapshot.
|
||||
state.reject_handoff(prepared.id);
|
||||
state.finish_handoff(overlapping.id, /*turn_id*/ None);
|
||||
let retry = state.prepare_handoff(&request).unwrap();
|
||||
assert_ne!(retry.id, prepared.id);
|
||||
assert_eq!(retry.input, prepared.input);
|
||||
assert_eq!(retry.sources, prepared.sources);
|
||||
let next = state
|
||||
.prepare_handoff(&RealtimeHandoffRequested {
|
||||
handoff_id: "next".into(),
|
||||
..request
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(next.sources, vec![stream.item_id]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handoff_fallback_is_retained_without_inventing_user_speech() {
|
||||
let mut state = started_state();
|
||||
state.frame_enrollment = Some(true);
|
||||
let prepared = state
|
||||
.prepare_handoff(&codex_protocol::protocol::RealtimeHandoffRequested {
|
||||
handoff_id: "fallback".into(),
|
||||
item_id: "upstream".into(),
|
||||
input_transcript: "only available in the handoff".into(),
|
||||
active_transcript: Vec::new(),
|
||||
})
|
||||
.unwrap();
|
||||
assert!(prepared.sources.is_empty());
|
||||
assert!(prepared.new_items.is_empty());
|
||||
assert!(
|
||||
prepared
|
||||
.input
|
||||
.contains("<input>only available in the handoff</input>")
|
||||
);
|
||||
assert!(
|
||||
prepared
|
||||
.input
|
||||
.contains("<transcript_delta>user: only available in the handoff</transcript_delta>")
|
||||
);
|
||||
}
|
||||
|
||||
/// A final's added words stay on the original unsealed event and enter handoff context.
|
||||
#[test]
|
||||
fn final_transcript_extends_unsealed_event_without_replacing_its_identity() {
|
||||
let mut state = started_state();
|
||||
state.frame_enrollment = Some(true);
|
||||
let start = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDelta(RealtimeTranscriptDelta {
|
||||
delta: "A bunch of stuff".into(),
|
||||
}),
|
||||
)
|
||||
.transcript_stream
|
||||
.unwrap()
|
||||
.started_item
|
||||
.unwrap();
|
||||
let done = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "A bunch of stuff, including coding.".into(),
|
||||
}),
|
||||
);
|
||||
let stream = done.transcript_stream.unwrap();
|
||||
assert_eq!(stream.item_id, start.id);
|
||||
assert!(stream.started_item.is_none());
|
||||
assert_eq!(stream.delta, ", including coding.");
|
||||
assert_eq!(done.items[0].id, start.id);
|
||||
assert_eq!(done.items[0].observed_at_ms, start.observed_at_ms);
|
||||
assert_eq!(
|
||||
done.items[0].content,
|
||||
super::speech(
|
||||
/*frames_enabled*/ true,
|
||||
RealtimeTranscriptRole::Assistant,
|
||||
"A bunch of stuff, including coding.".into()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// A final received after a handoff contributes only new words and keeps sealed evidence unchanged.
|
||||
#[test]
|
||||
fn final_transcript_after_handoff_seals_only_the_added_suffix() {
|
||||
let mut state = started_state();
|
||||
state.frame_enrollment = Some(true);
|
||||
observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDelta(RealtimeTranscriptDelta {
|
||||
delta: "Already spoken".into(),
|
||||
}),
|
||||
);
|
||||
let first = state
|
||||
.prepare_handoff(&codex_protocol::protocol::RealtimeHandoffRequested {
|
||||
handoff_id: "first".into(),
|
||||
item_id: "first".into(),
|
||||
input_transcript: "first task".into(),
|
||||
active_transcript: Vec::new(),
|
||||
})
|
||||
.unwrap();
|
||||
let done = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "Already spoken, with more detail".into(),
|
||||
}),
|
||||
);
|
||||
let stream = done.transcript_stream.unwrap();
|
||||
let started = stream.started_item.unwrap();
|
||||
assert_ne!(started.id, first.new_items[0].id);
|
||||
assert_eq!(stream.delta, ", with more detail");
|
||||
assert_eq!(done.items[0].id, started.id);
|
||||
assert_eq!(done.items[0].observed_at_ms, started.observed_at_ms);
|
||||
assert_eq!(
|
||||
done.items[0].content,
|
||||
super::speech(
|
||||
/*frames_enabled*/ true,
|
||||
RealtimeTranscriptRole::Assistant,
|
||||
", with more detail".into()
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
first.new_items[0].content,
|
||||
super::speech(
|
||||
/*frames_enabled*/ true,
|
||||
RealtimeTranscriptRole::Assistant,
|
||||
"Already spoken".into()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v3_completed_chunk_retires_only_its_own_frames() {
|
||||
use codex_protocol::protocol::RealtimeHandoffRequested;
|
||||
use codex_protocol::protocol::RealtimeTranscriptEntry;
|
||||
let mut state = RealtimeHistoryState {
|
||||
frame_enrollment: Some(true),
|
||||
..Default::default()
|
||||
};
|
||||
state.observe(&EventMsg::RealtimeConversationStarted(
|
||||
RealtimeConversationStartedEvent {
|
||||
realtime_session_id: Some("v3-call".into()),
|
||||
version: RealtimeConversationVersion::V3,
|
||||
},
|
||||
));
|
||||
let user = |text: &str| {
|
||||
RealtimeEvent::InputTranscriptDelta(RealtimeTranscriptDelta { delta: text.into() })
|
||||
};
|
||||
let user_done = |text: &str| {
|
||||
RealtimeEvent::InputTranscriptDone(RealtimeTranscriptDone { text: text.into() })
|
||||
};
|
||||
let ask = |id: &str| RealtimeHandoffRequested {
|
||||
handoff_id: id.into(),
|
||||
item_id: id.into(),
|
||||
input_transcript: "task".into(),
|
||||
active_transcript: Vec::new(),
|
||||
};
|
||||
let chunk = |text: &str| RealtimeTranscriptEntry {
|
||||
role: "user".into(),
|
||||
text: text.into(),
|
||||
};
|
||||
|
||||
// A rejected request already took this utterance from the socket but did
|
||||
// not commit its frame to model history. An identical later chunk must keep it.
|
||||
let old = observe_realtime(&mut state, user_done("repeat"))
|
||||
.items
|
||||
.remove(0)
|
||||
.id;
|
||||
let request = ask("rejected");
|
||||
let rejected = state.prepare_handoff(&request).unwrap();
|
||||
state.reject_handoff(rejected.id);
|
||||
observe_realtime(&mut state, RealtimeEvent::HandoffRequested(request));
|
||||
observe_realtime(&mut state, user_done("repeat"));
|
||||
state.consume_recorded_chunk(&[chunk("repeat")]);
|
||||
|
||||
// User starts first; assistant finishes first. Handoff sources must follow
|
||||
// the socket's capture order, not the order in which the frames sealed.
|
||||
let user_id = observe_realtime(&mut state, user("prefix"))
|
||||
.transcript_stream
|
||||
.unwrap()
|
||||
.item_id;
|
||||
let assistant_id = observe_realtime(
|
||||
&mut state,
|
||||
RealtimeEvent::OutputTranscriptDone(RealtimeTranscriptDone {
|
||||
text: "reply".into(),
|
||||
}),
|
||||
)
|
||||
.items
|
||||
.remove(0)
|
||||
.id;
|
||||
let request = ask("accepted");
|
||||
let accepted = state.prepare_handoff(&request).unwrap();
|
||||
assert_eq!(accepted.sources, vec![old, user_id, assistant_id]);
|
||||
state.finish_handoff(accepted.id, /*turn_id*/ None);
|
||||
observe_realtime(&mut state, RealtimeEvent::HandoffRequested(request));
|
||||
|
||||
// The transport final repeats the accepted prefix; Core seals only its
|
||||
// suffix. Recording the chunk must still retire that exact suffix.
|
||||
let final_text = "prefix plus more";
|
||||
assert_eq!(
|
||||
observe_realtime(&mut state, user_done(final_text))
|
||||
.items
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
state.consume_recorded_chunk(&[chunk(final_text)]);
|
||||
assert!(state.prepare_carryover("ended").is_none());
|
||||
}
|
||||
|
||||
@@ -330,8 +330,13 @@ pub(super) async fn shutdown_session_runtime(sess: &Arc<Session>) {
|
||||
if let Some(startup_prewarm) = startup_prewarm {
|
||||
startup_prewarm.abort().await;
|
||||
}
|
||||
let _ = sess.conversation.shutdown().await;
|
||||
sess.abort_all_tasks(TurnAbortReason::Interrupted).await;
|
||||
let (conversation_shutdown, ()) = tokio::join!(
|
||||
sess.conversation.shutdown(),
|
||||
sess.abort_all_tasks(TurnAbortReason::Interrupted),
|
||||
);
|
||||
if let Err(err) = conversation_shutdown {
|
||||
warn!("failed to shut down realtime conversation: {err}");
|
||||
}
|
||||
if let Some(reporting) = &sess.outcome_reporting {
|
||||
reporting.handoffs.wait().await;
|
||||
}
|
||||
@@ -688,17 +693,16 @@ pub(super) async fn submission_loop(
|
||||
let _ = reply.send(result);
|
||||
false
|
||||
}
|
||||
Op::RealtimeConversationEnd { realtime_session_id, reply } => {
|
||||
crate::realtime_conversation::handle_end(&sess, sub.id, &realtime_session_id, reply).await;
|
||||
false
|
||||
}
|
||||
Op::RealtimeConversationDetach {
|
||||
realtime_session_id,
|
||||
reply,
|
||||
} => {
|
||||
crate::realtime_conversation::handle_detach(
|
||||
&sess,
|
||||
Some(&realtime_session_id),
|
||||
&sub.id,
|
||||
)
|
||||
.await;
|
||||
let _ = reply.send(Ok(()));
|
||||
crate::realtime_conversation::handle_detach(&sess, &realtime_session_id, reply)
|
||||
.await;
|
||||
false
|
||||
}
|
||||
Op::RealtimeConversationAudio(params) => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::RealtimeRecord;
|
||||
use super::TurnInput as PendingTurnInput;
|
||||
use super::session::Session;
|
||||
use super::turn_context::TurnContext;
|
||||
@@ -7,6 +8,7 @@ use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_thread_store::PersistContext;
|
||||
use std::sync::Arc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
@@ -26,6 +28,7 @@ impl Session {
|
||||
pub(crate) async fn history_injection_context(
|
||||
self: &Arc<Self>,
|
||||
) -> CodexResult<Arc<TurnContext>> {
|
||||
self.conversation.check_history_recovery()?;
|
||||
if self.reference_context_item().await.is_some() {
|
||||
let turn = self.new_inject_items_context().await;
|
||||
// Compaction can clear the baseline while we build this context.
|
||||
@@ -193,6 +196,26 @@ impl Session {
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(crate) async fn record_realtime_session_chunk(
|
||||
&self,
|
||||
turn: &TurnContext,
|
||||
item: ResponseItemEnvelope,
|
||||
) -> std::io::Result<()> {
|
||||
self.try_ensure_rollout_materialized(PersistContext::Standard)
|
||||
.await?;
|
||||
let (items, images) = self
|
||||
.prepare_conversation_items_for_history(turn, turn.model_info(), vec![item])
|
||||
.await;
|
||||
self.record_prepared_items(
|
||||
turn,
|
||||
turn.model_info(),
|
||||
items,
|
||||
images,
|
||||
Some(RealtimeRecord::Chunk),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Injects items into active work, or records them without starting a turn.
|
||||
pub(crate) async fn inject_no_new_turn(
|
||||
&self,
|
||||
|
||||
@@ -278,7 +278,6 @@ mod model_catalog_settings;
|
||||
mod model_ids;
|
||||
pub(crate) mod multi_agents;
|
||||
mod plugin_selection;
|
||||
#[expect(dead_code, reason = "Activated with hangup in the next PR")]
|
||||
mod realtime_handoff;
|
||||
mod realtime_history;
|
||||
mod retained_context;
|
||||
@@ -456,6 +455,11 @@ pub(crate) enum NetworkApprovalTurn {
|
||||
AnyActive,
|
||||
}
|
||||
|
||||
enum RealtimeRecord<'a> {
|
||||
Handoff(&'a crate::realtime_history::handoff::PreparedRealtimeInput),
|
||||
Chunk,
|
||||
}
|
||||
|
||||
/// Queue and lifecycle endpoints for a running [`Session`].
|
||||
///
|
||||
/// Runtime state lives on `Session`; keeping these endpoints separate lets all
|
||||
@@ -3041,7 +3045,7 @@ impl Session {
|
||||
self.deliver_event_raw(event).await;
|
||||
}
|
||||
|
||||
async fn deliver_event_raw(&self, event: Event) {
|
||||
pub(crate) async fn deliver_event_raw(&self, event: Event) {
|
||||
if let Some(parent_tx) = self.parent_observer_event_tx.get() {
|
||||
let forwarded = match &event.msg {
|
||||
EventMsg::RawResponseItem(item) => {
|
||||
@@ -4017,12 +4021,10 @@ impl Session {
|
||||
&self,
|
||||
input: &[ResolvedUserInput],
|
||||
) -> ResponseItem {
|
||||
self.response_item_from_user_input_with_image_positions(input)
|
||||
.0
|
||||
Self::response_item_from_user_input_with_image_positions(input).0
|
||||
}
|
||||
|
||||
fn response_item_from_user_input_with_image_positions(
|
||||
&self,
|
||||
input: &[ResolvedUserInput],
|
||||
) -> (ResponseItem, HashMap<usize, usize>) {
|
||||
let (message, image_positions) =
|
||||
@@ -4108,7 +4110,7 @@ impl Session {
|
||||
model_info: &ModelInfo,
|
||||
mut items: Vec<ResponseItemEnvelope>,
|
||||
image_preparations: Vec<ImagePreparationMetadata>,
|
||||
realtime: Option<crate::realtime_history::handoff::PreparedRealtimeInput>,
|
||||
realtime: Option<RealtimeRecord<'_>>,
|
||||
) -> std::io::Result<()> {
|
||||
// Save the originating history budget for replay.
|
||||
// Preserve any existing tool-specific override.
|
||||
@@ -4136,9 +4138,7 @@ impl Session {
|
||||
let force_mcp_checkpoint = items
|
||||
.iter()
|
||||
.any(|envelope| crate::context_manager::is_user_turn_boundary(&envelope.item));
|
||||
let cached_retry = realtime
|
||||
.as_ref()
|
||||
.is_some_and(|input| input.cached_envelope.is_some());
|
||||
let cached_retry = matches!(&realtime, Some(RealtimeRecord::Handoff(input)) if input.cached_envelope.is_some());
|
||||
let mcp_revision = (!cached_retry)
|
||||
.then(|| {
|
||||
self.services
|
||||
@@ -4219,7 +4219,7 @@ impl Session {
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(prepared) = &realtime {
|
||||
if let Some(RealtimeRecord::Handoff(prepared)) = &realtime {
|
||||
let envelope = if let Some(cached) = &prepared.cached_envelope {
|
||||
cached.clone()
|
||||
} else {
|
||||
@@ -4245,6 +4245,24 @@ impl Session {
|
||||
}
|
||||
response_items = vec![envelope.item.clone()];
|
||||
items = vec![envelope];
|
||||
} else if matches!(&realtime, Some(RealtimeRecord::Chunk)) {
|
||||
let [envelope] = items.as_slice() else {
|
||||
return Err(std::io::Error::other(
|
||||
"realtime chunk must contain one input",
|
||||
));
|
||||
};
|
||||
let envelope = self
|
||||
.record_realtime_chunk_input(
|
||||
&mut state,
|
||||
envelope.clone(),
|
||||
realtime_settings_event,
|
||||
policy,
|
||||
)
|
||||
.await?;
|
||||
if realtime_initial_settings {
|
||||
state.take_initial_settings_pending();
|
||||
}
|
||||
response_items = vec![envelope.item];
|
||||
} else {
|
||||
state
|
||||
.pending_audio_duration_ids
|
||||
@@ -4263,9 +4281,11 @@ impl Session {
|
||||
metadata: image,
|
||||
});
|
||||
}
|
||||
let persisted = if let Some(prepared) = &realtime {
|
||||
let persisted = if let Some(RealtimeRecord::Handoff(prepared)) = &realtime {
|
||||
self.finish_realtime_handoff(prepared, &items[0]).await;
|
||||
true
|
||||
} else if matches!(&realtime, Some(RealtimeRecord::Chunk)) {
|
||||
true
|
||||
} else {
|
||||
let settings_guard =
|
||||
if force_mcp_checkpoint && self.reference_context_item().await.is_none() {
|
||||
@@ -6098,7 +6118,7 @@ impl Session {
|
||||
// Emit full prompt telemetry only for inputs selected for recording.
|
||||
turn_context.session_telemetry.user_prompt(&display_input);
|
||||
let (mut response_item, user_image_content_indices) =
|
||||
self.response_item_from_user_input_with_image_positions(input);
|
||||
Self::response_item_from_user_input_with_image_positions(input);
|
||||
if let Some(uuid) = metadata.response_item_uuid {
|
||||
response_item.set_id(Some(ResponseItemId::for_admitted_user_input(uuid)));
|
||||
}
|
||||
@@ -6138,7 +6158,7 @@ impl Session {
|
||||
model_info,
|
||||
prepared_items,
|
||||
image_preparations,
|
||||
realtime.clone(),
|
||||
realtime.as_ref().map(RealtimeRecord::Handoff),
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -6,9 +6,11 @@ use crate::realtime_conversation::RealtimeConversationManager;
|
||||
use crate::realtime_history::handoff::HandoffSettlement;
|
||||
use crate::realtime_history::handoff::PreparedRealtimeInput;
|
||||
use crate::state::SessionState;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::RealtimeHandoffMetadata;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::ResponseItemId;
|
||||
use codex_protocol::protocol::CodexErrorInfo;
|
||||
use codex_protocol::protocol::ErrorEvent;
|
||||
use codex_protocol::protocol::Event;
|
||||
@@ -57,6 +59,48 @@ impl Drop for RealtimeSealGuard {
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Persist and flush the projected chunk before publishing its sole model input.
|
||||
pub(super) async fn record_realtime_chunk_input(
|
||||
&self,
|
||||
state: &mut SessionState,
|
||||
mut envelope: ResponseItemEnvelope,
|
||||
settings_event: Option<EventMsg>,
|
||||
policy: TruncationPolicy,
|
||||
) -> std::io::Result<ResponseItemEnvelope> {
|
||||
let is_chunk = envelope
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_input.is_some());
|
||||
if self.conversation.history_recovery_required() || !is_chunk {
|
||||
return Err(std::io::Error::other("invalid realtime chunk input"));
|
||||
}
|
||||
state.history.prepare_item_sampling(&mut envelope);
|
||||
let staged = state.history.stage_realtime_input(&mut envelope);
|
||||
let items = settings_event
|
||||
.into_iter()
|
||||
.map(RolloutItem::EventMsg)
|
||||
.chain(std::iter::once(RolloutItem::ResponseItem(envelope.clone())))
|
||||
.collect::<Vec<_>>();
|
||||
let live = self
|
||||
.live_thread_for_persistence("record realtime transcript chunk")
|
||||
.map_err(std::io::Error::other)?;
|
||||
let mut guard = RealtimeSealGuard::new(Arc::clone(&self.conversation));
|
||||
live.append_items(&items)
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
live.flush().await.map_err(std::io::Error::other)?;
|
||||
if self.conversation.history_recovery_required() {
|
||||
return Err(std::io::Error::other(
|
||||
"realtime history recovery is required",
|
||||
));
|
||||
}
|
||||
state
|
||||
.history
|
||||
.record_sealed_realtime_input(&envelope, staged, policy);
|
||||
guard.disarm();
|
||||
Ok(envelope)
|
||||
}
|
||||
|
||||
pub(super) async fn record_realtime_handoff_input(
|
||||
&self,
|
||||
state: &mut SessionState,
|
||||
@@ -110,6 +154,11 @@ impl Session {
|
||||
seal_guard.disarm();
|
||||
return Err(std::io::Error::other(error));
|
||||
}
|
||||
if self.conversation.history_recovery_required() {
|
||||
return Err(std::io::Error::other(
|
||||
"realtime history recovery is required",
|
||||
));
|
||||
}
|
||||
// Fresh recording exposes the source only after capturing the review backup;
|
||||
// a cached retry replays its already persisted provenance.
|
||||
let fresh_source = if replay {
|
||||
@@ -222,10 +271,7 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn route_realtime_text_input(
|
||||
self: &Arc<Self>,
|
||||
text: String,
|
||||
) -> Result<(), &'static str> {
|
||||
async fn route_realtime_text_input(self: &Arc<Self>, text: String) -> Result<(), &'static str> {
|
||||
let submission_id = Uuid::now_v7().to_string();
|
||||
let submission = turn_input::handle(
|
||||
self,
|
||||
@@ -297,7 +343,8 @@ impl Session {
|
||||
.lock()
|
||||
.await
|
||||
.pending_handoff(id)?;
|
||||
(prepared.reserved && prepared.input == *text).then_some(prepared)
|
||||
(prepared.reserved && prepared.receipt.is_none() && prepared.input == *text)
|
||||
.then_some(prepared)
|
||||
}
|
||||
|
||||
pub(crate) async fn realtime_handoff_metadata(
|
||||
@@ -405,6 +452,47 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "Keep durable carryover and live model history in the same order as agent input."
|
||||
)]
|
||||
pub(crate) async fn record_realtime_carryover(&self, instruction: &str) -> std::io::Result<()> {
|
||||
let history = self
|
||||
.realtime_history
|
||||
.as_ref()
|
||||
.ok_or_else(|| std::io::Error::other("realtime history is disabled"))?;
|
||||
let Some(prepared) = history.lock().await.prepare_carryover(instruction) else {
|
||||
return Ok(());
|
||||
};
|
||||
let receipt = prepared
|
||||
.receipt
|
||||
.as_ref()
|
||||
.ok_or_else(|| std::io::Error::other("missing realtime carryover receipt"))?;
|
||||
let mut input = Self::realtime_carryover_input(receipt)
|
||||
.ok_or_else(|| std::io::Error::other("invalid realtime carryover receipt"))?;
|
||||
let mut records = prepared.new_items.clone();
|
||||
records.push(receipt.clone());
|
||||
let context = self.new_inject_items_context().await;
|
||||
{
|
||||
let mut state = self.state.lock().await;
|
||||
self.live_thread_for_persistence("retain realtime context")
|
||||
.map_err(std::io::Error::other)?
|
||||
.seal_realtime_items(records.clone())
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
state.history.record_annotated_items(
|
||||
std::slice::from_mut(&mut input),
|
||||
context.model_info().truncation_policy.into(),
|
||||
);
|
||||
}
|
||||
history
|
||||
.lock()
|
||||
.await
|
||||
.finish_handoff(prepared.id, /*turn_id*/ None);
|
||||
self.publish_realtime_acceptance(&records).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish_realtime_acceptance(&self, records: &[RealtimeItem]) {
|
||||
for item in records {
|
||||
let event = |payload| Event {
|
||||
@@ -421,4 +509,35 @@ impl Session {
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Hangup has no ordinary turn input. Its receipt is the only durable copy and
|
||||
/// reconstructs context for the next real turn without creating a boundary.
|
||||
pub(super) fn realtime_carryover_input(item: &RealtimeItem) -> Option<ResponseItemEnvelope> {
|
||||
let RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
input_item_id,
|
||||
input,
|
||||
..
|
||||
} = &item.content
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let (mut response, _) =
|
||||
Self::response_item_from_user_input_with_image_positions(&[ResolvedUserInput::text(
|
||||
input.clone(),
|
||||
)]);
|
||||
response.set_id(Some(ResponseItemId::from_server(input_item_id.clone())));
|
||||
if let Some(observed_at_ms) = item.observed_at_ms {
|
||||
response.set_create_time_if_missing(serde_json::Number::from_f64(
|
||||
observed_at_ms as f64 / 1000.0,
|
||||
)?);
|
||||
}
|
||||
Some(ResponseItemEnvelope {
|
||||
item: response,
|
||||
metadata: Some(CodexHarnessMetadata {
|
||||
realtime_session_end_input: true,
|
||||
..Default::default()
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,43 @@ use codex_protocol::protocol::RealtimeConversationRealtimeEvent;
|
||||
use codex_protocol::protocol::RealtimeEvent;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
|
||||
fn realtime_store_read_error(error: codex_thread_store::ThreadStoreError) -> std::io::Error {
|
||||
match error {
|
||||
error @ codex_thread_store::ThreadStoreError::Retryable { .. } => {
|
||||
std::io::Error::new(std::io::ErrorKind::WouldBlock, error)
|
||||
}
|
||||
error => std::io::Error::other(error),
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// A successful close must mean the canonical close marker was sealed.
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "Keep realtime event reduction, close persistence, and delivery in the same order."
|
||||
)]
|
||||
pub(crate) async fn seal_realtime_close(&self, event: Event) -> anyhow::Result<()> {
|
||||
let Some(history) = &self.realtime_history else {
|
||||
self.send_event_raw(event).await;
|
||||
return Ok(());
|
||||
};
|
||||
let mut history = history.lock().await;
|
||||
if !history.frames_enabled() {
|
||||
drop(history);
|
||||
self.send_event_raw(event).await;
|
||||
return Ok(());
|
||||
}
|
||||
self.services.mcp_runtime.observe_event(&event.msg);
|
||||
let effects = history.observe(&event.msg);
|
||||
self.send_realtime_history_effects(&event.id, effects)
|
||||
.await?;
|
||||
self.services
|
||||
.rollout_thread_trace
|
||||
.record_protocol_event(&event.msg);
|
||||
self.deliver_event_raw(event).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Start and attach operations run sequentially in the submission loop.
|
||||
/// Cache enrollment after a successful lookup so a failed start can retry.
|
||||
pub(crate) async fn initialize_realtime_history(&self) -> std::io::Result<bool> {
|
||||
@@ -22,7 +58,7 @@ impl Session {
|
||||
.thread_store
|
||||
.realtime_frames_enabled(self.thread_id())
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
.map_err(realtime_store_read_error)?;
|
||||
let floor = if enabled
|
||||
&& matches!(
|
||||
self.history_initialization,
|
||||
@@ -32,7 +68,7 @@ impl Session {
|
||||
.thread_store
|
||||
.realtime_frame_clock_floor(self.thread_id())
|
||||
.await
|
||||
.map_err(std::io::Error::other)?
|
||||
.map_err(realtime_store_read_error)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
@@ -284,9 +284,13 @@ impl ContextManager {
|
||||
| RolloutItem::EventMsg(_) => None,
|
||||
})
|
||||
.peekable();
|
||||
let last_started_turn_id = started_turn_ids
|
||||
.peek()
|
||||
.map(|id| (*id).to_owned())
|
||||
let last_started_turn_id = replay_items
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|item| match item {
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.or_else(|| resume_metadata.and_then(|metadata| metadata.last_started_turn_id.clone()));
|
||||
// Saved attribution still identifies the turn when continuation eligibility was cleared.
|
||||
let checkpoint_turn_id = resume_metadata.and_then(|metadata| {
|
||||
@@ -534,8 +538,37 @@ impl ContextManager {
|
||||
}
|
||||
let rollout_suffix =
|
||||
history_checkpoint.map_or(rollout_items, |checkpoint| checkpoint.suffix);
|
||||
// A compacted replacement may already contain a reconstructed hangup input.
|
||||
let tail_ids = rollout_suffix
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
RolloutItem::RealtimeItem(item) => match &item.content {
|
||||
codex_protocol::realtime::RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
input_item_id,
|
||||
..
|
||||
} => Some(input_item_id.as_str()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
let mut saved_tails = history
|
||||
.raw_items()
|
||||
.filter_map(|item| item.id())
|
||||
.filter(|id| tail_ids.contains(id.as_str()))
|
||||
.map(ToString::to_string)
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
for item in rollout_suffix {
|
||||
match item {
|
||||
RolloutItem::RealtimeItem(item) => {
|
||||
if let Some(input) = Session::realtime_carryover_input(item)
|
||||
&& let Some(id) = input.item.id()
|
||||
&& saved_tails.insert(id.to_string())
|
||||
{
|
||||
history.replay_annotated_item(&input, truncation_policy);
|
||||
}
|
||||
}
|
||||
RolloutItem::RetainedContext(event) => {
|
||||
history.record_retained_context(event);
|
||||
}
|
||||
@@ -573,7 +606,6 @@ impl ContextManager {
|
||||
}
|
||||
RolloutItem::EventMsg(_)
|
||||
| RolloutItem::TurnContext(_)
|
||||
| RolloutItem::RealtimeItem(_)
|
||||
| RolloutItem::WorldState(_)
|
||||
| RolloutItem::SecurityRiskScore(_)
|
||||
| RolloutItem::TokenUsageRecord(_)
|
||||
|
||||
@@ -2882,3 +2882,118 @@ async fn record_initial_history_resumed_replaced_incomplete_compacted_turn_clear
|
||||
);
|
||||
assert!(session.reference_context_item().await.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn annotated_realtime_input_replays_as_the_ordinary_input() {
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::RealtimeHandoffMetadata;
|
||||
use codex_protocol::ResponseItemId;
|
||||
use codex_protocol::realtime::RealtimeHandoffMode;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let mut item = user_message("accepted exactly");
|
||||
item.set_id(Some(ResponseItemId::from_server("msg_acceptance".into())));
|
||||
item.set_turn_id_if_missing("accepted-turn");
|
||||
let envelope = ResponseItemEnvelope {
|
||||
item,
|
||||
metadata: Some(CodexHarnessMetadata {
|
||||
realtime_handoff: Some(Box::new(RealtimeHandoffMetadata {
|
||||
event_id: "event_acceptance".into(),
|
||||
realtime_session_id: "voice".into(),
|
||||
observed_at_ms: 42,
|
||||
handoff_id: "handoff".into(),
|
||||
mode: RealtimeHandoffMode::Start,
|
||||
source_event_ids: vec!["speech".into()],
|
||||
})),
|
||||
..Default::default()
|
||||
}),
|
||||
};
|
||||
assert!(matches!(
|
||||
envelope.realtime_handoff_item().unwrap().content,
|
||||
RealtimeItemContent::TurnInputAccepted { turn_id, input, .. }
|
||||
if turn_id == "accepted-turn" && input == "accepted exactly"
|
||||
));
|
||||
let restored = session
|
||||
.reconstruct_history_from_rollout(&turn, &[RolloutItem::ResponseItem(envelope.clone())])
|
||||
.await;
|
||||
assert_eq!(restored.history.len(), 1);
|
||||
assert_eq!(restored.history[0].item, envelope.item);
|
||||
assert_eq!(
|
||||
restored.history[0]
|
||||
.metadata
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.realtime_handoff,
|
||||
envelope.metadata.as_ref().unwrap().realtime_handoff
|
||||
);
|
||||
assert_eq!(restored.last_started_turn_id, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn hangup_input_replays_once_and_does_not_create_a_rollback_boundary() {
|
||||
use codex_protocol::realtime::RealtimeItem;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let receipt = RealtimeItem {
|
||||
id: "event_tail".into(),
|
||||
realtime_session_id: "voice".into(),
|
||||
observed_at_ms: Some(42),
|
||||
content: RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
source_event_ids: vec!["speech".into()],
|
||||
input_item_id: "msg_tail".into(),
|
||||
input: "use the smaller logo".into(),
|
||||
},
|
||||
};
|
||||
let tail = Session::realtime_carryover_input(&receipt).unwrap();
|
||||
let turn_a = user_message("turn A");
|
||||
let turn_b = user_message("turn B");
|
||||
let rollback = RolloutItem::EventMsg(EventMsg::ThreadRolledBack(ThreadRolledBackEvent {
|
||||
num_turns: 1,
|
||||
}));
|
||||
let records = vec![
|
||||
RolloutItem::ResponseItem(turn_a.clone().into()),
|
||||
RolloutItem::RealtimeItem(receipt.clone()),
|
||||
RolloutItem::RealtimeItem(receipt.clone()),
|
||||
];
|
||||
let restored = session
|
||||
.reconstruct_history_from_rollout(&turn, &records)
|
||||
.await;
|
||||
assert_eq!(restored.history.len(), 2);
|
||||
assert_eq!(restored.history[0].item, turn_a);
|
||||
assert_eq!(restored.history[1].item, tail.item);
|
||||
assert!(
|
||||
restored.history[1]
|
||||
.metadata
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.realtime_session_end_input
|
||||
);
|
||||
|
||||
let mut rollback_a = records.clone();
|
||||
rollback_a.push(rollback.clone());
|
||||
assert!(
|
||||
session
|
||||
.reconstruct_history_from_rollout(&turn, &rollback_a)
|
||||
.await
|
||||
.history
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let mut rollback_b = records;
|
||||
rollback_b.push(RolloutItem::ResponseItem(turn_b.into()));
|
||||
rollback_b.push(rollback);
|
||||
let restored = session
|
||||
.reconstruct_history_from_rollout(&turn, &rollback_b)
|
||||
.await;
|
||||
assert_eq!(
|
||||
restored
|
||||
.history
|
||||
.iter()
|
||||
.map(|item| &item.item)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![&turn_a, &tail.item]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -630,10 +630,12 @@ async fn warm_plugins_and_skills_for_session_init(
|
||||
|
||||
impl Session {
|
||||
pub(crate) fn turn_starts_closed(&self) -> bool {
|
||||
self.services
|
||||
.local_agent_runtime
|
||||
.suspension_root()
|
||||
.is_some()
|
||||
self.conversation.history_recovery_required()
|
||||
|| self
|
||||
.services
|
||||
.local_agent_runtime
|
||||
.suspension_root()
|
||||
.is_some()
|
||||
|| self
|
||||
.outcome_reporting
|
||||
.as_ref()
|
||||
|
||||
@@ -45,7 +45,11 @@ pub(crate) fn user_message_positions_in_rollout(items: &[RolloutItem]) -> Vec<us
|
||||
for (idx, item) in items.iter().enumerate() {
|
||||
match item {
|
||||
RolloutItem::ResponseItem(item)
|
||||
if matches!(&item.item, ResponseItem::Message { .. })
|
||||
if !item
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
&& matches!(&item.item, ResponseItem::Message { .. })
|
||||
&& matches!(
|
||||
event_mapping::parse_turn_item(&item.item),
|
||||
Some(TurnItem::UserMessage(_))
|
||||
|
||||
@@ -297,7 +297,7 @@ else:
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_user_prompt_submit_hook(
|
||||
pub(super) fn write_user_prompt_submit_hook(
|
||||
home: &Path,
|
||||
blocked_prompt: &str,
|
||||
additional_context: &str,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use anyhow::Result;
|
||||
use codex_api::RealtimeEvent;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::protocol::ConversationStartParams;
|
||||
use codex_protocol::protocol::ConversationStartTransport;
|
||||
use codex_protocol::protocol::ConversationTextParams;
|
||||
@@ -23,6 +24,7 @@ use futures::StreamExt;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::SystemTime;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -124,7 +126,10 @@ async fn initiated_sideband_delegates_without_callback() -> Result<()> {
|
||||
#[tokio::test]
|
||||
async fn initiated_sideband_backpressure_keeps_events_and_lifecycle_live() -> Result<()> {
|
||||
let api = start_mock_server().await;
|
||||
let test = test_codex().build_with_auto_env(&api).await?;
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let mut transports = Vec::new();
|
||||
for session in ["original", "replacement"] {
|
||||
let (events, incoming) = mpsc::channel(/*buffer*/ 1);
|
||||
@@ -163,6 +168,9 @@ async fn initiated_sideband_backpressure_keeps_events_and_lifecycle_live() -> Re
|
||||
})
|
||||
.await;
|
||||
transports.push((events, commands));
|
||||
if session == "original" {
|
||||
end(&test, session).await?;
|
||||
}
|
||||
}
|
||||
detach(&test, "replacement").await?;
|
||||
for (events, mut commands) in transports {
|
||||
@@ -174,9 +182,9 @@ async fn initiated_sideband_backpressure_keeps_events_and_lifecycle_live() -> Re
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Detach is a history barrier for accepted final speech, including when both caller halves disappear.
|
||||
/// Terminal stop is a history barrier for accepted final speech.
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn initiated_sideband_detach_preserves_queued_transcript_in_history() -> Result<()> {
|
||||
async fn initiated_sideband_end_preserves_queued_transcript_in_history() -> Result<()> {
|
||||
let api = start_mock_server().await;
|
||||
let test = test_codex().build_with_auto_env(&api).await?;
|
||||
test.codex.ensure_rollout_materialized().await;
|
||||
@@ -195,7 +203,7 @@ async fn initiated_sideband_detach_preserves_queued_transcript_in_history() -> R
|
||||
let (outgoing, commands) = mpsc::channel(32);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
let mut start_params = params(&session, "ws://unused.invalid");
|
||||
// Completed turns must persist even when the optional unfinished-tail flush is off.
|
||||
// V3 ignores the old unfinished-tail setting.
|
||||
start_params.flush_transcript_tail_on_session_end = !completed_turn;
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
@@ -211,9 +219,13 @@ async fn initiated_sideband_detach_preserves_queued_transcript_in_history() -> R
|
||||
json!({"type": "input_transcript.added", "item": {"text": text}})
|
||||
};
|
||||
events.try_send(event.to_string())?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationRealtime(_))
|
||||
})
|
||||
.await;
|
||||
end(&test, &session).await?;
|
||||
drop(events);
|
||||
drop(commands);
|
||||
detach(&test, &session).await?;
|
||||
let history = test.codex.load_history(/*include_archived*/ false).await?;
|
||||
let tails = history.items.iter().filter(|item| {
|
||||
matches!(item,
|
||||
@@ -227,13 +239,696 @@ async fn initiated_sideband_detach_preserves_queued_transcript_in_history() -> R
|
||||
}).count();
|
||||
assert_eq!(
|
||||
tails, 1,
|
||||
"detach must persist the accepted final transcript exactly once"
|
||||
"terminal stop must persist the accepted final transcript exactly once"
|
||||
);
|
||||
}
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A task-only delegation already parsed before stop is routed while fanout drains.
|
||||
#[tokio::test]
|
||||
async fn terminal_stop_routes_a_parsed_task_only_handoff() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_thread_store::ThreadStore;
|
||||
use core_test_support::responses;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let response = responses::mount_sse_once(&api, responses::sse_completed("queued-task")).await;
|
||||
let store = Arc::new(HostedClockStore::blocking_frame());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.with_model_info_override("gpt-5.5", |model| model.use_responses_lite = false)
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let (events, incoming) = mpsc::channel(1);
|
||||
let (outgoing, _commands) = mpsc::channel(32);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("queued", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
let completed_transcript = "earlier speech ".repeat(400);
|
||||
events
|
||||
.send(
|
||||
json!({"type":"turn.done", "turn":{"role":"user", "transcript":completed_transcript}})
|
||||
.to_string(),
|
||||
)
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), store.wait_for_blocked_frame()).await?;
|
||||
events.send(json!({"type":"delegation.created", "item":{"id":"queued-task", "type":"delegation", "target":"client", "content":[{"type":"input_text", "text":"do this task"}]}}).to_string()).await?;
|
||||
// The one-slot input queue accepting two more events proves the delegation was parsed.
|
||||
for _ in 0..2 {
|
||||
events
|
||||
.send(json!({"type":"session.started", "session":{"id":"queued"}}).to_string())
|
||||
.await?;
|
||||
}
|
||||
let (reply, stopped) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationEnd {
|
||||
realtime_session_id: "queued".to_owned(),
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), events.closed()).await?;
|
||||
store.release_frame();
|
||||
timeout(Duration::from_secs(10), stopped).await???;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
assert_eq!(response.requests().len(), 1);
|
||||
let items = store
|
||||
.load_realtime_session(test.session_configured.thread_id, "queued".to_owned())
|
||||
.await?
|
||||
.expect("started session");
|
||||
assert_eq!(
|
||||
items
|
||||
.iter()
|
||||
.filter(|item| matches!(&item.content,
|
||||
RealtimeItemContent::TurnInputAccepted { handoff_id, source_event_ids, .. }
|
||||
if handoff_id == "queued-task" && source_event_ids.is_empty()
|
||||
))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Transport loss must arm recovery even when fanout cannot drain its bounded queue.
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn sideband_loss_with_saturated_fanout_requires_recovery_without_end() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let store = Arc::new(HostedClockStore::blocking_frame());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let (events, incoming) = mpsc::channel(1);
|
||||
let (outgoing, commands) = mpsc::channel(1);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("saturated", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
events
|
||||
.send(
|
||||
json!({"type":"turn.done", "turn":{"role":"user", "transcript":"unfinished"}})
|
||||
.to_string(),
|
||||
)
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), store.wait_for_blocked_frame()).await?;
|
||||
// Fanout's queue holds 256 events; the next event parks its producer, and
|
||||
// the final event occupies the one-slot caller queue.
|
||||
timeout(Duration::from_secs(10), async {
|
||||
for _ in 0..258 {
|
||||
events
|
||||
.send(json!({"type":"session.started", "session":{"id":"saturated"}}).to_string())
|
||||
.await?;
|
||||
}
|
||||
anyhow::Ok(())
|
||||
})
|
||||
.await??;
|
||||
tokio::time::pause();
|
||||
drop(events);
|
||||
drop(commands);
|
||||
// Let the transport watcher and watchdog run as their deadline approaches.
|
||||
for _ in 0..32 {
|
||||
tokio::time::advance(Duration::from_secs(1)).await;
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
tokio::time::resume();
|
||||
let closed = timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
if let EventMsg::RealtimeConversationClosed(closed) = test.codex.next_event().await?.msg
|
||||
{
|
||||
break anyhow::Ok(closed);
|
||||
}
|
||||
}
|
||||
})
|
||||
.await??;
|
||||
assert_eq!(closed.reason.as_deref(), Some("history_recovery_required"));
|
||||
store.release_frame();
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Realtime frames carry pending input over at logical end, including after detach.
|
||||
#[tokio::test]
|
||||
async fn frame_enrolled_v3_saves_input_even_when_legacy_flush_setting_is_false() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_protocol::realtime::SessionInputReason;
|
||||
use codex_thread_store::ThreadStore;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let store = Arc::new(HostedClockStore::default());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let (events, incoming) = mpsc::channel(1);
|
||||
let (outgoing, _commands) = mpsc::channel(1);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
let mut start = params("no-flush", "ws://unused.invalid");
|
||||
start.flush_transcript_tail_on_session_end = false;
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: start,
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
events
|
||||
.send(
|
||||
json!({"type":"input_transcript.added", "item":{"text":"save this at hangup"}})
|
||||
.to_string(),
|
||||
)
|
||||
.await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationRealtime(event)
|
||||
if matches!(event.payload, codex_protocol::protocol::RealtimeEvent::InputTranscriptDelta(_)))
|
||||
}).await;
|
||||
end(&test, "no-flush").await?;
|
||||
let items = store
|
||||
.load_realtime_session(test.session_configured.thread_id, "no-flush".to_owned())
|
||||
.await?
|
||||
.expect("started session");
|
||||
assert!(items.iter().any(|item| matches!(&item.content,
|
||||
RealtimeItemContent::SessionInputSaved { reason: SessionInputReason::SessionEnd, input, .. }
|
||||
if input.contains("save this at hangup")
|
||||
)));
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Thread-scoped Stop recovers the latest open persisted session after Core reload.
|
||||
#[tokio::test]
|
||||
async fn thread_stop_recovers_and_finishes_the_latest_persisted_session() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_protocol::realtime::RealtimeFrameChannels;
|
||||
use codex_protocol::realtime::RealtimeItem;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_protocol::realtime::RealtimeTranscriptRole;
|
||||
use codex_protocol::realtime::RealtimeVoiceTranscript;
|
||||
use codex_protocol::realtime::SessionInputReason;
|
||||
use codex_thread_store::ThreadStore;
|
||||
use core_test_support::responses;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let model = responses::mount_sse_once(
|
||||
&api,
|
||||
responses::sse(vec![responses::ev_response_created("unexpected")]),
|
||||
)
|
||||
.await;
|
||||
let store = Arc::new(HostedClockStore::default());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let thread_id = test.session_configured.thread_id;
|
||||
let session_id = "cold-stop";
|
||||
// Populate only persisted history: this Core runtime has never attached the call.
|
||||
store
|
||||
.seal_realtime_items(
|
||||
thread_id,
|
||||
vec![
|
||||
RealtimeItem {
|
||||
id: "start".to_owned(),
|
||||
realtime_session_id: session_id.to_owned(),
|
||||
observed_at_ms: Some(1),
|
||||
content: RealtimeItemContent::RealtimeSessionStarted,
|
||||
},
|
||||
RealtimeItem {
|
||||
id: "speech".to_owned(),
|
||||
realtime_session_id: session_id.to_owned(),
|
||||
observed_at_ms: Some(2),
|
||||
content: RealtimeItemContent::Frame {
|
||||
channels: RealtimeFrameChannels {
|
||||
voice_transcript: RealtimeVoiceTranscript {
|
||||
role: RealtimeTranscriptRole::User,
|
||||
text: "remember the smaller logo".to_owned(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationClosed(_))
|
||||
})
|
||||
.await;
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
let items = store
|
||||
.load_realtime_session(thread_id, session_id.to_owned())
|
||||
.await?
|
||||
.expect("persisted session");
|
||||
assert_eq!(
|
||||
items.iter().filter(|item| matches!(&item.content,
|
||||
RealtimeItemContent::SessionInputSaved { reason: SessionInputReason::SessionEnd, input, source_event_ids, .. }
|
||||
if input.contains("remember the smaller logo") && source_event_ids == &["speech".to_owned()]
|
||||
)).count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
items
|
||||
.iter()
|
||||
.filter(|item| matches!(
|
||||
item.content,
|
||||
RealtimeItemContent::RealtimeSessionClosed { .. }
|
||||
))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert!(model.requests().is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A timeout fences an allowing steer hook and lets ordinary turn abort own cleanup.
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn hangup_timeout_aborts_a_turn_with_an_allowing_steer_hook() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_core::TurnInputRequest;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_thread_store::LoadThreadHistoryParams;
|
||||
use codex_thread_store::ThreadStore;
|
||||
use core_test_support::responses;
|
||||
use core_test_support::streaming_sse::StreamingSseChunk;
|
||||
use core_test_support::streaming_sse::start_streaming_sse_server;
|
||||
|
||||
core_test_support::skip_if_wine_exec!(Ok(()), "the hook requires host-native Python");
|
||||
let (release_response, response_gate) = oneshot::channel();
|
||||
let (server, _completions) = start_streaming_sse_server(vec![vec![
|
||||
StreamingSseChunk {
|
||||
gate: None,
|
||||
body: responses::sse(vec![responses::ev_response_created("active")]),
|
||||
},
|
||||
StreamingSseChunk {
|
||||
gate: Some(response_gate),
|
||||
body: responses::sse(vec![responses::ev_completed("active")]),
|
||||
},
|
||||
]])
|
||||
.await;
|
||||
let store = Arc::new(HostedClockStore::default());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.with_config(core_test_support::hooks::trust_discovered_hooks)
|
||||
.with_pre_build_hook(|home| {
|
||||
super::hooks::write_user_prompt_submit_hook(home, "never-block", "")
|
||||
.expect("write allowing prompt hook");
|
||||
let script = home.join("user_prompt_submit_hook.py");
|
||||
let original = std::fs::read_to_string(&script).expect("read hook");
|
||||
let gate = format!(
|
||||
"payload = json.load(sys.stdin)\nif 'steer-timeout' in payload.get('prompt', ''):\n import time\n Path(r\"{}\").touch()\n while not Path(r\"{}\").exists():\n time.sleep(0.01)",
|
||||
home.join("hook_started").display(),
|
||||
home.join("hook_release").display(),
|
||||
);
|
||||
std::fs::write(script, original.replace("payload = json.load(sys.stdin)", &gate))
|
||||
.expect("gate allowing steer hook");
|
||||
})
|
||||
.build_with_streaming_server(&server)
|
||||
.await?;
|
||||
let started = test
|
||||
.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "initial active turn".to_owned(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.await?;
|
||||
let turn_id = match started {
|
||||
codex_core::TurnInputSubmission::Started { turn_id, .. } => turn_id,
|
||||
other => anyhow::bail!("expected started turn, got {other:?}"),
|
||||
};
|
||||
timeout(
|
||||
Duration::from_secs(10),
|
||||
server.wait_for_request_count(/*count*/ 1),
|
||||
)
|
||||
.await?;
|
||||
let (events, incoming) = mpsc::channel(8);
|
||||
let (outgoing, _commands) = mpsc::channel(8);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("hook-timeout", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
events.send(json!({"type":"delegation.created", "item":{"id":"steer-timeout", "type":"delegation", "target":"client", "content":[{"type":"input_text", "text":"steer-timeout"}]}}).to_string()).await?;
|
||||
wait_for_event(&test.codex, |event| matches!(event,
|
||||
EventMsg::RealtimeConversationRealtime(event)
|
||||
if matches!(&event.payload, RealtimeEvent::HandoffRequested(handoff) if handoff.handoff_id == "steer-timeout")
|
||||
)).await;
|
||||
release_response.send(()).expect("first response gate");
|
||||
let started = test.home.path().join("hook_started");
|
||||
timeout(Duration::from_secs(10), async {
|
||||
while !started.exists() {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
let (reply, mut stopped) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationEnd {
|
||||
realtime_session_id: "hook-timeout".to_owned(),
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
assert!(
|
||||
timeout(Duration::from_millis(100), &mut stopped)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
tokio::time::pause();
|
||||
tokio::time::advance(Duration::from_secs(31)).await;
|
||||
let result = timeout(Duration::from_secs(10), stopped).await;
|
||||
tokio::time::resume();
|
||||
assert!(result??.is_err(), "timeout must require recovery");
|
||||
std::fs::write(test.home.path().join("hook_release"), "")?;
|
||||
wait_for_event(&test.codex, |event| matches!(event, EventMsg::TurnAborted(aborted) if aborted.turn_id.as_deref() == Some(turn_id.as_str()))).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
assert_eq!(
|
||||
server.requests().await.len(),
|
||||
1,
|
||||
"the released steer must not sample"
|
||||
);
|
||||
let history = store
|
||||
.load_latest_model_context(LoadThreadHistoryParams {
|
||||
thread_id: test.session_configured.thread_id,
|
||||
include_archived: false,
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(history.items.iter().filter(|item| matches!(item,
|
||||
RolloutItem::EventMsg(EventMsg::TurnAborted(aborted)) if aborted.turn_id.as_deref() == Some(turn_id.as_str())
|
||||
) || matches!(item,
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(completed)) if completed.turn_id == turn_id
|
||||
)).count(), 1);
|
||||
let session = store
|
||||
.load_realtime_session(test.session_configured.thread_id, "hook-timeout".to_owned())
|
||||
.await?
|
||||
.expect("started session");
|
||||
assert!(!session.iter().any(|item| matches!(&item.content,
|
||||
RealtimeItemContent::TurnInputAccepted { handoff_id, .. } if handoff_id == "steer-timeout"
|
||||
)));
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
server.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reattaching an open logical call does not start a second session; a later cold stop closes it.
|
||||
#[tokio::test]
|
||||
async fn reconnect_restores_sealed_pending_speech_and_rejects_closed_reuse() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_protocol::realtime::SessionInputReason;
|
||||
use codex_thread_store::ThreadStore;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let store = Arc::new(HostedClockStore::default());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
for attach in 0..2 {
|
||||
let (events, incoming) = mpsc::channel(8);
|
||||
let (outgoing, _commands) = mpsc::channel(8);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("reconnect", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
if attach == 0 {
|
||||
events
|
||||
.send(json!({"type":"input_transcript.added", "item":{"text":"use the smaller logo"}}).to_string())
|
||||
.await?;
|
||||
events
|
||||
.send(json!({"type":"turn.done", "turn":{"role":"user","transcript":"use the smaller logo"}}).to_string())
|
||||
.await?;
|
||||
// Detach may discard unsealed deltas; recover only after the frame is durable.
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event,
|
||||
EventMsg::RealtimeConversationRealtime(event)
|
||||
if matches!(&event.payload,
|
||||
codex_protocol::protocol::RealtimeEvent::HistoryItemCompleted(item)
|
||||
if matches!(&item.content, RealtimeItemContent::Frame { .. })
|
||||
)
|
||||
)
|
||||
})
|
||||
.await;
|
||||
}
|
||||
detach(&test, "reconnect").await?;
|
||||
}
|
||||
end(&test, "reconnect").await?;
|
||||
end(&test, "reconnect").await?;
|
||||
let items = store
|
||||
.load_realtime_session(test.session_configured.thread_id, "reconnect".to_owned())
|
||||
.await?
|
||||
.expect("started session");
|
||||
assert_eq!(
|
||||
items
|
||||
.iter()
|
||||
.filter(|item| matches!(item.content, RealtimeItemContent::RealtimeSessionStarted))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
items
|
||||
.iter()
|
||||
.filter(|item| matches!(
|
||||
item.content,
|
||||
RealtimeItemContent::RealtimeSessionClosed { .. }
|
||||
))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
let frame = items
|
||||
.iter()
|
||||
.find(|item| matches!(item.content, RealtimeItemContent::Frame { .. }))
|
||||
.expect("sealed frame");
|
||||
assert!(items.iter().any(|item| matches!(&item.content,
|
||||
RealtimeItemContent::SessionInputSaved { reason: SessionInputReason::SessionEnd, input, source_event_ids, .. }
|
||||
if input.contains("use the smaller logo") && source_event_ids.as_slice() == std::slice::from_ref(&frame.id)
|
||||
)));
|
||||
let (_events, incoming) = mpsc::channel(8);
|
||||
let (outgoing, _commands) = mpsc::channel(8);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("reconnect", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
assert!(timeout(Duration::from_secs(10), ready).await??.is_err());
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An uncertain but committed voice input keeps its place ahead of later typed input on replay.
|
||||
#[tokio::test]
|
||||
async fn committed_handoff_retry_preserves_live_and_recovery_input_order() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_thread_store::LoadThreadHistoryParams;
|
||||
use core_test_support::responses;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let model = responses::mount_sse_sequence(
|
||||
&api,
|
||||
["voice", "typed"].map(responses::sse_completed).into(),
|
||||
)
|
||||
.await;
|
||||
let store = Arc::new(HostedClockStore::failing_after_input_commit());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_model_info_override("gpt-5.5", |model| model.use_responses_lite = false)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let (events, incoming) = mpsc::channel(32);
|
||||
let (outgoing, _commands) = mpsc::channel(32);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("order", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
events
|
||||
.send(json!({"type":"delegation.created","item":{"id":"handoff-A","type":"delegation","target":"client","content":[{"type":"input_text","text":"voice A"}]}}).to_string())
|
||||
.await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
assert_eq!(store.input_attempts(), 2);
|
||||
test.submit_text_turn("typed B").await?;
|
||||
|
||||
let label = |text: &str| {
|
||||
if text.contains("<input>voice A</input>") {
|
||||
Some("A")
|
||||
} else if text == "typed B" {
|
||||
Some("B")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
let live = model.requests()[1]
|
||||
.message_input_texts("user")
|
||||
.iter()
|
||||
.filter_map(|text| label(text))
|
||||
.collect::<Vec<_>>();
|
||||
let context = test
|
||||
.thread_store
|
||||
.load_latest_model_context(LoadThreadHistoryParams {
|
||||
thread_id: test.session_configured.thread_id,
|
||||
include_archived: true,
|
||||
})
|
||||
.await?;
|
||||
let recovery = context
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
RolloutItem::ResponseItem(envelope) => match &envelope.item {
|
||||
ResponseItem::Message { role, content, .. } if role == "user" => Some(content),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.flatten()
|
||||
.filter_map(|content| match content {
|
||||
ContentItem::InputText { text } => label(text),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!((live, recovery), (vec!["A", "B"], vec!["A", "B"]));
|
||||
detach(&test, "order").await?;
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A failed close is terminal for the current owner; retrying Stop cannot replay an uncertain write.
|
||||
#[tokio::test]
|
||||
async fn failed_close_requires_history_recovery_before_another_call() -> Result<()> {
|
||||
use super::realtime_conversation::realtime_history_store::HostedClockStore;
|
||||
use codex_core::TurnInputRequest;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_thread_store::ThreadStore;
|
||||
use core_test_support::wait_for_event_match;
|
||||
|
||||
let api = start_mock_server().await;
|
||||
let store = Arc::new(HostedClockStore::failing_close());
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_thread_store(store.clone())
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let (_events, incoming) = mpsc::channel(32);
|
||||
let (outgoing, _commands) = mpsc::channel(32);
|
||||
let (reply, ready) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationAttachStream {
|
||||
params: params("failed-close", "ws://unused.invalid"),
|
||||
transport: RealtimeSidebandTransport { incoming, outgoing },
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
timeout(Duration::from_secs(10), ready).await???;
|
||||
|
||||
let (reply, result) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationEnd {
|
||||
realtime_session_id: "failed-close".to_owned(),
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
let error = timeout(Duration::from_secs(10), result)
|
||||
.await??
|
||||
.expect_err("an uncertain close must fail End");
|
||||
assert!(
|
||||
matches!(error.details(), CodexErrorDetails::Fatal(message) if message == "realtime history recovery required; reload the thread before continuing")
|
||||
);
|
||||
let closed = wait_for_event_match(&test.codex, |event| match event {
|
||||
EventMsg::RealtimeConversationClosed(closed) => Some(closed.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(closed.reason.as_deref(), Some("history_recovery_required"));
|
||||
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
let repeated = wait_for_event_match(&test.codex, |event| match event {
|
||||
EventMsg::RealtimeConversationClosed(closed) => Some(closed.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(repeated, closed);
|
||||
assert_eq!(store.close_attempts(), 1);
|
||||
let history = store
|
||||
.load_latest_model_context(codex_thread_store::LoadThreadHistoryParams {
|
||||
thread_id: test.session_configured.thread_id,
|
||||
include_archived: false,
|
||||
})
|
||||
.await?;
|
||||
assert!(!history.items.iter().any(|item| matches!(item,
|
||||
RolloutItem::RealtimeItem(item)
|
||||
if matches!(item.content, RealtimeItemContent::RealtimeSessionClosed { .. })
|
||||
)));
|
||||
|
||||
let (_, result) = start(&test, "next-call", "ws://unused.invalid").await?;
|
||||
let error = timeout(Duration::from_secs(10), result)
|
||||
.await??
|
||||
.expect_err("a new attachment must wait for recovery");
|
||||
assert!(matches!(error.details(), CodexErrorDetails::Fatal(_)));
|
||||
let error = test
|
||||
.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: "continue".to_owned(),
|
||||
text_elements: Vec::new(),
|
||||
}]))
|
||||
.await
|
||||
.expect_err("turn input must wait for recovery");
|
||||
assert!(matches!(error.details(), CodexErrorDetails::Fatal(_)));
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Connection {
|
||||
accept: oneshot::Sender<bool>,
|
||||
messages: mpsc::UnboundedReceiver<Value>,
|
||||
@@ -333,7 +1028,7 @@ fn params(session: &str, url: &str) -> ConversationStartParams {
|
||||
ConversationStartParams {
|
||||
client_managed_handoffs: false,
|
||||
delegation_ack_filler: None,
|
||||
flush_transcript_tail_on_session_end: false,
|
||||
flush_transcript_tail_on_session_end: true,
|
||||
codex_responses_as_items: false,
|
||||
codex_response_item_prefix: None,
|
||||
codex_response_handoff_mode: Default::default(),
|
||||
@@ -387,6 +1082,34 @@ async fn detach(test: &TestCodex, session: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn end(test: &TestCodex, session: &str) -> Result<()> {
|
||||
timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
let (reply, result) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationEnd {
|
||||
realtime_session_id: session.to_owned(),
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
match result.await? {
|
||||
Ok(()) => return anyhow::Ok(()),
|
||||
Err(error) if is_realtime_busy(&error) => {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
Err(error) => return Err(error.into()),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await??;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_realtime_busy(error: &codex_protocol::error::CodexErr) -> bool {
|
||||
matches!(error.details(), codex_protocol::error::CodexErrorDetails::Io(error)
|
||||
if error.kind() == std::io::ErrorKind::WouldBlock)
|
||||
}
|
||||
|
||||
async fn attached(
|
||||
test: &TestCodex,
|
||||
server: &mut Sideband,
|
||||
@@ -608,11 +1331,22 @@ pub(super) async fn attachment_replacement_scenario() -> Result<Vec<ResponsesReq
|
||||
})
|
||||
.with_config(|config| {
|
||||
config.base_instructions = Some(String::new());
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
});
|
||||
let test = builder.build_with_auto_env(&api).await?;
|
||||
let mut original = Sideband::new().await?;
|
||||
let mut newer = Sideband::new().await?;
|
||||
let (original_id, mut original_messages) = attached(&test, &mut original, "original").await?;
|
||||
let (_, busy) = start(&test, "newer", &newer.url).await?;
|
||||
assert!(is_realtime_busy(
|
||||
&timeout(Duration::from_secs(10), busy)
|
||||
.await??
|
||||
.expect_err("replacement settles first")
|
||||
));
|
||||
end(&test, "original").await?;
|
||||
let (newer_id, mut completion) = start(&test, "newer", &newer.url).await?;
|
||||
let connection = newer.connected().await;
|
||||
assert_eq!(
|
||||
@@ -670,10 +1404,22 @@ pub(super) async fn attachment_replacement_scenario() -> Result<Vec<ResponsesReq
|
||||
#[tokio::test]
|
||||
async fn failed_attachment_leaves_core_disconnected() -> Result<()> {
|
||||
let api = start_mock_server().await;
|
||||
let test = test_codex().build_with_auto_env(&api).await?;
|
||||
let test = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_config(|config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
})
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let mut original = Sideband::new().await?;
|
||||
let mut newer = Sideband::new().await?;
|
||||
let (_, mut current) = attached(&test, &mut original, "original").await?;
|
||||
let (_, busy) = start(&test, "newer", &newer.url).await?;
|
||||
assert!(timeout(Duration::from_secs(10), busy).await??.is_err());
|
||||
end(&test, "original").await?;
|
||||
let (_, result) = start(&test, "newer", &newer.url).await?;
|
||||
let connection = newer.connected().await;
|
||||
assert_eq!(
|
||||
@@ -688,10 +1434,14 @@ async fn failed_attachment_leaves_core_disconnected() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case::test_case(true; "record unfinished tail")]
|
||||
#[test_case::test_case(false; "drop unfinished tail")]
|
||||
#[test_case::test_case(ThreadHistoryMode::Legacy, true; "legacy record unfinished tail")]
|
||||
#[test_case::test_case(ThreadHistoryMode::Legacy, false; "legacy setting ignored for v3")]
|
||||
#[test_case::test_case(ThreadHistoryMode::Paginated, true; "frames record unfinished tail")]
|
||||
#[tokio::test]
|
||||
async fn completed_turns_reach_history_before_the_next_handoff(flush_tail: bool) -> Result<()> {
|
||||
async fn completed_turns_reach_history_before_the_next_handoff(
|
||||
history_mode: ThreadHistoryMode,
|
||||
flush_tail: bool,
|
||||
) -> Result<()> {
|
||||
use codex_protocol::protocol::RealtimeEvent;
|
||||
use core_test_support::responses;
|
||||
use core_test_support::wait_for_event;
|
||||
@@ -704,7 +1454,10 @@ async fn completed_turns_reach_history_before_the_next_handoff(flush_tail: bool)
|
||||
])
|
||||
});
|
||||
let model = responses::mount_sse_sequence(&api, replies.into()).await;
|
||||
let test = test_codex().build_with_auto_env(&api).await?;
|
||||
let test = test_codex()
|
||||
.with_history_mode(history_mode)
|
||||
.build_with_auto_env(&api)
|
||||
.await?;
|
||||
let mut sideband = Sideband::new().await?;
|
||||
let mut params = params("transcript", &sideband.url);
|
||||
params.flush_transcript_tail_on_session_end = flush_tail;
|
||||
@@ -763,15 +1516,13 @@ async fn completed_turns_reach_history_before_the_next_handoff(flush_tail: bool)
|
||||
let unfinished = "a".repeat(12 * 1024);
|
||||
send(json!({"type":"output_transcript.added", "item":{"text":unfinished}}))?;
|
||||
wait_for_event(&test.codex, |event| matches!(event, EventMsg::RealtimeConversationRealtime(event) if matches!(event.payload, RealtimeEvent::OutputTranscriptDelta(_)))).await;
|
||||
detach(&test, "transcript").await?;
|
||||
end(&test, "transcript").await?;
|
||||
test.submit_text_turn("continue").await?;
|
||||
let after_close = transcript(model.last_request().expect("text turn"));
|
||||
assert_eq!(after_close.len(), completed.len() + usize::from(flush_tail));
|
||||
assert_eq!(after_close.len(), completed.len() + 1);
|
||||
assert_eq!(&after_close[..completed.len()], completed.as_slice());
|
||||
if flush_tail {
|
||||
assert!(after_close[completed.len()].contains(&format!(
|
||||
"<transcript_delta>assistant: {unfinished}</transcript_delta>"
|
||||
)));
|
||||
}
|
||||
assert!(after_close[completed.len()].contains(&format!(
|
||||
"<transcript_delta>assistant: {unfinished}</transcript_delta>"
|
||||
)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ use codex_core::TurnInputRequest;
|
||||
use codex_core::TurnInputSubmission;
|
||||
use codex_core::test_support::auth_manager_from_auth;
|
||||
use codex_features::Feature;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::InitialHistory;
|
||||
use codex_history::RealtimeHandoffMetadata;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_login::CodexAuth;
|
||||
@@ -88,6 +90,9 @@ use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
use wiremock::matchers::path_regex;
|
||||
|
||||
#[path = "realtime_history_store.rs"]
|
||||
pub(super) mod realtime_history_store;
|
||||
|
||||
const STARTUP_CONTEXT_HEADER: &str = "Startup context from Codex.";
|
||||
const STARTUP_CONTEXT_OPEN_TAG: &str = "<startup_context>";
|
||||
const STARTUP_CONTEXT_CLOSE_TAG: &str = "</startup_context>";
|
||||
@@ -2318,8 +2323,148 @@ async fn conversation_start_uses_openai_env_key_fallback_with_chatgpt_auth() ->
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn two_existing_calls_on_one_thread_keep_distinct_openings_and_saved_tails() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let calls = [("rtc_first", "first tail"), ("rtc_second", "second tail")];
|
||||
let realtime_server = start_websocket_server_with_headers(calls.iter().map(|(id, tail)| {
|
||||
WebSocketConnectionConfig {
|
||||
requests: vec![vec![
|
||||
json!({"type":"session.updated", "session":{"id":id, "instructions":"fixture"}}),
|
||||
json!({"type":"input_transcript.added", "item":{"text":tail}}),
|
||||
]],
|
||||
response_headers: Vec::new(),
|
||||
accept_delay: None,
|
||||
close_after_requests: false,
|
||||
}
|
||||
}).collect()).await;
|
||||
let api_server = start_mock_server().await;
|
||||
let mut builder = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_config({
|
||||
let url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
config.experimental_realtime_ws_base_url = Some(url);
|
||||
config.realtime.version = RealtimeWsVersion::V3;
|
||||
}
|
||||
});
|
||||
let test = builder.build_with_auto_env(&api_server).await?;
|
||||
test.codex.ensure_rollout_materialized().await;
|
||||
for (call_id, _) in calls {
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationStart(ConversationStartParams {
|
||||
client_managed_handoffs: false,
|
||||
delegation_ack_filler: None,
|
||||
flush_transcript_tail_on_session_end: true,
|
||||
codex_responses_as_items: false,
|
||||
codex_response_item_prefix: None,
|
||||
codex_response_handoff_mode:
|
||||
codex_protocol::protocol::CodexResponseHandoffMode::Thinking,
|
||||
backend_reasoning_status: false,
|
||||
codex_response_handoff_channel_prefixes: None,
|
||||
model: None,
|
||||
output_modality: RealtimeOutputModality::Audio,
|
||||
include_startup_context: false,
|
||||
initial_items: Vec::new(),
|
||||
realtime_start_instructions: None,
|
||||
realtime_end_instructions: None,
|
||||
prompt: None,
|
||||
realtime_session_id: None,
|
||||
voice_session_id: None,
|
||||
transport: Some(ConversationStartTransport::ExistingCall {
|
||||
call_id: call_id.to_string(),
|
||||
sideband_base_url: None,
|
||||
}),
|
||||
version: Some(RealtimeConversationVersion::V3),
|
||||
voice: None,
|
||||
}))
|
||||
.await?;
|
||||
let started = wait_for_event_match(&test.codex, |msg| match msg {
|
||||
EventMsg::RealtimeConversationStarted(event) => Some(event.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(started.realtime_session_id.as_deref(), Some(call_id));
|
||||
// Existing-call attach sends no session.update. Trigger the fixture's transcript.
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationText(ConversationTextParams {
|
||||
text: "ready".into(),
|
||||
role: ConversationTextRole::User,
|
||||
}))
|
||||
.await?;
|
||||
wait_for_event_match(&test.codex, |msg| match msg {
|
||||
EventMsg::RealtimeConversationRealtime(RealtimeConversationRealtimeEvent {
|
||||
payload: RealtimeEvent::InputTranscriptDelta(_),
|
||||
}) => Some(()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
wait_for_event_match(&test.codex, |msg| match msg {
|
||||
EventMsg::RealtimeConversationClosed(_) => Some(()),
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
for (call_id, tail) in calls {
|
||||
let page = test
|
||||
.thread_store
|
||||
.list_realtime(codex_thread_store::ListRealtimeParams {
|
||||
thread_id: test.session_configured.thread_id,
|
||||
session_id: Some(call_id.into()),
|
||||
order: codex_app_server_protocol::ThreadRealtimeOrder::Position,
|
||||
page_size: 20,
|
||||
from_observed_at_ms: None,
|
||||
to_observed_at_ms: None,
|
||||
after_position: None,
|
||||
event_id: None,
|
||||
cursor: None,
|
||||
snapshot_end: None,
|
||||
required_through_position: None,
|
||||
})
|
||||
.await?;
|
||||
let openings = page
|
||||
.data
|
||||
.iter()
|
||||
.filter(|entry| {
|
||||
matches!(
|
||||
entry.item.content,
|
||||
ThreadRealtimeItemContent::RealtimeSessionStarted
|
||||
)
|
||||
})
|
||||
.count();
|
||||
assert_eq!(openings, 1);
|
||||
let tails: Vec<_> = page
|
||||
.data
|
||||
.iter()
|
||||
.filter_map(|entry| match &entry.item.content {
|
||||
ThreadRealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_app_server_protocol::ThreadRealtimeSessionInputReason::SessionEnd,
|
||||
input,
|
||||
..
|
||||
} => Some(input),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let [saved] = tails.as_slice() else {
|
||||
panic!("expected one saved tail for {call_id}, got {tails:?}");
|
||||
};
|
||||
assert!(saved.contains(&format!("user: {tail}")));
|
||||
}
|
||||
test.codex.submit(Op::Shutdown).await?;
|
||||
realtime_server.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn assert_transport_close_tail_flush(
|
||||
flush_transcript_tail_on_session_end: bool,
|
||||
history_mode: ThreadHistoryMode,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
@@ -2345,9 +2490,13 @@ async fn assert_transport_close_tail_flush(
|
||||
]]])
|
||||
.await;
|
||||
|
||||
let mut builder = test_codex().with_config({
|
||||
let mut builder = test_codex().with_history_mode(history_mode).with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
}
|
||||
@@ -2413,14 +2562,25 @@ async fn assert_transport_close_tail_flush(
|
||||
.await;
|
||||
assert_eq!(closed.reason.as_deref(), Some("transport_closed"));
|
||||
// Closed must be a history barrier, including when there is no active turn.
|
||||
let history = test.codex.load_history(/*include_archived*/ false).await?;
|
||||
let tails = history.items.iter().filter(|item| {
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
let ordinary_tails = history.iter().filter(|item| {
|
||||
matches!(item, RolloutItem::ResponseItem(ResponseItemEnvelope { item: ResponseItem::Message { role, content, .. }, .. })
|
||||
if role == "user" && content.iter().any(|item| matches!(item,
|
||||
ContentItem::InputText { text } if text.contains("<source>transcript_tail_flush</source>")
|
||||
&& text.contains("user: transport tail"))))
|
||||
}).count();
|
||||
assert_eq!(tails, usize::from(flush_transcript_tail_on_session_end));
|
||||
let saved_tails = history.iter().filter(|item| {
|
||||
matches!(item, RolloutItem::RealtimeItem(codex_protocol::realtime::RealtimeItem {
|
||||
content: codex_protocol::realtime::RealtimeItemContent::SessionInputSaved { reason: codex_protocol::realtime::SessionInputReason::SessionEnd, input, .. }, ..
|
||||
}) if input.contains("user: transport tail"))
|
||||
}).count();
|
||||
let expected = usize::from(flush_transcript_tail_on_session_end);
|
||||
// Protocol V1 retains the old opt-in transcript flush in either history mode.
|
||||
assert_eq!((ordinary_tails, saved_tails), (expected, 0));
|
||||
assert!(response_mock.requests().is_empty());
|
||||
|
||||
test.submit_text_turn("continue in text").await?;
|
||||
@@ -2438,8 +2598,18 @@ async fn assert_transport_close_tail_flush(
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn conversation_transport_close_tail_flush_is_opt_in() -> Result<()> {
|
||||
assert_transport_close_tail_flush(/*flush_transcript_tail_on_session_end*/ false).await?;
|
||||
assert_transport_close_tail_flush(/*flush_transcript_tail_on_session_end*/ true).await?;
|
||||
for history_mode in [ThreadHistoryMode::Legacy, ThreadHistoryMode::Paginated] {
|
||||
assert_transport_close_tail_flush(
|
||||
/*flush_transcript_tail_on_session_end*/ false,
|
||||
history_mode,
|
||||
)
|
||||
.await?;
|
||||
assert_transport_close_tail_flush(
|
||||
/*flush_transcript_tail_on_session_end*/ true,
|
||||
history_mode,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -4919,7 +5089,31 @@ fn message_input_texts(body: &Value, role: &str) -> Vec<String> {
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
realtime_handoff_history_scenario(/*block_prompt*/ false).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn realtime_handoff_hook_rejection_has_no_acceptance_receipt() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
core_test_support::skip_if_wine_exec!(
|
||||
Ok(()),
|
||||
"the prompt hook requires host-native Python and filesystem paths"
|
||||
);
|
||||
realtime_handoff_history_scenario(/*block_prompt*/ true).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn realtime_handoff_history_scenario(
|
||||
block_prompt: bool,
|
||||
) -> Result<Vec<responses::ResponsesRequest>> {
|
||||
realtime_handoff_history_scenario_inner(block_prompt).await
|
||||
}
|
||||
|
||||
async fn realtime_handoff_history_scenario_inner(
|
||||
block_prompt: bool,
|
||||
) -> Result<Vec<responses::ResponsesRequest>> {
|
||||
let api_server = start_mock_server().await;
|
||||
let response_mock = responses::mount_sse_once(
|
||||
&api_server,
|
||||
@@ -4931,22 +5125,27 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
)
|
||||
.await;
|
||||
|
||||
let realtime_server = start_websocket_server(vec![vec![vec![
|
||||
json!({
|
||||
"type": "session.updated",
|
||||
"session": { "id": "sess_inbound", "instructions": "backend prompt" }
|
||||
}),
|
||||
json!({
|
||||
"type": "conversation.input_transcript.delta",
|
||||
"delta": "text from realtime"
|
||||
}),
|
||||
json!({
|
||||
"type": "conversation.handoff.requested",
|
||||
"handoff_id": "handoff_inbound",
|
||||
"item_id": "item_inbound",
|
||||
"input_transcript": "text from realtime"
|
||||
}),
|
||||
]]])
|
||||
let realtime_server = start_websocket_server_with_headers(vec![WebSocketConnectionConfig {
|
||||
requests: vec![vec![
|
||||
json!({
|
||||
"type": "session.updated",
|
||||
"session": { "id": "sess_inbound", "instructions": "backend prompt" }
|
||||
}),
|
||||
if block_prompt {
|
||||
json!({"type":"input_transcript.added", "item":{"text":"text from realtime"}})
|
||||
} else {
|
||||
json!({"type":"conversation.input_transcript.delta", "delta":"text from realtime"})
|
||||
},
|
||||
if block_prompt {
|
||||
json!({"type":"delegation.created", "item":{"id":"handoff_inbound", "type":"delegation", "target":"client", "content":[{"type":"input_text", "text":"text from realtime"}]}})
|
||||
} else {
|
||||
json!({"type":"conversation.handoff.requested", "handoff_id":"handoff_inbound", "item_id":"item_inbound", "input_transcript":"text from realtime"})
|
||||
},
|
||||
]],
|
||||
response_headers: Vec::new(),
|
||||
accept_delay: None,
|
||||
close_after_requests: !block_prompt,
|
||||
}])
|
||||
.await;
|
||||
|
||||
let mut builder = test_codex()
|
||||
@@ -4954,17 +5153,46 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
.with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
if block_prompt {
|
||||
core_test_support::hooks::trust_discovered_hooks(config);
|
||||
}
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
config.realtime.version = if block_prompt {
|
||||
RealtimeWsVersion::V3
|
||||
} else {
|
||||
RealtimeWsVersion::V1
|
||||
};
|
||||
}
|
||||
});
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
builder = builder.with_pre_build_hook(move |home| {
|
||||
if block_prompt {
|
||||
super::hooks::write_user_prompt_submit_hook(home,
|
||||
"<realtime_delegation>\n <input>text from realtime</input>\n <transcript_delta>user: text from realtime</transcript_delta>\n</realtime_delegation>", "")
|
||||
.expect("write blocking realtime prompt hook");
|
||||
let script = home.join("user_prompt_submit_hook.py");
|
||||
let existing = fs::read_to_string(&script).expect("read prompt hook");
|
||||
let gate = format!(
|
||||
"import time\nfrom pathlib import Path\nPath(r\"{}\").touch()\nwhile not Path(r\"{}\").exists():\n time.sleep(0.01)\n",
|
||||
home.join("hook_started").display(),
|
||||
home.join("hook_release").display(),
|
||||
);
|
||||
fs::write(script, format!("{gate}{existing}")).expect("gate prompt hook");
|
||||
}
|
||||
});
|
||||
}
|
||||
let test = builder.build_with_auto_env(&api_server).await?;
|
||||
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationStart(ConversationStartParams {
|
||||
client_managed_handoffs: false,
|
||||
delegation_ack_filler: None,
|
||||
flush_transcript_tail_on_session_end: false,
|
||||
flush_transcript_tail_on_session_end: block_prompt,
|
||||
codex_responses_as_items: false,
|
||||
codex_response_item_prefix: None,
|
||||
codex_response_handoff_mode:
|
||||
@@ -4978,7 +5206,7 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
realtime_start_instructions: None,
|
||||
realtime_end_instructions: None,
|
||||
prompt: Some(Some("backend prompt".to_string())),
|
||||
realtime_session_id: None,
|
||||
realtime_session_id: Some("sess_inbound".to_owned()),
|
||||
voice_session_id: None,
|
||||
transport: None,
|
||||
version: None,
|
||||
@@ -5019,11 +5247,65 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
};
|
||||
Uuid::parse_str(&turn_id).context("realtime-routed turn ID should be a UUID")?;
|
||||
|
||||
if block_prompt {
|
||||
let started = test.home.path().join("hook_started");
|
||||
timeout(Duration::from_secs(10), async {
|
||||
while !started.exists() {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.context("prompt hook did not start")?;
|
||||
let (reply, mut stopped) = oneshot::channel();
|
||||
test.codex
|
||||
.submit(Op::RealtimeConversationEnd {
|
||||
realtime_session_id: "sess_inbound".to_owned(),
|
||||
reply,
|
||||
})
|
||||
.await?;
|
||||
// Terminal stop disconnects immediately but waits for the reserved handoff to settle.
|
||||
assert!(
|
||||
timeout(Duration::from_millis(100), &mut stopped)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
// An interrupt must still reach the submission dispatcher while stop is pending.
|
||||
test.codex.submit(Op::Interrupt).await?;
|
||||
timeout(Duration::from_secs(10), stopped).await???;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationClosed(_))
|
||||
})
|
||||
.await;
|
||||
assert!(response_mock.requests().is_empty());
|
||||
test.thread_store
|
||||
.flush_thread(test.session_configured.thread_id)
|
||||
.await?;
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
assert!(
|
||||
!history
|
||||
.iter()
|
||||
.filter_map(RolloutItem::realtime_item)
|
||||
.any(|item| matches!(
|
||||
item.content,
|
||||
codex_protocol::realtime::RealtimeItemContent::TurnInputAccepted { .. }
|
||||
)),
|
||||
"a hook-blocked prompt must not become recoverable accepted input"
|
||||
);
|
||||
assert!(history.iter().any(|item| matches!(item,
|
||||
RolloutItem::RealtimeItem(codex_protocol::realtime::RealtimeItem {
|
||||
content: codex_protocol::realtime::RealtimeItemContent::SessionInputSaved { reason: codex_protocol::realtime::SessionInputReason::SessionEnd, input, .. }, ..
|
||||
}) if input.contains("user: text from realtime")
|
||||
)), "rejected handoff speech remains available for closure carryover");
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
test.thread_store
|
||||
.flush_thread(test.session_configured.thread_id)
|
||||
.await?;
|
||||
@@ -5060,7 +5342,46 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
}]
|
||||
);
|
||||
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
let receipt = history
|
||||
.iter()
|
||||
.filter_map(RolloutItem::realtime_item)
|
||||
.map(std::borrow::Cow::into_owned)
|
||||
.find_map(|item| match item.content {
|
||||
codex_protocol::realtime::RealtimeItemContent::TurnInputAccepted {
|
||||
source_event_ids,
|
||||
input,
|
||||
input_item_id,
|
||||
..
|
||||
} => Some((source_event_ids, input, input_item_id)),
|
||||
_ => None,
|
||||
})
|
||||
.expect("accepted handoff is durable");
|
||||
assert_eq!(receipt.0.len(), 1);
|
||||
assert!(history.iter().any(|item| matches!(item,
|
||||
RolloutItem::RealtimeItem(item) if item.id == receipt.0[0]
|
||||
&& matches!(&item.content, codex_protocol::realtime::RealtimeItemContent::Frame { channels } if channels.voice_transcript.text == "text from realtime")
|
||||
)));
|
||||
assert!(history.iter().any(|item| matches!(item,
|
||||
RolloutItem::ResponseItem(item) if item.id().is_some_and(|id| id.as_str() == receipt.2)
|
||||
&& item.metadata.as_ref().is_some_and(|metadata| metadata.realtime_handoff.is_some())
|
||||
)));
|
||||
assert!(
|
||||
!history.iter().any(|item| matches!(
|
||||
item,
|
||||
RolloutItem::RealtimeItem(codex_protocol::realtime::RealtimeItem {
|
||||
content: codex_protocol::realtime::RealtimeItemContent::TurnInputAccepted { .. },
|
||||
..
|
||||
})
|
||||
)),
|
||||
"accepted input must not be persisted again as a standalone receipt"
|
||||
);
|
||||
let request = response_mock.single_request();
|
||||
assert!(request.message_input_texts("user").contains(&receipt.1));
|
||||
let turn_metadata: Value = serde_json::from_str(
|
||||
request
|
||||
.header("x-codex-turn-metadata")
|
||||
@@ -5073,6 +5394,189 @@ async fn inbound_handoff_request_starts_turn_and_promotes_its_artifact() -> Resu
|
||||
== "<realtime_delegation>\n <input>text from realtime</input>\n <transcript_delta>user: text from realtime</transcript_delta>\n</realtime_delegation>"));
|
||||
|
||||
realtime_server.shutdown().await;
|
||||
Ok(response_mock.requests())
|
||||
}
|
||||
|
||||
/// Simulate a stop after ordinary admission persisted the canonical input but
|
||||
/// before sampling. Loading the thread alone does not run inference.
|
||||
#[test_case(false; "local_rollout")]
|
||||
#[test_case(true; "hosted_clock")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn explicit_recovery_dispatches_the_committed_handoff_on_its_receiving_turn(
|
||||
hosted_clock: bool,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let server = responses::start_mock_server().await;
|
||||
let model = responses::mount_sse_once(&server, responses::sse_completed("recovered")).await;
|
||||
let mut builder = test_codex().with_history_mode(ThreadHistoryMode::Paginated);
|
||||
let hosted_store = Arc::new(realtime_history_store::HostedClockStore::default());
|
||||
let voice_server = if hosted_clock {
|
||||
let voice = start_websocket_server(vec![vec![vec![]], vec![vec![]]]).await;
|
||||
let url = voice.uri().to_string();
|
||||
builder = builder
|
||||
.with_thread_store(hosted_store.clone())
|
||||
.with_config(move |config| {
|
||||
config.realtime.version = RealtimeWsVersion::V2;
|
||||
config.experimental_realtime_ws_base_url = Some(url);
|
||||
config.experimental_realtime_ws_startup_context = Some(String::new());
|
||||
});
|
||||
Some(voice)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let test = builder.build_with_auto_env(&server).await?;
|
||||
let thread_id = test.session_configured.thread_id;
|
||||
let turn_id = "receiving-turn";
|
||||
let accepted = "<realtime_delegation>\n <input>recover this</input>\n</realtime_delegation>";
|
||||
let receipt_id = uuid::Uuid::now_v7();
|
||||
let mut input: ResponseItem = serde_json::from_value(json!({
|
||||
"type": "message",
|
||||
"id": codex_protocol::ResponseItemId::for_admitted_user_input(receipt_id),
|
||||
"role": "user",
|
||||
"content": [{"type": "input_text", "text": accepted}],
|
||||
}))?;
|
||||
input.set_turn_id_if_missing(turn_id);
|
||||
test.thread_store
|
||||
.append_items(codex_thread_store::AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(
|
||||
codex_protocol::protocol::TurnStartedEvent {
|
||||
turn_attribution: None,
|
||||
turn_id: turn_id.to_string(),
|
||||
root_turn_id: Some(turn_id.to_string()),
|
||||
trace_id: None,
|
||||
started_at: None,
|
||||
model_context_window: None,
|
||||
collaboration_mode_kind: Default::default(),
|
||||
},
|
||||
)),
|
||||
RolloutItem::ResponseItem(ResponseItemEnvelope {
|
||||
item: input,
|
||||
metadata: Some(CodexHarnessMetadata {
|
||||
realtime_handoff: Some(Box::new(RealtimeHandoffMetadata {
|
||||
event_id: receipt_id.to_string(),
|
||||
realtime_session_id: "voice".to_string(),
|
||||
observed_at_ms: 1_000,
|
||||
handoff_id: "handoff".to_string(),
|
||||
mode: codex_protocol::realtime::RealtimeHandoffMode::Start,
|
||||
source_event_ids: Vec::new(),
|
||||
})),
|
||||
..Default::default()
|
||||
}),
|
||||
}),
|
||||
],
|
||||
})
|
||||
.await?;
|
||||
test.thread_store.flush_thread(thread_id).await?;
|
||||
let path = test.codex.rollout_path();
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
test.thread_manager.remove_thread(&thread_id).await;
|
||||
let context = test
|
||||
.thread_store
|
||||
.load_latest_model_context(codex_thread_store::LoadThreadHistoryParams {
|
||||
thread_id,
|
||||
include_archived: false,
|
||||
})
|
||||
.await?;
|
||||
let resumed = test
|
||||
.thread_manager
|
||||
.resume_thread_with_history(
|
||||
test.config.clone(),
|
||||
InitialHistory::Resumed(codex_history::ResumedHistory {
|
||||
conversation_id: thread_id,
|
||||
history: Arc::new(context.items),
|
||||
history_revision: context.revision,
|
||||
rollout_path: path,
|
||||
}),
|
||||
auth_manager_from_auth(CodexAuth::from_api_key("dummy")),
|
||||
/*parent_trace*/ None,
|
||||
ClientMcpExtensions::default(),
|
||||
)
|
||||
.await?
|
||||
.thread;
|
||||
assert!(model.requests().is_empty());
|
||||
|
||||
let recovery = resumed
|
||||
.recover_turn_if_idle(codex_core::RecoverTurnRequest {
|
||||
turn_id: turn_id.into(),
|
||||
thread_settings: Default::default(),
|
||||
trace: None,
|
||||
cyber_access_program: None,
|
||||
prompt_cache_ttl: None,
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(
|
||||
recovery,
|
||||
codex_core::StartIfIdleSubmission::Started {
|
||||
turn_id: turn_id.into(),
|
||||
root_turn_id: turn_id.into(),
|
||||
}
|
||||
);
|
||||
wait_for_event(
|
||||
&resumed,
|
||||
|event| matches!(event, EventMsg::TurnComplete(event) if event.turn_id == turn_id),
|
||||
)
|
||||
.await;
|
||||
let messages = model.single_request().message_input_texts("user");
|
||||
assert_eq!(
|
||||
messages
|
||||
.iter()
|
||||
.filter(|text| text.as_str() == accepted)
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
if voice_server.is_some() {
|
||||
assert_eq!(hosted_store.reads(), (0, 0));
|
||||
for call_id in ["first-call", "second-call"] {
|
||||
resumed
|
||||
.submit(Op::RealtimeConversationStart(ConversationStartParams {
|
||||
client_managed_handoffs: false,
|
||||
delegation_ack_filler: None,
|
||||
flush_transcript_tail_on_session_end: false,
|
||||
codex_responses_as_items: false,
|
||||
codex_response_item_prefix: None,
|
||||
codex_response_handoff_mode:
|
||||
codex_protocol::protocol::CodexResponseHandoffMode::Thinking,
|
||||
backend_reasoning_status: false,
|
||||
codex_response_handoff_channel_prefixes: None,
|
||||
model: None,
|
||||
output_modality: RealtimeOutputModality::Audio,
|
||||
include_startup_context: true,
|
||||
initial_items: Vec::new(),
|
||||
realtime_start_instructions: None,
|
||||
realtime_end_instructions: None,
|
||||
prompt: None,
|
||||
realtime_session_id: Some(call_id.to_string()),
|
||||
voice_session_id: None,
|
||||
transport: None,
|
||||
version: None,
|
||||
voice: None,
|
||||
}))
|
||||
.await?;
|
||||
let observed_at_ms = wait_for_event_match(&resumed, |msg| match msg {
|
||||
EventMsg::RealtimeConversationRealtime(RealtimeConversationRealtimeEvent {
|
||||
payload: RealtimeEvent::HistoryItemCompleted(item),
|
||||
}) if item.realtime_session_id == call_id
|
||||
&& matches!(
|
||||
item.content,
|
||||
codex_protocol::realtime::RealtimeItemContent::RealtimeSessionStarted
|
||||
) =>
|
||||
{
|
||||
Some(item.observed_at_ms)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.await;
|
||||
assert_eq!(observed_at_ms, Some(realtime_history_store::FRAME_FLOOR_MS));
|
||||
resumed.submit(Op::RealtimeConversationClose).await?;
|
||||
wait_for_event(&resumed, |msg| {
|
||||
matches!(msg, EventMsg::RealtimeConversationClosed(_))
|
||||
})
|
||||
.await;
|
||||
}
|
||||
assert_eq!(hosted_store.reads(), (1, 1));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -5373,13 +5877,19 @@ async fn conversation_close_routes_only_remaining_transcript_tail_once() -> Resu
|
||||
close_after_requests: false,
|
||||
}])
|
||||
.await;
|
||||
let mut builder = test_codex().with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
}
|
||||
});
|
||||
let mut builder = test_codex()
|
||||
.with_history_mode(ThreadHistoryMode::Paginated)
|
||||
.with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
}
|
||||
});
|
||||
let test = builder.build(&api_server).await?;
|
||||
|
||||
test.codex
|
||||
@@ -5424,21 +5934,23 @@ async fn conversation_close_routes_only_remaining_transcript_tail_once() -> Resu
|
||||
.await;
|
||||
assert_eq!(closed.reason.as_deref(), Some("requested"));
|
||||
|
||||
let history = test.codex.load_history(/*include_archived*/ false).await?;
|
||||
let tails = history.items.iter().filter(|item| {
|
||||
matches!(item, RolloutItem::ResponseItem(ResponseItemEnvelope { item: ResponseItem::Message { role, content, .. }, .. })
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
let tails = history.iter().filter(|item| matches!(item,
|
||||
RolloutItem::ResponseItem(ResponseItemEnvelope { item: ResponseItem::Message { role, content, .. }, .. })
|
||||
if role == "user" && content.iter().any(|item| matches!(item,
|
||||
ContentItem::InputText { text } if text.contains("<source>transcript_tail_flush</source>")
|
||||
&& text.contains("assistant: remaining answer\nuser: remaining question"))))
|
||||
}).count();
|
||||
assert_eq!(tails, 1);
|
||||
ContentItem::InputText { text } if text.contains("<source>transcript_tail_flush</source>") && text.contains("assistant: remaining answer\nuser: remaining question")))
|
||||
)).count();
|
||||
assert_eq!(
|
||||
tails, 1,
|
||||
"protocol V1 saves the tail through legacy history"
|
||||
);
|
||||
assert_eq!(response_mock.requests().len(), 1);
|
||||
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::RealtimeConversationClosed(_))
|
||||
})
|
||||
.await;
|
||||
test.submit_text_turn("continue in text").await?;
|
||||
|
||||
let requests = response_mock.requests();
|
||||
@@ -5868,13 +6380,16 @@ async fn inbound_handoff_request_does_not_block_realtime_event_forwarding() -> R
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case(false, false; "without_tail")]
|
||||
#[test_case(true, false; "with_tail")]
|
||||
#[test_case(true, true; "interrupt_after_close")]
|
||||
#[test_case(false, false, ThreadHistoryMode::Legacy; "without_tail")]
|
||||
#[test_case(true, false, ThreadHistoryMode::Legacy; "with_tail")]
|
||||
#[test_case(true, true, ThreadHistoryMode::Legacy; "interrupt_after_close")]
|
||||
#[test_case(false, false, ThreadHistoryMode::Paginated; "v1_steer")]
|
||||
#[test_case(true, false, ThreadHistoryMode::Paginated; "v1_active_tail")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn inbound_handoff_request_updates_realtime_state_during_active_turn(
|
||||
flush_transcript_tail_on_session_end: bool,
|
||||
interrupt: bool,
|
||||
history_mode: ThreadHistoryMode,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
@@ -5952,13 +6467,20 @@ async fn inbound_handoff_request_updates_realtime_state_during_active_turn(
|
||||
}])
|
||||
.await;
|
||||
|
||||
let mut builder = test_codex().with_model("gpt-5.4").with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
}
|
||||
});
|
||||
let mut builder = test_codex()
|
||||
.with_history_mode(history_mode)
|
||||
.with_model("gpt-5.4")
|
||||
.with_config({
|
||||
let realtime_base_url = realtime_server.uri().to_string();
|
||||
move |config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::RealtimeFramesLocal)
|
||||
.expect("enable local realtime frames");
|
||||
config.experimental_realtime_ws_base_url = Some(realtime_base_url);
|
||||
config.realtime.version = RealtimeWsVersion::V1;
|
||||
}
|
||||
});
|
||||
let test = builder.build_with_streaming_server(&api_server).await?;
|
||||
|
||||
let started = test
|
||||
@@ -6050,6 +6572,21 @@ async fn inbound_handoff_request_updates_realtime_state_during_active_turn(
|
||||
.await
|
||||
.context("steered request did not start")?;
|
||||
|
||||
if history_mode == ThreadHistoryMode::Paginated {
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
assert!(history.iter().filter_map(RolloutItem::realtime_item).any(|item| matches!(
|
||||
&item.content,
|
||||
codex_protocol::realtime::RealtimeItemContent::TurnInputAccepted {
|
||||
turn_id: receiving_turn, mode: codex_protocol::realtime::RealtimeHandoffMode::Steer,
|
||||
source_event_ids, input, ..
|
||||
} if receiving_turn == &turn_id && source_event_ids.len() == 1 && input.contains("steer via realtime")
|
||||
)), "the steering receipt must be durable before the second model request");
|
||||
}
|
||||
|
||||
// Closed and the persisted tail must not wait for the in-flight model response.
|
||||
test.codex.submit(Op::RealtimeConversationClose).await?;
|
||||
let closed = wait_for_event_match(&test.codex, |event| match event {
|
||||
@@ -6059,13 +6596,26 @@ async fn inbound_handoff_request_updates_realtime_state_during_active_turn(
|
||||
.await;
|
||||
assert_eq!(closed.reason.as_deref(), Some("requested"));
|
||||
if flush_transcript_tail_on_session_end {
|
||||
let history = test.codex.load_history(/*include_archived*/ false).await?;
|
||||
assert!(history.items.iter().any(|item| {
|
||||
matches!(item, RolloutItem::ResponseItem(ResponseItemEnvelope {
|
||||
let (history, _, parse_errors) = codex_rollout::RolloutRecorder::load_rollout_items(
|
||||
&test.codex.rollout_path().expect("local rollout path"),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(parse_errors, 0);
|
||||
let ordinary_tails = history
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
matches!(item, RolloutItem::ResponseItem(ResponseItemEnvelope {
|
||||
item: ResponseItem::Message { role, content, .. }, ..
|
||||
}) if role == "user" && content.iter().any(|item| matches!(item,
|
||||
ContentItem::InputText { text } if text.contains("user: final transcript tail"))))
|
||||
}));
|
||||
})
|
||||
.count();
|
||||
let saved_tails = history.iter().filter(|item| {
|
||||
matches!(item, RolloutItem::RealtimeItem(codex_protocol::realtime::RealtimeItem {
|
||||
content: codex_protocol::realtime::RealtimeItemContent::SessionInputSaved { reason: codex_protocol::realtime::SessionInputReason::SessionEnd, input, .. }, ..
|
||||
}) if input.contains("user: final transcript tail"))
|
||||
}).count();
|
||||
assert_eq!((ordinary_tails, saved_tails), (1, 0));
|
||||
}
|
||||
if interrupt {
|
||||
test.codex.submit(Op::Interrupt).await?;
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
//! Hosted realtime test store with a durable clock floor and optional seal failures.
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_history::RolloutItem;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::realtime::RealtimeItem;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_thread_store::AppendThreadItemsParams;
|
||||
use codex_thread_store::ArchiveThreadParams;
|
||||
use codex_thread_store::CreateThreadParams;
|
||||
use codex_thread_store::DeleteThreadParams;
|
||||
use codex_thread_store::InMemoryThreadStore;
|
||||
use codex_thread_store::ListThreadsParams;
|
||||
use codex_thread_store::LoadThreadHistoryParams;
|
||||
use codex_thread_store::PersistContext;
|
||||
use codex_thread_store::ReadThreadByRolloutPathParams;
|
||||
use codex_thread_store::ReadThreadParams;
|
||||
use codex_thread_store::ResumeThreadParams;
|
||||
use codex_thread_store::StoredModelContext;
|
||||
use codex_thread_store::StoredThread;
|
||||
use codex_thread_store::StoredThreadHistory;
|
||||
use codex_thread_store::ThreadPage;
|
||||
use codex_thread_store::ThreadStore;
|
||||
use codex_thread_store::ThreadStoreError;
|
||||
use codex_thread_store::ThreadStoreFuture;
|
||||
use codex_thread_store::UpdateThreadMetadataParams;
|
||||
use std::any::Any;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::Notify;
|
||||
|
||||
pub(super) const FRAME_FLOOR_MS: i64 = 4_102_444_800_000;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(in crate::suite) struct HostedClockStore {
|
||||
inner: InMemoryThreadStore,
|
||||
enrollment_reads: AtomicUsize,
|
||||
floor_reads: AtomicUsize,
|
||||
fail_close: bool,
|
||||
close_attempts: AtomicUsize,
|
||||
fail_after_input_commit: AtomicBool,
|
||||
input_attempts: AtomicUsize,
|
||||
sealed_inputs: Mutex<HashSet<String>>,
|
||||
block_first_frame: AtomicBool,
|
||||
frame_started: Notify,
|
||||
frame_release: Notify,
|
||||
}
|
||||
|
||||
impl HostedClockStore {
|
||||
pub(in crate::suite) fn blocking_frame() -> Self {
|
||||
Self {
|
||||
block_first_frame: AtomicBool::new(true),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::suite) async fn wait_for_blocked_frame(&self) {
|
||||
self.frame_started.notified().await;
|
||||
}
|
||||
|
||||
pub(in crate::suite) fn release_frame(&self) {
|
||||
self.frame_release.notify_one();
|
||||
}
|
||||
|
||||
pub(in crate::suite) fn failing_close() -> Self {
|
||||
Self {
|
||||
fail_close: true,
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::suite) fn close_attempts(&self) -> usize {
|
||||
self.close_attempts.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub(in crate::suite) fn failing_after_input_commit() -> Self {
|
||||
Self {
|
||||
fail_after_input_commit: AtomicBool::new(true),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::suite) fn input_attempts(&self) -> usize {
|
||||
self.input_attempts.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub(super) fn reads(&self) -> (usize, usize) {
|
||||
(
|
||||
self.enrollment_reads.load(Ordering::Relaxed),
|
||||
self.floor_reads.load(Ordering::Relaxed),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! delegate_store_methods {
|
||||
($(fn $name:ident($param:ident: $params:ty) -> $result:ty;)*) => {
|
||||
$(fn $name(&self, $param: $params) -> ThreadStoreFuture<'_, $result> {
|
||||
self.inner.$name($param)
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl ThreadStore for HostedClockStore {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn realtime_frames_enabled(&self, _: ThreadId) -> ThreadStoreFuture<'_, bool> {
|
||||
self.enrollment_reads.fetch_add(1, Ordering::Relaxed);
|
||||
Box::pin(async { Ok(true) })
|
||||
}
|
||||
|
||||
fn realtime_frame_clock_floor(&self, _: ThreadId) -> ThreadStoreFuture<'_, Option<i64>> {
|
||||
self.floor_reads.fetch_add(1, Ordering::Relaxed);
|
||||
Box::pin(async { Ok(Some(FRAME_FLOOR_MS)) })
|
||||
}
|
||||
|
||||
fn seal_realtime_items(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
items: Vec<RealtimeItem>,
|
||||
) -> ThreadStoreFuture<'_, ()> {
|
||||
Box::pin(async move {
|
||||
self.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: items.into_iter().map(RolloutItem::RealtimeItem).collect(),
|
||||
})
|
||||
.await
|
||||
})
|
||||
}
|
||||
|
||||
fn append_items(&self, params: AppendThreadItemsParams) -> ThreadStoreFuture<'_, ()> {
|
||||
if params.items.iter().any(|item| {
|
||||
matches!(item, RolloutItem::RealtimeItem(item) if matches!(
|
||||
item.content, RealtimeItemContent::RealtimeSessionClosed { .. }
|
||||
))
|
||||
}) {
|
||||
self.close_attempts.fetch_add(1, Ordering::Relaxed);
|
||||
if self.fail_close {
|
||||
return Box::pin(async {
|
||||
Err(ThreadStoreError::OutcomeUnknown {
|
||||
message: "injected close failure".to_owned(),
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
Box::pin(async move {
|
||||
if params.items.iter().any(|item| {
|
||||
matches!(item, RolloutItem::RealtimeItem(item) if matches!(
|
||||
item.content, RealtimeItemContent::Frame { .. }
|
||||
))
|
||||
}) && self.block_first_frame.swap(false, Ordering::Relaxed)
|
||||
{
|
||||
self.frame_started.notify_one();
|
||||
self.frame_release.notified().await;
|
||||
}
|
||||
self.inner.append_items(params).await
|
||||
})
|
||||
}
|
||||
|
||||
fn load_realtime_session(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
session_id: String,
|
||||
) -> ThreadStoreFuture<'_, Option<Vec<RealtimeItem>>> {
|
||||
Box::pin(async move {
|
||||
let context = self
|
||||
.inner
|
||||
.load_latest_model_context(LoadThreadHistoryParams {
|
||||
thread_id,
|
||||
include_archived: true,
|
||||
})
|
||||
.await?;
|
||||
let items: Vec<_> = context
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| item.realtime_item().map(std::borrow::Cow::into_owned))
|
||||
.filter(|item| item.realtime_session_id == session_id)
|
||||
.collect();
|
||||
Ok(items
|
||||
.iter()
|
||||
.any(|item| matches!(item.content, RealtimeItemContent::RealtimeSessionStarted))
|
||||
.then_some(items))
|
||||
})
|
||||
}
|
||||
|
||||
fn load_realtime_session_head(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
) -> ThreadStoreFuture<'_, Option<String>> {
|
||||
Box::pin(async move {
|
||||
let context = self
|
||||
.inner
|
||||
.load_latest_model_context(LoadThreadHistoryParams {
|
||||
thread_id,
|
||||
include_archived: true,
|
||||
})
|
||||
.await?;
|
||||
Ok(context
|
||||
.items
|
||||
.iter()
|
||||
.rev()
|
||||
.filter_map(RolloutItem::realtime_item)
|
||||
.find_map(|item| {
|
||||
matches!(item.content, RealtimeItemContent::RealtimeSessionStarted)
|
||||
.then(|| item.realtime_session_id.clone())
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
fn persist_thread(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
context: PersistContext,
|
||||
) -> ThreadStoreFuture<'_, ()> {
|
||||
self.inner.persist_thread(thread_id, context)
|
||||
}
|
||||
|
||||
fn seal_realtime_input(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
frames: Vec<RealtimeItem>,
|
||||
input: ResponseItemEnvelope,
|
||||
) -> ThreadStoreFuture<'_, ()> {
|
||||
self.input_attempts.fetch_add(1, Ordering::Relaxed);
|
||||
Box::pin(async move {
|
||||
let id = input.item.id().expect("accepted input ID").to_string();
|
||||
// This fixture retries seals serially; do not hold its bookkeeping lock across storage I/O.
|
||||
if self.sealed_inputs.lock().await.contains(&id) {
|
||||
return Ok(());
|
||||
}
|
||||
let mut items = frames
|
||||
.into_iter()
|
||||
.map(RolloutItem::RealtimeItem)
|
||||
.collect::<Vec<_>>();
|
||||
items.push(RolloutItem::ResponseItem(input));
|
||||
self.inner
|
||||
.append_items(AppendThreadItemsParams { thread_id, items })
|
||||
.await?;
|
||||
self.sealed_inputs.lock().await.insert(id);
|
||||
if self.fail_after_input_commit.swap(false, Ordering::Relaxed) {
|
||||
return Err(ThreadStoreError::OutcomeUnknown {
|
||||
message: "injected error after input commit".to_owned(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
delegate_store_methods! {
|
||||
fn read_thread(params: ReadThreadParams) -> StoredThread;
|
||||
fn resume_thread(params: ResumeThreadParams) -> Arc<Vec<RolloutItem>>;
|
||||
fn create_thread(params: CreateThreadParams) -> ();
|
||||
fn flush_thread(thread_id: ThreadId) -> ();
|
||||
fn shutdown_thread(thread_id: ThreadId) -> ();
|
||||
fn discard_thread(thread_id: ThreadId) -> ();
|
||||
fn load_history(params: LoadThreadHistoryParams) -> StoredThreadHistory;
|
||||
fn load_latest_model_context(params: LoadThreadHistoryParams) -> StoredModelContext;
|
||||
fn read_thread_by_rollout_path(params: ReadThreadByRolloutPathParams) -> StoredThread;
|
||||
fn list_threads(params: ListThreadsParams) -> ThreadPage;
|
||||
fn update_thread_metadata(params: UpdateThreadMetadataParams) -> Option<StoredThread>;
|
||||
fn archive_thread(params: ArchiveThreadParams) -> ();
|
||||
fn unarchive_thread(params: ArchiveThreadParams) -> StoredThread;
|
||||
fn delete_thread(params: DeleteThreadParams) -> ();
|
||||
}
|
||||
}
|
||||
@@ -4971,6 +4971,24 @@ async fn unshared_apps_catalog_across_turns() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn realtime_handoff_preserves_accepted_context() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let requests = super::realtime_conversation::realtime_handoff_history_scenario(
|
||||
/*block_prompt*/ false,
|
||||
)
|
||||
.await?;
|
||||
insta::assert_snapshot!(
|
||||
"realtime_turn_input_accepted_context",
|
||||
context_snapshot::format_request_history_snapshot(
|
||||
"A realtime handoff consumes the exact sealed transcript referenced by its durable acceptance.",
|
||||
&requests,
|
||||
&ContextSnapshotOptions::default().rewrite_known_segments(),
|
||||
)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn catalog_plugin_guidance_follows_model_and_survives_resume() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
---
|
||||
source: core/tests/suite/scenarios.rs
|
||||
assertion_line: 4337
|
||||
expression: "context_snapshot::format_request_history_snapshot(\"A realtime handoff consumes the exact sealed transcript referenced by its durable acceptance.\",\n&requests, &ContextSnapshotOptions::default().rewrite_known_segments(),)"
|
||||
---
|
||||
Scenario: A realtime handoff consumes the exact sealed transcript referenced by its durable acceptance.
|
||||
|
||||
## Window 1
|
||||
-- request 1 (turn) --
|
||||
00:message/developer:
|
||||
<MODEL_INSTRUCTIONS>
|
||||
01:message/developer[3]:
|
||||
[01] <realtime_conversation>
|
||||
Realtime conversation started.
|
||||
|
||||
You are operating as a backend executor behind an intermediary. The user does not talk to you directly. ...ediary and may be summarized before the user sees it. [hash=5114d16b52865327]
|
||||
|
||||
When invoked, you receive the latest conversation transcript and any relevant mode or metadata. The inte...void verbose responses that add user-visible latency. [hash=e229c6c9120dff28]
|
||||
|
||||
When user text is routed from realtime, treat it as a transcript. It may be unpunctuated or contain recognition errors.
|
||||
|
||||
- Keep responses concise and action-oriented. Your updates should help the intermediary respond to the user.
|
||||
</realtime_conversation>
|
||||
[02] <PERMISSIONS_INSTRUCTIONS>
|
||||
[03] <tools>
|
||||
Deferred tool namespaces:
|
||||
- multi_agent_v1: Tools for spawning and managing sub-agents.
|
||||
</tools>
|
||||
02:message/user:
|
||||
<ENVIRONMENT_CONTEXT>
|
||||
03:message/user:
|
||||
<realtime_delegation>
|
||||
<input>text from realtime</input>
|
||||
<transcript_delta>user: text from realtime</transcript_delta>
|
||||
</realtime_delegation>
|
||||
@@ -91,6 +91,9 @@ pub struct CodexHarnessMetadata {
|
||||
/// Provenance of a transcript chunk saved in ordinary model history.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub realtime_session_input: Option<Box<RealtimeSessionInputMetadata>>,
|
||||
/// End-of-call transcript context, recorded without creating a user turn.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub realtime_session_end_input: bool,
|
||||
|
||||
/// Thread-local append sequence. Compaction keeps the identities of retained inputs.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
|
||||
@@ -789,7 +789,13 @@ pub enum Op {
|
||||
/// Close the running realtime conversation stream.
|
||||
RealtimeConversationClose,
|
||||
|
||||
/// Close only the matching session after earlier start operations finish.
|
||||
/// End the identified logical call, including after its transport owner is lost.
|
||||
RealtimeConversationEnd {
|
||||
realtime_session_id: String,
|
||||
reply: oneshot::Sender<CodexResult<()>>,
|
||||
},
|
||||
|
||||
/// Disconnect only the matching transport after earlier start operations finish.
|
||||
RealtimeConversationDetach {
|
||||
realtime_session_id: String,
|
||||
reply: oneshot::Sender<CodexResult<()>>,
|
||||
@@ -1151,6 +1157,7 @@ impl Op {
|
||||
Self::RealtimeConversationText(_) => "realtime_conversation_text",
|
||||
Self::RealtimeConversationSpeech(_) => "realtime_conversation_speech",
|
||||
Self::RealtimeConversationClose => "realtime_conversation_close",
|
||||
Self::RealtimeConversationEnd { .. } => "realtime_conversation_end",
|
||||
Self::RealtimeConversationDetach { .. } => "realtime_conversation_detach",
|
||||
Self::RealtimeConversationListVoices => "realtime_conversation_list_voices",
|
||||
Self::RecoverTurn { .. } => "recover_turn",
|
||||
|
||||
@@ -286,7 +286,7 @@ fn rollout_item_type(item: &RolloutItem) -> String {
|
||||
"realtime.turn_input_accepted".to_string()
|
||||
}
|
||||
codex_protocol::realtime::RealtimeItemContent::SessionInputSaved { .. } => {
|
||||
"realtime.session_end_input_saved".to_string()
|
||||
"realtime.session_input_saved".to_string()
|
||||
}
|
||||
},
|
||||
RolloutItem::EventMsg(EventMsg::ItemCompleted(event)) => {
|
||||
|
||||
@@ -300,6 +300,25 @@ impl LiveThread {
|
||||
.seal_realtime_items(self.thread_id, items)
|
||||
.await;
|
||||
}
|
||||
if items.last().is_some_and(|item| {
|
||||
matches!(
|
||||
item.content,
|
||||
codex_protocol::realtime::RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
..
|
||||
}
|
||||
)
|
||||
}) {
|
||||
self.thread_store
|
||||
.seal_realtime_items(self.thread_id, items.clone())
|
||||
.await?;
|
||||
let records = items
|
||||
.into_iter()
|
||||
.map(RolloutItem::RealtimeItem)
|
||||
.collect::<Vec<_>>();
|
||||
self.observe_appended_metadata(&records).await?;
|
||||
return Ok(());
|
||||
}
|
||||
let records: Vec<_> = items.into_iter().map(RolloutItem::RealtimeItem).collect();
|
||||
self.append_items(&records).await?;
|
||||
self.flush().await
|
||||
|
||||
@@ -199,6 +199,39 @@ pub(super) async fn append_items(
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn seal_realtime_items(
|
||||
store: &LocalThreadStore,
|
||||
thread_id: ThreadId,
|
||||
items: Vec<codex_protocol::realtime::RealtimeItem>,
|
||||
) -> ThreadStoreResult<()> {
|
||||
let id = items
|
||||
.last()
|
||||
.filter(|item| {
|
||||
matches!(
|
||||
item.content,
|
||||
codex_protocol::realtime::RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
.map(|item| item.id.clone());
|
||||
let records = items.into_iter().map(RolloutItem::RealtimeItem).collect();
|
||||
if let Some(id) = id {
|
||||
seal_realtime_records(store, thread_id, id, records).await
|
||||
} else {
|
||||
append_items(
|
||||
store,
|
||||
AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: records,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
flush_thread(store, thread_id).await
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn seal_realtime_input(
|
||||
store: &LocalThreadStore,
|
||||
thread_id: ThreadId,
|
||||
|
||||
@@ -547,6 +547,14 @@ impl LocalThreadStore {
|
||||
}
|
||||
|
||||
impl ThreadStore for LocalThreadStore {
|
||||
fn seal_realtime_items(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
items: Vec<RealtimeItem>,
|
||||
) -> ThreadStoreFuture<'_, ()> {
|
||||
Box::pin(async move { live_writer::seal_realtime_items(self, thread_id, items).await })
|
||||
}
|
||||
|
||||
fn seal_realtime_input(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
|
||||
@@ -3,6 +3,8 @@ use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::ThreadHistoryMode;
|
||||
use codex_protocol::protocol::ThreadMemoryMode;
|
||||
use codex_protocol::realtime::RealtimeItem;
|
||||
use codex_protocol::realtime::RealtimeItemContent;
|
||||
use codex_rollout::ResponseItemEnvelope;
|
||||
use codex_rollout::RolloutItem;
|
||||
use codex_rollout::RolloutRecorder;
|
||||
@@ -94,3 +96,68 @@ async fn retry_after_uncertain_local_input_seal_does_not_append_again() {
|
||||
});
|
||||
assert_eq!(accepted.count(), 1);
|
||||
}
|
||||
|
||||
/// A failed directory sync can leave the standalone hangup input in the file.
|
||||
/// A retry must confirm the original batch instead of duplicating that context.
|
||||
#[tokio::test]
|
||||
async fn retry_after_uncertain_local_carryover_seal_does_not_append_again() {
|
||||
let home = TempDir::new().unwrap();
|
||||
let uuid = Uuid::now_v7();
|
||||
let thread_id = codex_protocol::ThreadId::from_string(&uuid.to_string()).unwrap();
|
||||
let path = write_session_file_with_history_mode(
|
||||
home.path(),
|
||||
"2025-01-03T12-00-00",
|
||||
uuid,
|
||||
ThreadHistoryMode::Paginated,
|
||||
)
|
||||
.unwrap();
|
||||
let store = LocalThreadStore::new(test_config(home.path()), /*state_db*/ None);
|
||||
store
|
||||
.resume_thread(ResumeThreadParams {
|
||||
history_revision: None,
|
||||
thread_id,
|
||||
rollout_path: Some(path.clone()),
|
||||
history: None,
|
||||
include_archived: true,
|
||||
metadata: ThreadPersistenceMetadata {
|
||||
cwd: Some(home.path().to_path_buf()),
|
||||
model_provider: "test-provider".into(),
|
||||
memory_mode: ThreadMemoryMode::Enabled,
|
||||
},
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let tail = RealtimeItem {
|
||||
id: "event-tail".into(),
|
||||
realtime_session_id: "call-1".into(),
|
||||
observed_at_ms: Some(2),
|
||||
content: RealtimeItemContent::SessionInputSaved {
|
||||
reason: codex_protocol::realtime::SessionInputReason::SessionEnd,
|
||||
source_event_ids: Vec::new(),
|
||||
input_item_id: "input-tail".into(),
|
||||
input: "last words".into(),
|
||||
},
|
||||
};
|
||||
let parent = path.parent().unwrap();
|
||||
let moved = home.path().join("moved");
|
||||
std::fs::rename(parent, &moved).unwrap();
|
||||
assert!(
|
||||
store
|
||||
.seal_realtime_items(thread_id, vec![tail.clone()])
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
std::fs::rename(&moved, parent).unwrap();
|
||||
store
|
||||
.seal_realtime_items(thread_id, vec![tail.clone()])
|
||||
.await
|
||||
.unwrap();
|
||||
let (items, _, _) = RolloutRecorder::load_rollout_items(&path).await.unwrap();
|
||||
assert_eq!(
|
||||
items
|
||||
.iter()
|
||||
.filter(|item| matches!(item, RolloutItem::RealtimeItem(item) if item.id == tail.id))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,13 @@ use codex_rollout::ResponseItemEnvelope;
|
||||
/// about dynamically registered contextual fragments that thread-store cannot
|
||||
/// depend on.
|
||||
pub(super) fn counts_as_boundary(envelope: &ResponseItemEnvelope) -> bool {
|
||||
if envelope
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let response = &envelope.item;
|
||||
if matches!(response, ResponseItem::AgentMessage { .. }) {
|
||||
return true;
|
||||
@@ -62,7 +69,13 @@ pub(super) fn drop_last_n_user_turns(history: &mut Vec<ResponseItemEnvelope>, nu
|
||||
} else {
|
||||
user_positions[user_positions.len() - count]
|
||||
};
|
||||
while cut_index > first_turn_index && is_pre_turn_context_update(&history[cut_index - 1].item) {
|
||||
while cut_index > first_turn_index
|
||||
&& !history[cut_index - 1]
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
&& is_pre_turn_context_update(&history[cut_index - 1].item)
|
||||
{
|
||||
cut_index -= 1;
|
||||
}
|
||||
history.truncate(cut_index);
|
||||
|
||||
@@ -196,7 +196,12 @@ impl RollbackPlanner {
|
||||
content: content.clone(),
|
||||
});
|
||||
}
|
||||
} else if rollback::is_pre_turn_context_update(&response.item) {
|
||||
} else if !response
|
||||
.metadata
|
||||
.as_ref()
|
||||
.is_some_and(|metadata| metadata.realtime_session_end_input)
|
||||
&& rollback::is_pre_turn_context_update(&response.item)
|
||||
{
|
||||
// Until another user boundary arrives, this is trailing context for the
|
||||
// previous turn. Keep that fallback owner so rollback drops it when there is
|
||||
// no later turn to attach it to.
|
||||
|
||||
@@ -1740,6 +1740,76 @@ async fn migration_preserves_reverse_replay_anchor_after_pre_compaction_rollback
|
||||
assert!(replacement_history.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migration_does_not_count_session_end_input_in_checkpoint_rollback() {
|
||||
let first = input_response_message("user", "first turn");
|
||||
let tail = input_response_message(
|
||||
"user",
|
||||
"<environment_context>saved speech</environment_context>",
|
||||
);
|
||||
let next = input_response_message("user", "next turn");
|
||||
let mut saved_tail = codex_rollout::ResponseItemEnvelope::new(tail.clone());
|
||||
saved_tail
|
||||
.metadata
|
||||
.get_or_insert_default()
|
||||
.realtime_session_end_input = true;
|
||||
|
||||
let migrated = migrate_after_rollback(vec![
|
||||
rollout_response_item(first.clone()),
|
||||
RolloutItem::ResponseItem(saved_tail.clone()),
|
||||
rollout_response_item(next.clone()),
|
||||
])
|
||||
.await;
|
||||
assert!(migrated.iter().any(|item| {
|
||||
matches!(item, RolloutItem::ResponseItem(response) if response == &saved_tail)
|
||||
}));
|
||||
|
||||
for (before_checkpoint, after_checkpoint, expected) in [
|
||||
(
|
||||
vec![rollout_response_item(first.clone())],
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
),
|
||||
(
|
||||
vec![rollout_response_item(first.clone())],
|
||||
vec![
|
||||
started("next-turn"),
|
||||
rollout_response_item(next.clone()),
|
||||
completed("next-turn"),
|
||||
],
|
||||
vec![first.clone(), tail],
|
||||
),
|
||||
] {
|
||||
let mut checkpoint = compacted(Vec::new());
|
||||
let RolloutItem::Compacted(item) = &mut checkpoint else {
|
||||
unreachable!();
|
||||
};
|
||||
item.replacement_history = Some(vec![first.clone().into(), saved_tail.clone()]);
|
||||
let migrated = migrate_after_rollback(
|
||||
before_checkpoint
|
||||
.into_iter()
|
||||
.chain([checkpoint])
|
||||
.chain(after_checkpoint)
|
||||
.collect(),
|
||||
)
|
||||
.await;
|
||||
let replacement = migrated
|
||||
.into_iter()
|
||||
.find_map(|item| match item {
|
||||
RolloutItem::Compacted(item) => item.replacement_history,
|
||||
_ => None,
|
||||
})
|
||||
.expect("surviving checkpoint");
|
||||
assert_eq!(
|
||||
replacement
|
||||
.into_iter()
|
||||
.map(codex_rollout::ResponseItemEnvelope::into_item)
|
||||
.collect::<Vec<_>>(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migration_preserves_same_turn_evidence_across_rollback_checkpoints() {
|
||||
assert_migrated_evidence_order(/*steer_order*/ None).await;
|
||||
|
||||
@@ -248,7 +248,7 @@ async fn check_remote_voice_start(
|
||||
let starts = recorded_params(&requests, "thread/realtime/start");
|
||||
assert_eq!(starts.len(), 1);
|
||||
let start = &starts[0];
|
||||
assert_eq!(start["realtimeSessionId"], thread_id.to_string());
|
||||
assert_eq!(start["realtimeSessionId"], "rtc_tui");
|
||||
assert_eq!(start["voiceSessionId"], voice_session_id);
|
||||
assert_eq!(
|
||||
serde_json::json!({
|
||||
|
||||
@@ -45,7 +45,7 @@ impl AppServerSession {
|
||||
realtime_start_instructions: None,
|
||||
realtime_end_instructions: None,
|
||||
prompt: None,
|
||||
realtime_session_id: Some(thread_id.to_string()),
|
||||
realtime_session_id: Some(call_id.clone()),
|
||||
voice_session_id: Some(voice_session_id.to_string()),
|
||||
transport: Some(ThreadRealtimeStartTransport::ExistingCall {
|
||||
// The TUI owns the peer connection and has already negotiated this call.
|
||||
|
||||
Reference in New Issue
Block a user