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
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-08 04:39:31 +00:00
1 parent 49a9d78999
commit 6b6fdc3572
3 files changed
+193 -15

No files matched your search

+4 -3
View File
@@ -1233,6 +1233,7 @@ impl ChatWidget {
self.realtime_conversation.transcript_role = Some(role); self.realtime_conversation.transcript_role = Some(role);
} }
self.realtime_conversation.transcript.push_str(&delta); self.realtime_conversation.transcript.push_str(&delta);
let mut discarded_prefix_bytes = 0;
if self.realtime_conversation.transcript.len() > MAX_TRANSCRIPT_BYTES { if self.realtime_conversation.transcript.len() > MAX_TRANSCRIPT_BYTES {
let mut start = self.realtime_conversation.transcript.len() - MAX_TRANSCRIPT_BYTES; let mut start = self.realtime_conversation.transcript.len() - MAX_TRANSCRIPT_BYTES;
while !self while !self
@@ -1243,6 +1244,7 @@ impl ChatWidget {
start += 1; start += 1;
} }
self.realtime_conversation.transcript.drain(..start); self.realtime_conversation.transcript.drain(..start);
discarded_prefix_bytes = start;
} }
let role = self let role = self
.realtime_conversation .realtime_conversation
@@ -1254,13 +1256,12 @@ impl ChatWidget {
.live_transcript_cell .live_transcript_cell
.as_deref() .as_deref()
.and_then(|cell| cell.as_any().downcast_ref::<SplitFlapTranscriptCell>()); .and_then(|cell| cell.as_any().downcast_ref::<SplitFlapTranscriptCell>());
let cell =
self.realtime_transcript_history_cell(role, &self.realtime_conversation.transcript);
let live_cell = SplitFlapTranscriptCell::new( let live_cell = SplitFlapTranscriptCell::new(
cell, |text| self.realtime_transcript_history_cell(role, text),
role, role,
&self.realtime_conversation.transcript, &self.realtime_conversation.transcript,
previous, previous,
discarded_prefix_bytes,
MotionMode::from_animations_enabled(self.config.animations), MotionMode::from_animations_enabled(self.config.animations),
self.frame_requester.clone(), self.frame_requester.clone(),
); );
@@ -50,14 +50,16 @@ pub(super) struct SplitFlapTranscriptCell {
impl SplitFlapTranscriptCell { impl SplitFlapTranscriptCell {
pub(super) fn new( pub(super) fn new(
inner: Box<dyn HistoryCell>, render: impl Fn(&str) -> Box<dyn HistoryCell>,
role: &str, role: &str,
target: &str, target: &str,
previous: Option<&Self>, previous: Option<&Self>,
discarded_prefix_bytes: usize,
motion_mode: MotionMode, motion_mode: MotionMode,
frame_requester: FrameRequester, frame_requester: FrameRequester,
) -> Self { ) -> Self {
let now = Instant::now(); let now = Instant::now();
let inner = render(target);
let visible_target = visible_glyphs(inner.as_ref()); let visible_target = visible_glyphs(inner.as_ref());
let mut flap_sample = visible_target let mut flap_sample = visible_target
.bytes() .bytes()
@@ -69,13 +71,28 @@ impl SplitFlapTranscriptCell {
flap_sample.extend(visible_target.bytes().rev().take(FLAP_SAMPLE_WINDOW)); flap_sample.extend(visible_target.bytes().rev().take(FLAP_SAMPLE_WINDOW));
} }
flap_sample.reverse(); flap_sample.reverse();
let previous = previous.filter(|cell| { let previous = previous.and_then(|cell| {
cell.role == role let retained = cell.target.get(discarded_prefix_bytes..)?;
&& target.starts_with(&cell.target) if cell.role != role || retained.is_empty() || !target.starts_with(retained) {
&& visible_target.starts_with(&cell.visible_target) 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 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(); .unwrap_or_default();
let existing_tiles = tile_arrivals.len(); let existing_tiles = tile_arrivals.len();
for index in existing_tiles..visible_target.len() { for index in existing_tiles..visible_target.len() {
@@ -98,7 +115,9 @@ impl SplitFlapTranscriptCell {
flap_sample, flap_sample,
tile_arrivals, tile_arrivals,
started_at: now, 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, motion_mode,
frame_requester, frame_requester,
}; };
@@ -21,12 +21,17 @@ impl HistoryCell for PlainTranscriptCell {
} }
} }
fn plain_transcript(text: &str) -> Box<dyn HistoryCell> {
Box::new(PlainTranscriptCell(text.to_string()))
}
fn cell(text: &str, motion_mode: MotionMode) -> SplitFlapTranscriptCell { fn cell(text: &str, motion_mode: MotionMode) -> SplitFlapTranscriptCell {
SplitFlapTranscriptCell::new( SplitFlapTranscriptCell::new(
Box::new(PlainTranscriptCell(text.to_string())), plain_transcript,
"user", "user",
text, text,
/*previous*/ None, /*previous*/ None,
/*discarded_prefix_bytes*/ 0,
motion_mode, motion_mode,
FrameRequester::test_dummy(), FrameRequester::test_dummy(),
) )
@@ -108,10 +113,11 @@ fn settled_tiles_briefly_glow_in_the_speakers_color() {
("user", MotionMode::Reduced, None), ("user", MotionMode::Reduced, None),
] { ] {
let cell = SplitFlapTranscriptCell::new( let cell = SplitFlapTranscriptCell::new(
Box::new(PlainTranscriptCell("A".to_string())), plain_transcript,
role, role,
"A", "A",
/*previous*/ None, /*previous*/ None,
/*discarded_prefix_bytes*/ 0,
motion_mode, motion_mode,
FrameRequester::test_dummy(), 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() { fn reduced_motion_keeps_the_original_transcript_and_requests_no_frames() {
let (frame_requester, mut requests) = FrameRequester::test_channel(); let (frame_requester, mut requests) = FrameRequester::test_channel();
let cell = SplitFlapTranscriptCell::new( let cell = SplitFlapTranscriptCell::new(
Box::new(PlainTranscriptCell("GATE 73".to_string())), plain_transcript,
"user", "user",
"GATE 73", "GATE 73",
/*previous*/ None, /*previous*/ None,
/*discarded_prefix_bytes*/ 0,
MotionMode::Reduced, MotionMode::Reduced,
frame_requester, frame_requester,
); );
@@ -160,10 +167,11 @@ fn appended_transcript_keeps_the_previous_words_settled() {
*arrival -= ANIMATION_DURATION; *arrival -= ANIMATION_DURATION;
} }
let appended = SplitFlapTranscriptCell::new( let appended = SplitFlapTranscriptCell::new(
Box::new(PlainTranscriptCell("GATE 73".to_string())), plain_transcript,
"user", "user",
"GATE 73", "GATE 73",
Some(&first), Some(&first),
/*discarded_prefix_bytes*/ 0,
MotionMode::Animated, MotionMode::Animated,
FrameRequester::test_dummy(), FrameRequester::test_dummy(),
); );
@@ -173,6 +181,155 @@ fn appended_transcript_keeps_the_previous_words_settled() {
assert_eq!(appended.tile_arrivals[..4], first.tile_arrivals); 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::<Vec<_>>()
.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<dyn HistoryCell> {
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<dyn HistoryCell>,
"é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] #[test]
fn unicode_graphemes_and_terminal_width_are_preserved() { fn unicode_graphemes_and_terminal_width_are_preserved() {
let text = "a\u{301} 東京 👩‍💻 B"; 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() { fn settled_split_flap_stops_scheduling_animation_frames() {
let (frame_requester, mut requests) = FrameRequester::test_channel(); let (frame_requester, mut requests) = FrameRequester::test_channel();
let mut cell = SplitFlapTranscriptCell::new( let mut cell = SplitFlapTranscriptCell::new(
Box::new(PlainTranscriptCell("GATE".to_string())), plain_transcript,
"user", "user",
"GATE", "GATE",
/*previous*/ None, /*previous*/ None,
/*discarded_prefix_bytes*/ 0,
MotionMode::Animated, MotionMode::Animated,
frame_requester, frame_requester,
); );