Files
codex/codex-rs/core/src/session/turn_tests.rs
T
Zahan Malkani ad5dd4a110 Persist realtime handoffs before accepting turn input (#62288)
## Why

Realtime history needs to record which transcript frames reached the
coding agent and which turn received them. If a handoff fails or the
conversation reconnects, Codex needs to distinguish speech that was
already used from speech that is still available for the next input.

## What changed

- Route handoffs through normal turn admission to start or steer a turn.
  When realtime frame history is enabled, persist the transcript frames
  and the receiving turn's input before publishing acceptance or exposing
  the input to the model. Attribute only transcript frames represented
  in the rendered input.
- Reserve transcript frames across overlapping handoffs and release them
  on definite rejection. Retry an uncertain write with the same input
  and require history recovery if the write still cannot be confirmed.
- On V3 reconnect, restore persisted speech that has not been consumed
  and put it before newly captured speech.

## Testing

Add regression tests for attribution after transcript truncation,
overlapping handoffs, V3 reconnect ordering, and preservation of typed
input when a handoff in the same turn fails before model sampling.

GitOrigin-RevId: 432dd51971f7ced20344ebc7f0c7b007bb849f54
2026-11-18 14:26:53 +00:00

255 lines
9.6 KiB
Rust

use super::*;
use crate::realtime_history::RealtimeHistoryState;
use crate::session::UserInputMetadata;
use crate::session::tests::make_session_and_context;
use codex_extension_api::ExtensionData;
use codex_extension_api::TurnItemContributor;
use codex_protocol::ResponseItemId;
use codex_protocol::items::AgentMessageContent;
use codex_protocol::protocol::ItemCompletedEvent;
use codex_protocol::protocol::ItemStartedEvent;
use codex_protocol::protocol::RealtimeConversationStartedEvent;
use codex_protocol::protocol::RealtimeConversationVersion;
use codex_protocol::protocol::RealtimeHandoffRequested;
use pretty_assertions::assert_eq;
use std::sync::Arc;
use tracing_subscriber::prelude::*;
#[tokio::test]
async fn failed_realtime_receipt_preserves_sibling_input_before_sampling() {
// The fixture has no live thread, so committing a reserved handoff fails.
let (mut session, turn_context) = make_session_and_context().await;
let mut history = RealtimeHistoryState::default();
history.frame_enrollment = Some(true);
history.observe(&EventMsg::RealtimeConversationStarted(
RealtimeConversationStartedEvent {
realtime_session_id: Some("call".to_string()),
version: RealtimeConversationVersion::V2,
},
));
let handoff = history
.prepare_handoff(&RealtimeHandoffRequested {
handoff_id: "handoff".to_string(),
item_id: "item".to_string(),
input_transcript: "voice prompt".to_string(),
active_transcript: Vec::new(),
})
.unwrap();
assert!(history.reserve_handoff(handoff.id));
session.realtime_history = Some(tokio::sync::Mutex::new(history));
let sibling_id = uuid::Uuid::now_v7();
let input = |text, id, realtime_handoff_id| TurnInput::UserInput {
content: vec![ResolvedUserInput::text(text)],
client_id: None,
metadata: UserInputMetadata {
response_item_uuid: Some(id),
realtime_handoff_id,
..Default::default()
},
};
let session = Arc::new(session);
let error = run_turn(
Arc::clone(&session),
Arc::new(turn_context),
vec![
input(handoff.input, handoff.id, Some(handoff.id)),
input(
"typed sibling".to_string(),
sibling_id,
/*realtime_handoff_id*/ None,
),
],
&mut McpStartupRequirements::default(),
/*prewarmed_client_session*/ None,
CancellationToken::new(),
)
.await
.expect_err("a turn with an unconfirmed handoff must fail before sampling");
assert!(error.to_string().contains("accept realtime handoff"));
let sibling = ResponseItemId::for_admitted_user_input(sibling_id);
let voice = ResponseItemId::for_admitted_user_input(handoff.id);
let history = session.clone_history().await;
let recorded: Vec<_> = history
.raw_items()
.filter_map(ResponseItem::id)
.filter(|id| *id == &sibling || *id == &voice)
.collect();
assert_eq!(recorded, vec![&sibling]);
}
struct RewriteAgentMessageContributor;
impl TurnItemContributor for RewriteAgentMessageContributor {
fn contribute<'a>(
&'a self,
_thread_store: &'a ExtensionData,
_turn_store: &'a ExtensionData,
item: &'a mut TurnItem,
) -> codex_extension_api::ExtensionFuture<'a, Result<(), String>> {
Box::pin(async move {
if let TurnItem::AgentMessage(agent_message) = item {
agent_message.phase = Some(codex_protocol::models::MessagePhase::Commentary);
agent_message.content = vec![AgentMessageContent::Text {
text: "plan contributed assistant text".to_string(),
}];
}
Ok(())
})
}
}
fn assistant_output_text(text: &str) -> ResponseItem {
ResponseItem::Message {
status: None,
encrypted_content: None,
id: Some(ResponseItemId::with_suffix("msg", "1")),
role: "assistant".to_string(),
content: vec![ContentItem::OutputText {
annotations: None,
logprobs: None,
text: text.to_string(),
}],
phase: None,
internal_chat_message_metadata_passthrough: None,
}
}
#[test]
fn post_sampling_token_estimate_is_disabled_by_always_on_sinks() {
let feedback = codex_feedback::CodexFeedback::new();
let subscriber = tracing_subscriber::registry()
.with(feedback.logger_layer())
.with(tracing_subscriber::fmt::layer().with_filter(codex_state::log_db::default_filter()));
static METADATA: tracing::Metadata<'static> = tracing::metadata! {
name: "post sampling token estimate filter probe",
target: POST_SAMPLING_TOKEN_ESTIMATE_TARGET,
level: tracing::Level::TRACE,
fields: &["turn_id", "estimated_token_count", "message"],
callsite: &CALLSITE,
kind: tracing::metadata::Kind::EVENT.hint(),
};
static CALLSITE: tracing::callsite::DefaultCallsite =
tracing::callsite::DefaultCallsite::new(&METADATA);
assert!(tracing::Subscriber::register_callsite(&subscriber, &METADATA).is_never());
}
#[tokio::test]
async fn plan_mode_uses_contributed_turn_item_for_last_agent_message() {
let (mut session, turn_context) = crate::session::tests::make_session_and_context().await;
let mut builder = codex_extension_api::ExtensionRegistryBuilder::new();
builder.turn_item_contributor(Arc::new(RewriteAgentMessageContributor));
session.services.extensions = Arc::new(builder.build());
let turn_store = ExtensionData::new(turn_context.sub_id.clone());
let mut state = PlanModeStreamState::new(/*unfinished_plan_id*/ None);
let mut last_agent_message = AssistantMessages::default();
let item = assistant_output_text("original assistant text");
let step_context = StepContext::for_test(Arc::new(turn_context));
let handled = handle_assistant_item_done_in_plan_mode(
&session,
&step_context,
&turn_store,
&item,
&mut state,
/*previously_active_item*/ None,
&mut last_agent_message,
/*moderation*/ None,
)
.await;
assert!(handled);
assert_eq!(
last_agent_message.trailing_commentary.as_deref(),
Some("plan contributed assistant text")
);
assert_eq!(last_agent_message.final_answer, None);
}
/// A model retry can reuse an unfinished plan. A later completion must use its own ID.
#[tokio::test]
async fn plan_attempts_only_reuse_unfinished_item_ids() {
let (session, turn, events) = crate::session::tests::make_session_and_context_with_rx().await;
let mut first = PlanModeStreamState::new(/*unfinished_plan_id*/ None).plan_item_state;
let first_id = first.item_id.clone();
first.start(&session, &turn).await;
first.push_delta(&session, &turn, "Partial").await;
let mut retry = PlanModeStreamState::new(first.unfinished_id()).plan_item_state;
retry.start(&session, &turn).await;
retry.push_delta(&session, &turn, "Plan A").await;
retry
.complete_with_text(&session, &turn, "Plan A".into())
.await;
retry
.complete_with_text(&session, &turn, "Repeated A".into())
.await;
let mut next = PlanModeStreamState::new(retry.unfinished_id()).plan_item_state;
next.start(&session, &turn).await;
next.push_delta(&session, &turn, "Plan B").await;
next.complete_with_text(&session, &turn, "Plan B".into())
.await;
let mut observed = Vec::new();
while let Ok(event) = events.try_recv() {
match event.msg {
EventMsg::ItemStarted(ItemStartedEvent {
item: TurnItem::Plan(plan),
..
}) => {
observed.push(("start", plan.id, plan.text));
}
EventMsg::PlanDelta(event) => observed.push(("delta", event.item_id, event.delta)),
EventMsg::ItemCompleted(ItemCompletedEvent {
item: TurnItem::Plan(plan),
..
}) => {
observed.push(("complete", plan.id, plan.text));
}
_ => {}
}
}
let next_id = next.item_id;
assert_eq!(Uuid::parse_str(&first_id).unwrap().get_version_num(), 7);
assert_ne!(first_id, next_id);
assert_eq!(
observed,
vec![
("start", first_id.clone(), String::new()),
("delta", first_id.clone(), "Partial".into()),
("start", first_id.clone(), String::new()),
("delta", first_id.clone(), "Plan A".into()),
("complete", first_id, "Plan A".into()),
("start", next_id.clone(), String::new()),
("delta", next_id.clone(), "Plan B".into()),
("complete", next_id, "Plan B".into()),
],
);
}
#[test]
fn realtime_user_verification_notice_excludes_request_payload() {
let event = EventMsg::ElicitationRequest(codex_protocol::approvals::ElicitationRequestEvent {
turn_id: None,
server_name: "private-server-name".to_string(),
environment_id: None,
id: codex_protocol::mcp::RequestId::String("private-request-id".to_string()),
request: codex_protocol::approvals::ElicitationRequest::UserVerification {
meta: None,
title: "private-title".to_string(),
description: "private-description".to_string(),
challenge: "private-challenge".to_string(),
},
});
assert_eq!(
realtime_text_for_event(&event),
Some(RealtimeEventText::Handoff(
"<user_verification_notice>User verification is required. Please respond in the app.</user_verification_notice>".to_string(),
None,
)),
);
}