From 6b6fdc3572c328e0ba7d67a5eca9e558738d22c7 Mon Sep 17 00:00:00 2001 From: Benjamin Carlsson Date: Tue, 8 Sep 2026 04:28:53 +0000 Subject: [PATCH] Preserve split-flap animation state when voice transcripts scroll (#43699) ## Why Trimming the live voice transcript to its 1,024-byte window could restart animations for retained words or skip animations for new text that repeats existing text. ## What changed Track discarded prefix bytes and render the retained text to identify which tiles survive. Preserve their arrival times and animation phase so only new tiles begin animating. Start fresh when the speaker changes or the retained text no longer matches. ## Testing Add regression coverage for repeated text, retained-word visibility, Unicode and Markdown rendering, reduced motion, and replaced or changed-speaker windows. GitOrigin-RevId: dd33f76441baa4d7787d0a9a1e72b281476acb4c --- codex-rs/tui/src/chatwidget/realtime.rs | 7 +- .../tui/src/chatwidget/realtime_split_flap.rs | 33 +++- .../chatwidget/realtime_split_flap_tests.rs | 168 +++++++++++++++++- 3 files changed, 193 insertions(+), 15 deletions(-) diff --git a/codex-rs/tui/src/chatwidget/realtime.rs b/codex-rs/tui/src/chatwidget/realtime.rs index 893d0efadf..77345cd060 100644 --- a/codex-rs/tui/src/chatwidget/realtime.rs +++ b/codex-rs/tui/src/chatwidget/realtime.rs @@ -1233,6 +1233,7 @@ impl ChatWidget { self.realtime_conversation.transcript_role = Some(role); } self.realtime_conversation.transcript.push_str(&delta); + let mut discarded_prefix_bytes = 0; if self.realtime_conversation.transcript.len() > MAX_TRANSCRIPT_BYTES { let mut start = self.realtime_conversation.transcript.len() - MAX_TRANSCRIPT_BYTES; while !self @@ -1243,6 +1244,7 @@ impl ChatWidget { start += 1; } self.realtime_conversation.transcript.drain(..start); + discarded_prefix_bytes = start; } let role = self .realtime_conversation @@ -1254,13 +1256,12 @@ impl ChatWidget { .live_transcript_cell .as_deref() .and_then(|cell| cell.as_any().downcast_ref::()); - let cell = - self.realtime_transcript_history_cell(role, &self.realtime_conversation.transcript); let live_cell = SplitFlapTranscriptCell::new( - cell, + |text| self.realtime_transcript_history_cell(role, text), role, &self.realtime_conversation.transcript, previous, + discarded_prefix_bytes, MotionMode::from_animations_enabled(self.config.animations), self.frame_requester.clone(), ); diff --git a/codex-rs/tui/src/chatwidget/realtime_split_flap.rs b/codex-rs/tui/src/chatwidget/realtime_split_flap.rs index 9612a2123a..f9635c52cc 100644 --- a/codex-rs/tui/src/chatwidget/realtime_split_flap.rs +++ b/codex-rs/tui/src/chatwidget/realtime_split_flap.rs @@ -50,14 +50,16 @@ pub(super) struct SplitFlapTranscriptCell { impl SplitFlapTranscriptCell { pub(super) fn new( - inner: Box, + render: impl Fn(&str) -> Box, role: &str, target: &str, previous: Option<&Self>, + discarded_prefix_bytes: usize, motion_mode: MotionMode, frame_requester: FrameRequester, ) -> Self { let now = Instant::now(); + let inner = render(target); let visible_target = visible_glyphs(inner.as_ref()); let mut flap_sample = visible_target .bytes() @@ -69,13 +71,28 @@ impl SplitFlapTranscriptCell { flap_sample.extend(visible_target.bytes().rev().take(FLAP_SAMPLE_WINDOW)); } flap_sample.reverse(); - let previous = previous.filter(|cell| { - cell.role == role - && target.starts_with(&cell.target) - && visible_target.starts_with(&cell.visible_target) + let previous = previous.and_then(|cell| { + let retained = cell.target.get(discarded_prefix_bytes..)?; + if cell.role != role || retained.is_empty() || !target.starts_with(retained) { + return None; + } + // Render the actual retained text: byte offsets are not tile offsets, and + // matching text alone could confuse repeated old words with new arrivals. + let retained_glyphs = if discarded_prefix_bytes == 0 { + cell.visible_target.clone() + } else { + visible_glyphs(render(retained).as_ref()) + }; + let discarded_tiles = cell + .visible_target + .len() + .checked_sub(retained_glyphs.len())?; + (cell.visible_target[discarded_tiles..] == retained_glyphs + && visible_target.starts_with(&retained_glyphs)) + .then_some((cell, discarded_tiles)) }); let mut tile_arrivals = previous - .map(|cell| cell.tile_arrivals.clone()) + .map(|(cell, discarded_tiles)| cell.tile_arrivals[discarded_tiles..].to_vec()) .unwrap_or_default(); let existing_tiles = tile_arrivals.len(); for index in existing_tiles..visible_target.len() { @@ -98,7 +115,9 @@ impl SplitFlapTranscriptCell { flap_sample, tile_arrivals, started_at: now, - phase_started_at: previous.map(|cell| cell.phase_started_at).unwrap_or(now), + phase_started_at: previous + .map(|(cell, _)| cell.phase_started_at) + .unwrap_or(now), motion_mode, frame_requester, }; diff --git a/codex-rs/tui/src/chatwidget/realtime_split_flap_tests.rs b/codex-rs/tui/src/chatwidget/realtime_split_flap_tests.rs index 8ebf0c3eed..bece1513be 100644 --- a/codex-rs/tui/src/chatwidget/realtime_split_flap_tests.rs +++ b/codex-rs/tui/src/chatwidget/realtime_split_flap_tests.rs @@ -21,12 +21,17 @@ impl HistoryCell for PlainTranscriptCell { } } +fn plain_transcript(text: &str) -> Box { + Box::new(PlainTranscriptCell(text.to_string())) +} + fn cell(text: &str, motion_mode: MotionMode) -> SplitFlapTranscriptCell { SplitFlapTranscriptCell::new( - Box::new(PlainTranscriptCell(text.to_string())), + plain_transcript, "user", text, /*previous*/ None, + /*discarded_prefix_bytes*/ 0, motion_mode, FrameRequester::test_dummy(), ) @@ -108,10 +113,11 @@ fn settled_tiles_briefly_glow_in_the_speakers_color() { ("user", MotionMode::Reduced, None), ] { let cell = SplitFlapTranscriptCell::new( - Box::new(PlainTranscriptCell("A".to_string())), + plain_transcript, role, "A", /*previous*/ None, + /*discarded_prefix_bytes*/ 0, motion_mode, FrameRequester::test_dummy(), ); @@ -140,10 +146,11 @@ fn settled_tiles_briefly_glow_in_the_speakers_color() { fn reduced_motion_keeps_the_original_transcript_and_requests_no_frames() { let (frame_requester, mut requests) = FrameRequester::test_channel(); let cell = SplitFlapTranscriptCell::new( - Box::new(PlainTranscriptCell("GATE 73".to_string())), + plain_transcript, "user", "GATE 73", /*previous*/ None, + /*discarded_prefix_bytes*/ 0, MotionMode::Reduced, frame_requester, ); @@ -160,10 +167,11 @@ fn appended_transcript_keeps_the_previous_words_settled() { *arrival -= ANIMATION_DURATION; } let appended = SplitFlapTranscriptCell::new( - Box::new(PlainTranscriptCell("GATE 73".to_string())), + plain_transcript, "user", "GATE 73", Some(&first), + /*discarded_prefix_bytes*/ 0, MotionMode::Animated, FrameRequester::test_dummy(), ); @@ -173,6 +181,155 @@ fn appended_transcript_keeps_the_previous_words_settled() { assert_eq!(appended.tile_arrivals[..4], first.tile_arrivals); } +#[test] +fn sliding_a_full_window_animates_only_new_tiles_even_when_text_repeats() { + let text = "A".repeat(/*n*/ 1024); + for motion_mode in [MotionMode::Animated, MotionMode::Reduced] { + let mut first = cell(&text, motion_mode); + for arrival in &mut first.tile_arrivals { + *arrival -= ANIMATION_DURATION; + } + let (frame_requester, mut requests) = FrameRequester::test_channel(); + let shifted = SplitFlapTranscriptCell::new( + plain_transcript, + "user", + &text, + Some(&first), + /*discarded_prefix_bytes*/ 2, + motion_mode, + frame_requester, + ); + + assert_eq!(shifted.tile_arrivals[..1022], first.tile_arrivals[2..]); + assert_eq!( + shifted.tile_arrivals[1022..], + [ + shifted.started_at - FRAME_INTERVAL, + shifted.started_at - FRAME_INTERVAL + Duration::from_millis(/*millis*/ 8), + ] + ); + assert_eq!(shifted.raw_lines(), first.raw_lines()); + assert_eq!( + requests.try_recv().is_ok(), + motion_mode == MotionMode::Animated + ); + if motion_mode == MotionMode::Reduced { + assert_eq!( + shifted.display_lines(/*width*/ 16), + first.inner.display_lines(/*width*/ 16) + ); + } + } +} + +#[test] +fn retained_words_stay_visible_while_the_new_window_tail_flips() { + let mut first = cell("OLD GATE", MotionMode::Animated); + for arrival in &mut first.tile_arrivals { + *arrival -= ANIMATION_DURATION; + } + let mut shifted = SplitFlapTranscriptCell::new( + plain_transcript, + "user", + "GATE 73", + Some(&first), + /*discarded_prefix_bytes*/ 4, + MotionMode::Animated, + FrameRequester::test_dummy(), + ); + assert_eq!(shifted.phase_started_at, first.phase_started_at); + // Fix the shared phase for deterministic frames without changing tile arrivals. + shifted.phase_started_at = shifted.started_at; + let frames = [0, 180, 285, 675] + .into_iter() + .map(|milliseconds| format!("{milliseconds:>3}ms {}", frame(&shifted, milliseconds))) + .collect::>() + .join("\n"); + + insta::assert_snapshot!(frames, @r" + 0ms › GATE E GAT + 180ms › GATE 73 GAT + 285ms › GATE 73 TEG + 675ms › GATE 73 + "); +} + +#[test] +fn sliding_uses_rendered_glyphs_instead_of_raw_byte_offsets() { + let markdown = |text: &str| -> Box { + Box::new(crate::history_cell::AgentMarkdownCell::new_spoken( + text.to_string(), + std::path::Path::new("."), + )) + }; + for (render, text, discarded_bytes, discarded_tiles) in [ + ( + plain_transcript as fn(&str) -> Box, + "éGA 東京B", + 4, + 2, + ), + (markdown, "DROP [GATE](https://example.test) **73**", 5, 4), + ] { + let first = SplitFlapTranscriptCell::new( + render, + "user", + text, + /*previous*/ None, + /*discarded_prefix_bytes*/ 0, + MotionMode::Animated, + FrameRequester::test_dummy(), + ); + let target = format!("{} 9", &text[discarded_bytes..]); + let shifted = SplitFlapTranscriptCell::new( + render, + "user", + &target, + Some(&first), + discarded_bytes, + MotionMode::Animated, + FrameRequester::test_dummy(), + ); + let retained_tiles = first.tile_arrivals.len() - discarded_tiles; + + assert_eq!( + shifted.tile_arrivals[..retained_tiles], + first.tile_arrivals[discarded_tiles..] + ); + assert_eq!(shifted.raw_lines(), render(&target).raw_lines()); + } +} + +#[test] +fn replaced_or_changed_speaker_windows_start_fresh() { + let first = cell("éGATE", MotionMode::Animated); + for (role, target, discarded_bytes) in [ + ("user", "éGATE", 6), + ("user", "éGATE", 12), + ("assistant", "GATE 73", 2), + ("user", "OTHER", 2), + ("user", "GATE", 1), + ] { + let replaced = SplitFlapTranscriptCell::new( + plain_transcript, + role, + target, + Some(&first), + discarded_bytes, + MotionMode::Animated, + FrameRequester::test_dummy(), + ); + + assert_eq!(replaced.phase_started_at, replaced.started_at); + assert!( + replaced + .tile_arrivals + .iter() + .all(|arrival| *arrival >= replaced.started_at) + ); + } +} + #[test] fn unicode_graphemes_and_terminal_width_are_preserved() { let text = "a\u{301} 東京 👩‍💻 B"; @@ -192,10 +349,11 @@ fn unicode_graphemes_and_terminal_width_are_preserved() { fn settled_split_flap_stops_scheduling_animation_frames() { let (frame_requester, mut requests) = FrameRequester::test_channel(); let mut cell = SplitFlapTranscriptCell::new( - Box::new(PlainTranscriptCell("GATE".to_string())), + plain_transcript, "user", "GATE", /*previous*/ None, + /*discarded_prefix_bytes*/ 0, MotionMode::Animated, frame_requester, );