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

@@ -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 {
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::<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]
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,
);