Limit inline diff previews in the TUI (#41143)

## Why

Large or minified diffs can require substantial work to allocate wrapped rows and syntax-highlighted spans in the inline patch view.

## What changed

- Limit inline patch previews to 12 rendered rows across all changed files, while retaining file summaries and directing users to the transcript when content is omitted.
- Preserve complete diff content in transcript and raw views.
- Bound preview source scanning to 64 KiB so zero-width content cannot bypass the row budget.

## Testing

Add coverage for added, deleted, updated, renamed, minified, narrow-width, wide-character, combining-character, and multi-file diffs, including preservation of complete transcript and raw output.

GitOrigin-RevId: 1899129ab548771c29d226ff624549573fb2f688
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-08-27 14:09:15 +00:00
1 parent 694edc23b2
commit a847c71a15
4 files changed
+488 -16

No files matched your search

+5
View File
@@ -1,6 +1,7 @@
//! Patch summaries and image-tool transcript helpers.
use super::*;
use crate::diff_render::create_diff_preview;
use codex_utils_path_uri::LegacyAppPathString;
#[derive(Debug)]
@@ -11,6 +12,10 @@ pub(crate) struct PatchHistoryCell {
impl HistoryCell for PatchHistoryCell {
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
create_diff_preview(&self.changes, &self.cwd, width as usize)
}
fn transcript_lines(&self, width: u16) -> Vec<Line<'static>> {
create_diff_summary(&self.changes, &self.cwd, width as usize)
}
+238
View File
@@ -49,6 +49,244 @@ fn test_cwd() -> PathBuf {
std::env::temp_dir()
}
#[test]
fn patch_preview_shows_minified_prefix_but_preserves_full_views() {
let changes = HashMap::from([(
PathBuf::from("metrics.json"),
FileChange::Add {
content: format!("[{}0]", "123,".repeat(/*n*/ 10_000)),
},
)]);
let cwd = test_cwd();
let cell = new_patch_event(changes.clone(), &cwd);
insta::assert_snapshot!(render_lines(&cell.display_lines(/*width*/ 80)).join("\n"), @"
• Added metrics.json (+1 -0)
1 +[123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
… Diff preview limited (ctrl + t to view transcript).
");
assert_eq!(
cell.transcript_lines(/*width*/ 80),
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80),
);
assert_eq!(
cell.raw_lines(),
plain_lines(create_diff_summary(&changes, &cwd, RAW_DIFF_SUMMARY_WIDTH)),
);
}
#[test]
fn patch_preview_limits_add_delete_and_update_content() {
for content in [
"value\n".repeat(/*n*/ 20),
format!("short\n{}\ntail\n", "界123\t".repeat(/*n*/ 100)),
] {
for change in [
FileChange::Add {
content: content.clone(),
},
FileChange::Delete {
content: content.clone(),
},
FileChange::Update {
unified_diff: diffy::create_patch("", &content).to_string(),
move_path: Some(PathBuf::from("renamed.txt")),
},
FileChange::Update {
unified_diff: diffy::create_patch(&content, "").to_string(),
move_path: None,
},
FileChange::Update {
unified_diff: diffy::DiffOptions::new()
.set_context_len(/*context_len*/ 20)
.create_patch(&content, &format!("{content}new\n"))
.to_string(),
move_path: None,
},
] {
let changes = HashMap::from([(PathBuf::from("values.txt"), change)]);
let cwd = test_cwd();
let cell = new_patch_event(changes.clone(), &cwd);
let full = create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80);
let mut expected = full[..13].to_vec();
expected.push(
" … Diff preview limited (ctrl + t to view transcript)."
.dim()
.into(),
);
assert_eq!(cell.display_lines(/*width*/ 80), expected);
assert_eq!(cell.transcript_lines(/*width*/ 80), full);
}
}
}
#[test]
fn patch_preview_shares_content_budget_and_keeps_file_summaries() {
let changes = HashMap::from([
(
PathBuf::from("a.txt"),
FileChange::Add {
content: "a\n".repeat(/*n*/ 11) + &"©️ ".repeat(/*n*/ 36),
},
),
(
PathBuf::from("b.txt"),
FileChange::Add {
content: "b\n".repeat(/*n*/ 10),
},
),
(
PathBuf::from("c.txt"),
FileChange::Add {
content: "c\n".into(),
},
),
]);
let cell = new_patch_event(changes, &test_cwd());
insta::assert_snapshot!(render_lines(&cell.display_lines(/*width*/ 80)).join("\n"), @"
• Edited 3 files (+23 -0)
└ a.txt (+12 -0)
1 +a
2 +a
3 +a
4 +a
5 +a
6 +a
7 +a
8 +a
9 +a
10 +a
11 +a
12 +©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️
└ b.txt (+10 -0)
└ c.txt (+1 -0)
… Diff preview limited (ctrl + t to view transcript).
");
}
#[test]
fn patch_preview_budgets_wrapped_rows_at_narrow_widths() {
for content in [
"0123456789".repeat(/*n*/ 1_000),
"ガ".repeat(/*n*/ 1_000),
"0123456789\n".repeat(/*n*/ 20),
format!("{}\n", "界\tvalue".repeat(/*n*/ 100)).repeat(/*n*/ 20),
format!("{}\n", "\u{a9}\u{fe0f}".repeat(/*n*/ 50)).repeat(/*n*/ 20),
] {
let changes = HashMap::from([(PathBuf::from("values.txt"), FileChange::Add { content })]);
let cell = new_patch_event(changes, &test_cwd());
for width in [8, 16, 40, 80] {
let lines = cell.display_lines(width);
let body = lines[1..lines.len() - 1].to_vec();
let rows = Paragraph::new(body)
.wrap(Wrap { trim: false })
.line_count(width);
assert_eq!(rows, 12);
}
}
}
#[test]
fn patch_preview_preserves_small_diffs_and_exact_budget() {
for count in [1, 12] {
let mut changes = HashMap::from([(
PathBuf::from("small.rs"),
FileChange::Add {
content: "let value = 1;\n".repeat(count),
},
)]);
changes.extend([
(
PathBuf::from("z-empty"),
FileChange::Add {
content: String::new(),
},
),
(
PathBuf::from("z-renamed"),
FileChange::Update {
unified_diff: String::new(),
move_path: Some(PathBuf::from("renamed")),
},
),
]);
let cwd = test_cwd();
let cell = new_patch_event(changes.clone(), &cwd);
assert_eq!(
cell.display_lines(/*width*/ 80),
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80),
);
}
}
#[test]
fn patch_preview_caps_zero_width_source_before_later_content() {
let make_changes: [fn(String) -> FileChange; 3] = [
|content| FileChange::Add { content },
|content| FileChange::Delete { content },
|content| FileChange::Update {
unified_diff: diffy::create_patch("", &content).to_string(),
move_path: None,
},
];
let cwd = test_cwd();
let content = format!("head\nxy{}\ntail\n", "\u{301}".repeat(/*n*/ 40_000));
let mut summaries = Vec::new();
for make_change in make_changes {
let changes = HashMap::from([
(PathBuf::from("a.txt"), make_change(content.clone())),
(PathBuf::from("b.txt"), make_change("later\n".into())),
]);
let cell = new_patch_event(changes.clone(), &cwd);
let preview = render_lines(&cell.display_lines(/*width*/ 80)).join("\n");
// The next two-byte combining mark straddles the 64 KiB boundary.
assert_eq!(preview.matches('\u{301}').count(), 32_764);
summaries.push(preview.replace('\u{301}', ""));
assert_eq!(
cell.transcript_lines(/*width*/ 80),
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80)
);
}
insta::assert_snapshot!(summaries.join("\n\n"), @"
• Edited 2 files (+4 -0)
└ a.txt (+3 -0)
1 +head
2 +xy
└ b.txt (+1 -0)
… Diff preview limited (ctrl + t to view transcript).
• Edited 2 files (+0 -4)
└ a.txt (+0 -3)
1 -head
2 -xy
└ b.txt (+0 -1)
… Diff preview limited (ctrl + t to view transcript).
• Edited 2 files (+4 -0)
└ a.txt (+3 -0)
1 +head
2 +xy
└ b.txt (+1 -0)
… Diff preview limited (ctrl + t to view transcript).
");
}
#[test]
fn streaming_agent_tail_blank_line_uses_one_viewport_row() {
let cell = StreamingAgentTailCell::new(