Keep unfinished link destinations out of rich streaming previews (#47589)

## Why

An unfinished Markdown link can display its destination across multiple preview rows, then collapse when the closing parenthesis arrives.

## What changed

Keep the link label visible while withholding the opening parenthesis and destination until the link closes. Scan incoming text incrementally, accounting for nested parentheses, escapes, angle-bracket destinations, titles, and inline code. Apply this holdback only to rich prose previews; newline commitment and finalization still render the original source.

## Testing

Add regression tests for character-by-character input, long Unicode destinations, resizing, raw-mode switching, and flushing unfinished links on newline or completion, plus a snapshot of the label-only preview.

GitOrigin-RevId: 9cea680a93dfcd0aea21f0fc12006b27400d4dab
This commit is contained in:
Eric Traut
2026-09-23 16:39:22 +00:00
committed by copyberry
parent 7342991f0c
commit e0a64cf2bc
3 changed files with 267 additions and 1 deletions
@@ -2,6 +2,164 @@ use super::*;
use crate::terminal_hyperlinks::visible_lines;
use pretty_assertions::assert_eq;
#[test]
fn unfinished_link_destinations_do_not_expand_the_preview() {
let cwd = std::env::temp_dir();
for destination in [
"https://example.com/a/very/long/path/to/the/guide",
"https://example.com/a_(nested_(path))",
r"https://example.com/escaped\)parenthesis",
"<https://example.com/unbalanced)parenthesis>",
" <https://example.com/unbalanced)parenthesis>",
"https://example.com/guide \"a title with ) parentheses\"",
"https://example.com/guide (x < y)",
] {
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
controller.push("See [documentation]");
let label = controller.current_tail_lines();
// Split at every character, including the opening delimiter and escapes.
for character in format!("({destination}").chars() {
controller.push(&character.to_string());
assert_eq!(controller.current_tail_lines(), label);
}
controller.push(") and continue.");
assert_eq!(
controller.current_tail_lines(),
render_source(
&format!("See [documentation]({destination}) and continue."),
Some(24),
&cwd,
HistoryRenderMode::Rich,
/*inline_visualization_context*/ None,
),
);
}
// The preview's byte limit must not drop the opener and expose a long URL tail.
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
controller.push("See [documentation]");
let label = controller.current_tail_lines();
controller.push(&format!("(https://example.com/{}", "界".repeat(3000)));
controller.push("more-path");
assert_eq!(controller.current_tail_lines(), label);
}
#[test]
fn unfinished_links_flush_on_newline_or_completion() {
let cwd = std::env::temp_dir();
let source = "See [documentation](https://example.com/a/very/long/path";
for ending in ["", "\n"] {
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
controller.push(source);
insta::assert_debug_snapshot!(
"unfinished_link_preview",
visible_lines(controller.current_tail_lines()),
);
controller.set_width(Some(32));
assert_eq!(
visible_lines(controller.current_tail_lines()),
vec![Line::from("See [documentation]")],
);
controller.push(ending);
let (queued, _) = controller.on_commit_tick_batch(usize::MAX);
let (remaining, finalized_source) = controller.finalize();
let displayed = queued
.into_iter()
.chain(remaining)
.flat_map(|cell| cell.display_lines(/*width*/ 34))
.collect::<Vec<_>>();
let expected = history_cell::AgentMessageCell::new_hyperlink_lines(
render_source(
source,
Some(32),
&cwd,
HistoryRenderMode::Rich,
/*inline_visualization_context*/ None,
),
/*is_first_line*/ true,
);
assert_eq!(displayed, expected.display_lines(/*width*/ 34));
assert_eq!(finalized_source, Some(format!("{source}\n")));
}
}
#[test]
fn link_preview_respects_code_escapes_and_raw_mode() {
let cwd = std::env::temp_dir();
for source in [
r"See \[label](https://example.com/unfinished",
r"See [label\](https://example.com/unfinished",
"See `[label](https://example.com/unfinished`",
"See ``[la`bel](https://example.com/unfinished``",
"See [label](https://example.com/complete) and prose",
] {
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
for character in source.chars() {
controller.push(&character.to_string());
}
assert_eq!(
controller.current_tail_lines(),
render_source(
source,
Some(24),
&cwd,
HistoryRenderMode::Rich,
/*inline_visualization_context*/ None,
),
"{source}",
);
}
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
controller.push("See `[code](literal` then [label](https://example.com/unfinished");
assert_eq!(
controller.current_tail_lines(),
render_source(
"See `[code](literal` then [label]",
Some(24),
&cwd,
HistoryRenderMode::Rich,
/*inline_visualization_context*/ None,
),
);
let source = "See [label](https://example.com/unfinished";
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Raw);
controller.push(source);
assert_eq!(
controller.current_tail_lines(),
render_source(
source,
Some(24),
&cwd,
HistoryRenderMode::Raw,
/*inline_visualization_context*/ None,
),
);
}
#[test]
fn link_holdback_stays_rich_only_when_a_pipe_freezes_the_preview() {
let cwd = std::env::temp_dir();
let source = "See [label](https://example.com";
let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
controller.push(source);
let label = controller.current_tail_lines();
controller.push("|suffix)");
assert_eq!(controller.current_tail_lines(), label);
controller.set_render_mode(HistoryRenderMode::Raw);
assert_eq!(
controller.current_tail_lines(),
render_source(
source,
Some(40),
&cwd,
HistoryRenderMode::Raw,
/*inline_visualization_context*/ None,
),
);
controller.set_render_mode(HistoryRenderMode::Rich);
assert_eq!(controller.current_tail_lines(), label);
}
#[test]
fn source_only_changes_refresh_the_preview_and_active_tail() {
let cwd = std::env::temp_dir();