Make transcript list spacing uniform after streaming (#46899)

## Why

Adding blank lines only after multiline items gives lists uneven spacing. Completed lists in redrawable transcript views can use consistent spacing based on their rendered width.

## What changed

- Keep assistant and plan lists compact while streaming in the fullscreen transcript, without holding back completed list rows.
- On completion or interruption, separate all sibling items if any item spans multiple rows; keep single-line lists compact and recompute spacing on resize.
- Preserve the reader's displayed content when consolidating streamed cells, and include spacing policy in the Markdown render cache key.
- Retain existing list spacing for native terminal scrollback.

## Testing

Add regression and snapshot coverage for streaming completion and interruption, width-dependent reflow, reader position, nested and quoted lists, item paragraphs, and preservation of links and source metadata.

GitOrigin-RevId: b2230aaf76ae798ebb6bf15563f8b8dcb6f3d19d
This commit is contained in:
Felipe Coury
2026-09-20 22:55:19 +00:00
committed by copyberry
parent d99213289f
commit 4ca8e7afff
25 changed files with 789 additions and 68 deletions
+25 -7
View File
@@ -44,7 +44,8 @@ use crate::history_cell::HistoryCell;
use crate::history_cell::HistoryRenderMode;
use crate::history_cell::{self};
use crate::inline_visualization::InlineVisualizationContext;
use crate::markdown::render_markdown_agent_with_links_cwd_and_visualizations;
use crate::markdown::render_markdown_agent_with_list_spacing;
use crate::markdown_render::ListSpacing;
use crate::style::proposed_plan_style;
use crate::terminal_hyperlinks::HyperlinkLine;
use crate::terminal_hyperlinks::prefix_hyperlink_lines;
@@ -59,7 +60,9 @@ use super::StreamState;
use super::prose_preview::PreviewMode;
use super::prose_preview::ProsePreview;
use super::render::StreamingRender;
#[cfg(test)]
use super::render::render_source;
use super::render::render_source_with_list_spacing;
use super::table_holdback::TableHoldbackScanner;
use super::table_holdback::TableHoldbackState;
#[cfg(test)]
@@ -200,17 +203,18 @@ impl StreamCore {
///
/// This intentionally re-renders from the full raw source instead of
/// trying to stitch together queued stable lines and the current tail. The
/// final render is the canonical transcript representation used for
/// consolidation, so callers that skip `reset()` can accidentally replay a
/// finished stream into the next answer.
/// remaining rows keep the stream's spacing so emitted offsets stay valid. Consolidation
/// renders the returned source with the completed cell's spacing policy. Callers that skip
/// `reset()` can accidentally replay a finished stream into the next answer.
fn finalize_remaining(&mut self) -> (Vec<HyperlinkLine>, String) {
let source = self.state.collector.finalize_and_take_source();
let mut rendered = render_source(
let mut rendered = render_source_with_list_spacing(
&source,
self.width,
self.cwd.as_path(),
self.render_mode,
self.inline_visualization_context.as_ref(),
self.render.list_spacing,
);
let remaining = rendered.split_off(self.emitted_stable_len.min(rendered.len()));
(remaining, source)
@@ -383,12 +387,13 @@ impl StreamCore {
if let Some(start) = previous_tail_start.or(self.active_tail_source_start(self.render_mode))
{
let prefix_len = |width, mode| {
render_source(
render_source_with_list_spacing(
&source[..start],
width,
self.cwd.as_path(),
mode,
self.inline_visualization_context.as_ref(),
self.render.list_spacing,
)
.len()
};
@@ -539,11 +544,12 @@ impl StreamCore {
let render_start = Instant::now();
let source = self.state.collector.committed_source();
let stable_prefix_render = render_markdown_agent_with_links_cwd_and_visualizations(
let stable_prefix_render = render_markdown_agent_with_list_spacing(
&source[..source_start.min(source.len())],
self.width,
Some(self.cwd.as_path()),
self.inline_visualization_context.as_ref(),
self.render.list_spacing,
);
let stable_prefix_len = stable_prefix_render.len();
tracing::trace!(
@@ -571,6 +577,12 @@ pub(crate) struct StreamController {
}
impl StreamController {
/// Select spacing before the first delta; final source-backed cells choose their own layout.
pub(crate) fn with_list_spacing(mut self, list_spacing: ListSpacing) -> Self {
self.core.render.list_spacing = list_spacing;
self
}
/// Create a controller whose markdown renderer shortens local file links relative to `cwd`.
///
/// `width` is the content width available to markdown rendering, not necessarily the full
@@ -697,6 +709,12 @@ pub(crate) struct PlanStreamController {
}
impl PlanStreamController {
/// Select spacing before the first delta; no list-specific holdback is needed.
pub(crate) fn with_list_spacing(mut self, list_spacing: ListSpacing) -> Self {
self.core.render.list_spacing = list_spacing;
self
}
/// Create a plan-stream controller whose markdown renderer shortens local file links relative
/// to `cwd`.
///
@@ -27,8 +27,13 @@ fn unicode_math_inline_stream_preserves_display_exclusion_boundaries() {
);
assert_eq!(
render.lines,
render_streaming_markdown_agent_with_links_and_cwd(&source, Some(40), Some(&cwd))
.lines
render_streaming_markdown_agent_with_links_and_cwd(
&source,
Some(40),
Some(&cwd),
crate::markdown_render::ListSpacing::AfterMultiline
)
.lines
);
for width in [16, 40] {
let mut resized = StreamingRender::new();
@@ -44,7 +49,8 @@ fn unicode_math_inline_stream_preserves_display_exclusion_boundaries() {
render_streaming_markdown_agent_with_links_and_cwd(
&source,
Some(width),
Some(&cwd)
Some(&cwd),
crate::markdown_render::ListSpacing::AfterMultiline,
)
.lines
);
+1
View File
@@ -168,6 +168,7 @@ fn unicode_math_rejected_display_keeps_its_closer_and_following_text() {
&source,
Some(40),
Some(&cwd),
crate::markdown_render::ListSpacing::AfterMultiline,
);
assert_eq!(
(&render.lines, render.pending_math_start),
+41 -8
View File
@@ -8,8 +8,9 @@ use crate::history_cell::HistoryRenderMode;
use crate::history_cell::raw_lines_from_source;
use crate::inline_visualization::InlineVisualizationContext;
use crate::inline_visualization::contains_inline_visualization;
use crate::markdown::render_markdown_agent_with_links_cwd_and_visualizations;
use crate::markdown::render_markdown_agent_with_list_spacing;
use crate::markdown::render_streaming_markdown_agent_with_links_and_cwd;
use crate::markdown_render::ListSpacing;
use crate::render::highlight::syntax_theme_revision;
use crate::terminal_hyperlinks::HyperlinkLine;
use crate::terminal_hyperlinks::plain_hyperlink_lines;
@@ -21,6 +22,7 @@ use std::path::Path;
/// The prefix before both boundaries is immutable; only the final top-level Markdown block is
/// re-rendered as committed source arrives.
pub(super) struct StreamingRender {
pub(super) list_spacing: ListSpacing,
pub(super) lines: Vec<HyperlinkLine>,
pub(super) pending_math_start: Option<usize>,
pub(super) mermaid_start: Option<usize>,
@@ -39,6 +41,7 @@ pub(super) struct StreamingRender {
impl StreamingRender {
pub(super) fn new() -> Self {
Self {
list_spacing: ListSpacing::AfterMultiline,
lines: Vec::with_capacity(64),
pending_math_start: None,
mermaid_start: None,
@@ -79,8 +82,12 @@ impl StreamingRender {
self.has_inline_visualization_directive = contains_inline_visualization(source);
self.lines = match (render_mode, inline_visualization_context) {
(HistoryRenderMode::Rich, None) if !self.has_inline_visualization_directive => {
let rendered =
render_streaming_markdown_agent_with_links_and_cwd(source, width, Some(cwd));
let rendered = render_streaming_markdown_agent_with_links_and_cwd(
source,
width,
Some(cwd),
self.list_spacing,
);
self.has_reference_link_definition = rendered.has_reference_link_definition;
self.pending_math_start = rendered.pending_math_start;
self.mermaid_start = rendered.mermaid_start;
@@ -93,16 +100,18 @@ impl StreamingRender {
source,
width,
Some(cwd),
self.list_spacing,
);
self.pending_math_start = rendered.pending_math_start;
self.mermaid_start = rendered.mermaid_start;
}
render_source(
render_source_with_list_spacing(
source,
width,
cwd,
render_mode,
inline_visualization_context,
self.list_spacing,
)
}
};
@@ -166,8 +175,12 @@ impl StreamingRender {
let pending_source = &raw_source[self.stable_source_len..];
let theme_revision = syntax_theme_revision();
let pending =
render_streaming_markdown_agent_with_links_and_cwd(pending_source, width, Some(cwd));
let pending = render_streaming_markdown_agent_with_links_and_cwd(
pending_source,
width,
Some(cwd),
self.list_spacing,
);
self.pending_math_start = pending
.pending_math_start
.map(|start| self.stable_source_len + start);
@@ -196,12 +209,13 @@ impl StreamingRender {
let mut newly_stable_rendered_len = None;
if let Some(boundary) = pending.last_top_level_block_start {
let newly_stable_source = &pending_source[..boundary];
let newly_stable = render_source(
let newly_stable = render_source_with_list_spacing(
newly_stable_source,
width,
cwd,
render_mode,
inline_visualization_context,
self.list_spacing,
);
self.stable_source_len += boundary;
newly_stable_rendered_len = Some(newly_stable.len());
@@ -228,13 +242,32 @@ pub(super) fn render_source(
cwd: &Path,
render_mode: HistoryRenderMode,
inline_visualization_context: Option<&InlineVisualizationContext>,
) -> Vec<HyperlinkLine> {
render_source_with_list_spacing(
source,
width,
cwd,
render_mode,
inline_visualization_context,
ListSpacing::AfterMultiline,
)
}
pub(super) fn render_source_with_list_spacing(
source: &str,
width: Option<usize>,
cwd: &Path,
render_mode: HistoryRenderMode,
inline_visualization_context: Option<&InlineVisualizationContext>,
list_spacing: ListSpacing,
) -> Vec<HyperlinkLine> {
match render_mode {
HistoryRenderMode::Rich => render_markdown_agent_with_links_cwd_and_visualizations(
HistoryRenderMode::Rich => render_markdown_agent_with_list_spacing(
source,
width,
Some(cwd),
inline_visualization_context,
list_spacing,
),
HistoryRenderMode::Raw => plain_hyperlink_lines(raw_lines_from_source(source)),
}
@@ -168,6 +168,7 @@ fn growing_single_top_level_blocks_render_and_scan_in_one_pass() {
&source,
width,
Some(cwd.as_path()),
crate::markdown_render::ListSpacing::AfterMultiline,
);
assert_eq!(pending.last_top_level_block_start, None);
assert_eq!(render.lines, pending.lines);
@@ -200,6 +201,59 @@ fn incremental_raw_render_preserves_blank_lines() {
);
}
#[test]
fn compact_list_spacing_keeps_incremental_and_full_renders_aligned() {
use crate::markdown_render::ListSpacing;
let cwd = test_cwd();
for mode in [HistoryRenderMode::Rich, HistoryRenderMode::Raw] {
let mut render = StreamingRender::new();
render.list_spacing = ListSpacing::Compact;
let mut source = String::new();
for chunk in [
"- Short\n",
"- A sibling with enough words to wrap across rows\n",
"- Last\n",
"\nAfter the list.\n\n",
"| Name | Value |\n| --- | --- |\n| A | B |\n",
"\n- Nested\n - One\n - Two\n- End\n",
] {
append(&mut render, &mut source, chunk, Some(24), &cwd, mode);
assert_eq!(
render.lines,
super::render_source_with_list_spacing(
&source,
Some(24),
&cwd,
mode,
/*inline_visualization_context*/ None,
ListSpacing::Compact,
)
);
}
for width in [80, 24] {
render.recompute(
&source,
Some(width),
&cwd,
mode,
/*inline_visualization_context*/ None,
);
assert_eq!(
render.lines,
super::render_source_with_list_spacing(
&source,
Some(width),
&cwd,
mode,
/*inline_visualization_context*/ None,
ListSpacing::Compact,
)
);
}
}
}
#[test]
fn inline_visualization_context_without_directives_keeps_stable_prefix() {
let cwd = test_cwd();