Files
codex/codex-rs/tui/src/chatwidget/realtime_tests.rs
T
Benjamin Carlsson 322bbf4d84 Classify voice session failures and record terminal outcomes (#52756)
## Why

Voice failure metrics lacked cause and lifecycle phase, and session durations were recorded before the final outcome was known. A failed control send could also overwrite a more specific WebRTC error.

## What changed

- Tag `codex.voice.session.failure` with bounded `cause` and `phase` values, recording at most one failure per session.
- Capture duration when stopping and emit `codex.voice.session.duration` at cleanup with a `success` or `failure` outcome.
- Carry typed WebRTC errors into the TUI, preserve existing errors when control sends fail, and avoid duplicate errors after startup failure.
- Record app-server disconnect failures for background voice sessions as well as the visible session.

## Testing

Add coverage for terminal outcome and failure metric attributes, preservation of specific startup and audio-session errors, and startup failures without duplicate control-channel errors.

GitOrigin-RevId: 5aeda6e766093e3fce7206560aa83b6f7770dddb
2026-10-10 02:54:59 +00:00

156 lines
5.1 KiB
Rust

//! Regression coverage for thread-owned voice sessions and transcript/handoff safety.
//! Synthetic events preserve typed turns and reject stale session generations.
#[path = "realtime/recording_controls_tests.rs"]
mod recording_controls_tests;
#[path = "realtime/session_metrics_tests.rs"]
mod session_metrics_tests;
use super::RealtimeConversationPhase;
use super::RealtimeFailureCause;
use crate::app_command::AppCommand;
use crate::app_event::AppEvent;
use crate::chatwidget::ChatWidget;
use crate::chatwidget::ReplayKind;
use crate::chatwidget::tests::make_chatwidget_manual_with_sender;
use crate::chatwidget::tests::render_bottom_popup;
use crate::history_cell::FinalMessageSeparator;
use codex_app_server_protocol::AgentMessageDeltaNotification;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ItemStartedNotification;
use codex_app_server_protocol::ReasoningSummaryPartAddedNotification;
use codex_app_server_protocol::ReasoningSummaryTextDeltaNotification;
use codex_app_server_protocol::ReasoningTextDeltaNotification;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnCompletedNotification;
use codex_app_server_protocol::TurnItemsView;
use codex_app_server_protocol::TurnStatus;
use codex_app_server_protocol::UserInput;
use codex_protocol::ThreadId;
use codex_protocol::items::AsyncUserInputQuestion;
use codex_protocol::models::MessagePhase;
use futures::future::AbortHandle;
use std::collections::VecDeque;
// Model the app's atomic handoff before inspecting its ordinary history events.
pub(crate) fn commit_realtime_history_events(
chat: &mut ChatWidget,
events: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
) {
let mut forwarded = Vec::new();
while let Ok(event) = events.try_recv() {
match event {
AppEvent::CommitRealtimeTranscriptHistory => forwarded.extend(
chat.take_realtime_transcript_history()
.into_iter()
.map(AppEvent::InsertHistoryCell),
),
event => forwarded.push(event),
}
}
for event in forwarded {
chat.app_event_tx.send(event);
}
}
fn activate_voice(chat: &mut ChatWidget) -> ThreadId {
let thread_id = ThreadId::new();
activate_voice_for_thread(chat, thread_id);
thread_id
}
pub(crate) fn activate_voice_for_thread(chat: &mut ChatWidget, thread_id: ThreadId) {
chat.thread_id = Some(thread_id);
chat.realtime_conversation.phase = RealtimeConversationPhase::Active;
chat.realtime_conversation.thread_id = Some(thread_id);
chat.realtime_conversation.backend_started = true;
chat.realtime_conversation.latest_input_was_voice = true;
}
fn user_item(text: &str) -> ThreadItem {
ThreadItem::UserMessage {
id: format!("user-{text}"),
client_id: None,
content: vec![UserInput::Text {
text: text.to_string(),
text_elements: Vec::new(),
}],
}
}
fn agent_item(item_id: &str, text: &str, phase: Option<MessagePhase>) -> ThreadItem {
ThreadItem::AgentMessage {
id: item_id.to_string(),
text: text.to_string(),
phase,
questions: None,
memory_citation: None,
delivery: None,
}
}
fn start_item(chat: &mut ChatWidget, thread_id: ThreadId, turn_id: &str, item: ThreadItem) {
chat.handle_server_notification(
ServerNotification::ItemStarted(ItemStartedNotification {
thread_id: thread_id.to_string(),
turn_id: turn_id.to_string(),
item,
started_at_ms: 0,
}),
/*replay_kind*/ None,
);
}
fn complete_item(chat: &mut ChatWidget, thread_id: ThreadId, turn_id: &str, item: ThreadItem) {
chat.handle_server_notification(
ServerNotification::ItemCompleted(ItemCompletedNotification {
thread_id: thread_id.to_string(),
turn_id: turn_id.to_string(),
item,
completed_at_ms: 0,
}),
/*replay_kind*/ None,
);
}
fn finish_turn(
chat: &mut ChatWidget,
thread_id: ThreadId,
turn_id: &str,
items: Vec<ThreadItem>,
status: TurnStatus,
) {
chat.handle_server_notification(
ServerNotification::TurnCompleted(TurnCompletedNotification {
thread_id: thread_id.to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items,
items_view: TurnItemsView::Summary,
status,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
},
}),
/*replay_kind*/ None,
);
}
#[path = "realtime_tests/caption_replay.rs"]
mod caption_replay;
#[path = "realtime_tests/handoff_privacy.rs"]
mod handoff_privacy;
#[path = "realtime_tests/handoffs.rs"]
mod handoffs;
#[path = "realtime_tests/lifecycle.rs"]
mod lifecycle;
#[path = "realtime_tests/speech_recovery.rs"]
mod speech_recovery;
#[path = "realtime_tests/transcripts.rs"]
mod transcripts;