Render completed assistant messages directly during replay (#42325)

## Why

Buffered thread snapshots can contain both streaming deltas and the completed
assistant item. Replaying both reconstructs an already-finished stream instead
of restoring its finalized transcript entry.

## What changed

- Omit superseded assistant deltas from replay snapshots when a matching
  completed item follows them.
- Preserve unfinished or unrelated deltas and treat other events as replay
  barriers so text needed before tools or prompts is not dropped.
- Render completed replay items directly as formatted history cells while
  preserving message order, separators, and composer drafts.

## Testing

Added replay tests covering completed, unfinished, unrelated, and
barrier-separated deltas, plus deterministic Markdown rendering and draft
preservation.

GitOrigin-RevId: 2870b09a04b32c0a4c0e9ab76970a8ebc3f5a7d4
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-02 18:36:53 +00:00
1 parent 637c3227b3
commit e6ff749506
8 files changed
+246 -15

No files matched your search

@@ -0,0 +1,39 @@
//! Completed replay messages preserve order, formatting, and composer state.
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn completed_replay_preserves_messages_and_draft_across_reconstruction() {
let mut outputs = Vec::new();
for _ in 0..2 {
let (mut chat, mut rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
chat.note_rendered_width(/*width*/ 80);
chat.handle_paste("Unsent draft".to_string());
replay_user_message_text(&mut chat, "user", "Question", ReplayKind::ThreadSnapshot);
replay_agent_message(
&mut chat,
"first",
"First **answer**",
ReplayKind::ThreadSnapshot,
);
replay_agent_message(
&mut chat,
"second",
"Second `answer`",
ReplayKind::ThreadSnapshot,
);
let cells = drain_insert_history(&mut rx);
outputs.push(cells.into_iter().flatten().collect::<Vec<_>>());
assert_eq!(chat.composer_text_with_pending(), "Unsent draft");
}
assert_eq!(outputs[0], outputs[1]);
insta::assert_snapshot!(outputs[0].iter().map(ToString::to_string).collect::<Vec<_>>().join("\n").trim(), @"
› Question
• First answer
• Second answer
");
}