Preserve voice caption order when replaying TUI history (#44749)

## Why

Retained voice captions were appended after replayed history, placing earlier speech after later typed turns when switching back to a thread.

## What changed

Anchor completed captions to the next live turn and restore them before that turn during replay. Handle buffered item, delta, and completion events even when the turn-start event has been evicted. Restore remaining captions at the end of replay.

## Testing

Extend regression coverage to verify caption order and avoid duplicates across repeated thread switches. Add coverage for inactive-thread captions replayed before a later buffered turn without its start event, with and without a message delta.

GitOrigin-RevId: 6860a649bdcdda4937afb3ab4bbd75cf4ea5b4e6
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-11 06:56:37 +00:00
1 parent eab107fed0
commit ab95cd4dd9
9 files changed
+220 -14

No files matched your search

+13 -1
View File
@@ -65,6 +65,11 @@ impl ChatWidget {
self.on_thread_settings_updated(notification);
}
ServerNotification::TurnStarted(notification) => {
if from_replay {
self.restore_realtime_transcripts_before_turn(&notification.turn.id);
} else {
self.anchor_realtime_transcripts_before_turn(&notification.turn.id);
}
if replay_kind.is_none() {
self.clear_misalignment_for_new_turn(
&notification.turn.id,
@@ -79,6 +84,7 @@ impl ChatWidget {
}
}
ServerNotification::TurnCompleted(notification) => {
self.restore_realtime_transcripts_before_turn(&notification.turn.id);
self.handle_turn_completed_notification(notification, replay_kind);
}
ServerNotification::ItemStarted(notification) => {
@@ -88,6 +94,7 @@ impl ChatWidget {
self.handle_item_completed_notification(notification, replay_kind);
}
ServerNotification::AgentMessageDelta(notification) => {
self.restore_realtime_transcripts_before_turn(&notification.turn_id);
if !self.is_realtime_delegated_reasoning_turn(&notification.turn_id)
&& (from_replay
|| !self.is_realtime_delegated_agent_item(
@@ -98,7 +105,10 @@ impl ChatWidget {
self.on_agent_message_delta(notification.delta);
}
}
ServerNotification::PlanDelta(notification) => self.on_plan_delta(notification.delta),
ServerNotification::PlanDelta(notification) => {
self.restore_realtime_transcripts_before_turn(&notification.turn_id);
self.on_plan_delta(notification.delta);
}
ServerNotification::ReasoningSummaryTextDelta(notification) => {
if !self.is_realtime_delegated_reasoning_item(
&notification.turn_id,
@@ -477,6 +487,7 @@ impl ChatWidget {
notification: ItemStartedNotification,
replay_kind: Option<ReplayKind>,
) {
self.restore_realtime_transcripts_before_turn(&notification.turn_id);
match notification.item {
ThreadItem::UserMessage { content, .. } if replay_kind.is_none() => {
self.note_realtime_user_item_started(&notification.turn_id, &content);
@@ -565,6 +576,7 @@ impl ChatWidget {
notification: ItemCompletedNotification,
replay_kind: Option<ReplayKind>,
) {
self.restore_realtime_transcripts_before_turn(&notification.turn_id);
if replay_kind.is_none()
&& self.is_realtime_delegated_reasoning_turn(&notification.turn_id)
&& realtime::is_private_realtime_agent_item(&notification.item)
+7
View File
@@ -2,6 +2,7 @@
//! Completed captions and both speakers' partials stay bounded across widget replacement.
mod recording_controls;
mod transcript_replay;
use super::ChatWidget;
use super::HistoryCell;
@@ -72,6 +73,7 @@ pub(crate) struct RealtimeTranscriptRecord {
pub(crate) role: String,
pub(crate) text: String,
pub(crate) complete: bool,
pub(crate) before_turn_id: Option<String>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -161,6 +163,7 @@ pub(super) struct RealtimeConversationUiState {
pub(super) live_transcript_cell: Option<Box<dyn HistoryCell>>,
pending_history_cells: VecDeque<Box<dyn HistoryCell>>,
accepted_transcripts: VecDeque<RealtimeTranscriptRecord>,
replay_transcripts: Option<VecDeque<RealtimeTranscriptRecord>>,
latest_input_was_voice: bool,
input_generation: u64,
latest_voice_input_fingerprint: Option<(usize, u64)>,
@@ -1014,6 +1017,7 @@ impl ChatWidget {
role,
text,
complete: false,
before_turn_id: None,
});
};
if let Some((role, text)) = self.realtime_conversation.interleaved_transcript.take() {
@@ -1347,6 +1351,7 @@ impl ChatWidget {
role: role.clone(),
text: text.clone(),
complete: true,
before_turn_id: None,
},
);
text.clone()
@@ -1488,6 +1493,7 @@ impl ChatWidget {
role: role.clone(),
text: text.clone(),
complete: true,
before_turn_id: None,
});
while self.realtime_conversation.pending_history_cells.len()
>= MAX_PENDING_TRANSCRIPT_CELLS
@@ -1713,6 +1719,7 @@ impl ChatWidget {
role,
text,
complete: true,
before_turn_id: None,
});
}
let pending_history_cells =
@@ -0,0 +1,57 @@
//! Keep retained voice captions before the subsequent agent turn during history replay.
//!
//! Anchors are assigned only to completed captions when a new live turn starts. Partial
//! captions remain eligible for late completion, and captions with no later turn stay last.
use super::ChatWidget;
use super::MAX_REPLAY_TRANSCRIPT_CELLS;
use super::RealtimeTranscriptRecord;
use std::collections::VecDeque;
impl ChatWidget {
pub(crate) fn anchor_realtime_transcripts_before_turn(&mut self, turn_id: &str) {
for record in &mut self.realtime_conversation.accepted_transcripts {
if record.complete && record.before_turn_id.is_none() {
record.before_turn_id = Some(turn_id.to_string());
}
}
}
pub(crate) fn queue_realtime_transcripts_for_replay(
&mut self,
records: VecDeque<RealtimeTranscriptRecord>,
) {
self.realtime_conversation.replay_transcripts = Some(records);
}
pub(crate) fn restore_realtime_transcripts_before_turn(&mut self, turn_id: &str) {
let mut remaining = VecDeque::new();
let Some(records) = self.realtime_conversation.replay_transcripts.take() else {
return;
};
for record in records {
if record.before_turn_id.as_deref() == Some(turn_id) {
// Insert after the queued consolidation, before replaying the next turn.
let cell = self.realtime_transcript_history_cell(&record.role, &record.text);
self.add_boxed_history(cell);
if self.realtime_conversation.accepted_transcripts.len()
>= MAX_REPLAY_TRANSCRIPT_CELLS
{
self.realtime_conversation.accepted_transcripts.pop_front();
}
self.realtime_conversation
.accepted_transcripts
.push_back(record);
} else {
remaining.push_back(record);
}
}
self.realtime_conversation.replay_transcripts = Some(remaining);
}
pub(crate) fn finish_realtime_transcript_replay(&mut self) {
if let Some(records) = self.realtime_conversation.replay_transcripts.take() {
self.restore_realtime_transcript_cells(records);
}
}
}
@@ -333,6 +333,7 @@ async fn restored_partial_caption_accepts_late_completion_without_duplicate_hist
role: "user".into(),
text: "last ".into(),
complete: false,
before_turn_id: None,
},
]));
let live = chat
@@ -383,6 +384,7 @@ async fn empty_late_completion_discards_the_restored_partial() {
role: "user".into(),
text: "unfinished".into(),
complete: false,
before_turn_id: None,
},
]));
chat.on_realtime_transcript_done("user".into(), String::new());
+1
View File
@@ -112,6 +112,7 @@ impl ChatWidget {
}))
.collect::<Vec<_>>();
for (turn, hidden_nested_review_turn) in turns.into_iter().zip(hidden_nested_review_turns) {
self.restore_realtime_transcripts_before_turn(&turn.id);
// Defer completed metadata-only turns until their page loads. Active
// turns must restore their lifecycle even before any items are available.
if turn.status == TurnStatus::Completed