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

+18
View File
@@ -6,6 +6,24 @@
use super::*;
impl ChatWidget {
/// Flush prior activity and preserve its separator before live or replayed assistant text.
pub(super) fn prepare_assistant_message(&mut self) {
// Before starting an agent stream, flush any active exec cell group.
self.flush_unified_exec_wait_streak();
self.flush_active_cell();
// If the previous turn inserted non-stream history (exec output, patch status, MCP
// calls), render a separator before starting the next streamed assistant message.
if self.transcript.needs_final_message_separator && self.transcript.had_work_activity {
self.add_to_history(history_cell::FinalMessageSeparator::new(
/*elapsed_seconds*/ None, /*runtime_metrics*/ None,
));
self.transcript.needs_final_message_separator = false;
} else if self.transcript.needs_final_message_separator {
// Reset the flag even if we don't show separator (no work was done)
self.transcript.needs_final_message_separator = false;
}
}
/// Replay a subset of initial events into the UI to seed the transcript when
/// resuming an existing session. This approximates the live event flow and
/// is intentionally conservative: only safe-to-replay items are rendered to
+23 -15
View File
@@ -330,7 +330,28 @@ impl ChatWidget {
}
}
let parsed = parse_assistant_markdown(&message, self.config.cwd.as_path());
self.finalize_completed_assistant_message(Some(parsed.visible_markdown.as_str()));
if from_replay && self.stream_controller.is_none() && !parsed.visible_markdown.is_empty() {
self.prepare_assistant_message();
self.mark_safety_buffering_agent_message_started();
self.bottom_pane.hide_status_indicator();
let context = self.thread_id.and_then(|thread_id| {
crate::inline_visualization::InlineVisualizationContext::from_config(
&self.config,
thread_id,
)
});
self.add_to_history(
history_cell::AgentMarkdownCell::new_with_inline_visualizations(
parsed.visible_markdown.clone(),
self.config.cwd.as_path(),
context,
),
);
self.handle_stream_finished();
self.request_redraw();
} else {
self.finalize_completed_assistant_message(Some(parsed.visible_markdown.as_str()));
}
if matches!(item.phase, Some(MessagePhase::FinalAnswer) | None)
&& !parsed.visible_markdown.is_empty()
{
@@ -453,20 +474,7 @@ impl ChatWidget {
self.mark_safety_buffering_agent_message_started();
}
if self.stream_controller.is_none() {
// Before starting an agent stream, flush any active exec cell group.
self.flush_unified_exec_wait_streak();
self.flush_active_cell();
// If the previous turn inserted non-stream history (exec output, patch status, MCP
// calls), render a separator before starting the next streamed assistant message.
if self.transcript.needs_final_message_separator && self.transcript.had_work_activity {
self.add_to_history(history_cell::FinalMessageSeparator::new(
/*elapsed_seconds*/ None, /*runtime_metrics*/ None,
));
self.transcript.needs_final_message_separator = false;
} else if self.transcript.needs_final_message_separator {
// Reset the flag even if we don't show separator (no work was done)
self.transcript.needs_final_message_separator = false;
}
self.prepare_assistant_message();
let inline_visualization_context = self.thread_id.and_then(|thread_id| {
crate::inline_visualization::InlineVisualizationContext::from_config(
&self.config,
+2
View File
@@ -258,6 +258,8 @@ mod plugin_catalog;
mod popups_and_settings;
#[path = "tests/rate_limit_recovery_tests.rs"]
mod rate_limit_recovery_tests;
#[path = "tests/replay_render_tests.rs"]
mod replay_render_tests;
mod review_mode;
mod side;
mod slash_commands;
@@ -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
");
}