Summarise large edit diffs and label the card with file and stat

The chat edit diff card rendered every line of an edit, so the >400-line
fallback flooded the transcript with thousands of Text nodes on a phone;
it also showed only the tool name, not the file, on the expanded panel.

- ChatFormat.toolEditDiff keeps the first MAX_RENDERED_DIFF_LINES (200)
  lines and puts the remainder in the new ToolDiff.omitted, while the
  +N/-M counts continue to describe the whole edit.
- DiffView prints "… N more changed lines not shown" first so the
  truncation is visible without scrolling.
- The expanded panel header now shows the edited file name and the shared
  green/red DiffStat, matching the collapsed chip.
- ChatFormatTest pins omitted = 0 for a small edit and the true counts,
  200-line cap and omitted = 300 for an oversized one.
- android/README.md records the change and its evidence.
This commit is contained in:
2026-10-16 15:15:35 +01:00
parent 291cec2bda
commit e2f306b8bd
4 changed files with 121 additions and 6 deletions
@@ -107,14 +107,28 @@ enum class DiffKind { CONTEXT, ADD, REMOVE }
data class DiffLine(val kind: DiffKind, val text: String)
/** A synthesised edit diff: the file, the `+N -M` counts and the ordered lines. */
data class ToolDiff(val file: String, val added: Int, val removed: Int, val lines: List<DiffLine>)
/**
* A synthesised edit diff: the file, the true `+N -M` counts and the ordered
* lines. [omitted] is how many diff lines are not in [lines] because the edit was
* too large to render in full (0 when the whole diff is shown); the counts always
* describe the whole edit, never just the rendered prefix.
*/
data class ToolDiff(
val file: String,
val added: Int,
val removed: Int,
val lines: List<DiffLine>,
val omitted: Int = 0,
)
/**
* Derives a unified diff for an `edit`-family tool call from its
* `oldString`/`newString` arguments. The brokered protocol carries no diff field
* (research report §2.5), so the +/- view is synthesised here instead. Returns
* null for any other tool, so non-edit calls keep the INPUT/OUTPUT panel.
* (research report §2.5), so the +/- view is synthesised here instead. A diff
* longer than [MAX_RENDERED_DIFF_LINES] is summarised — only that many lines are
* kept, with [ToolDiff.omitted] carrying the rest — so a very large edit cannot
* flood the transcript on a phone. Returns null for any other tool, so non-edit
* calls keep the INPUT/OUTPUT panel.
*/
internal fun toolEditDiff(input: JsonElement?): ToolDiff? {
val obj = input as? JsonObject ?: return null
@@ -126,7 +140,8 @@ internal fun toolEditDiff(input: JsonElement?): ToolDiff? {
file = obj.stringOrNull("filePath", "file_path", "file").orEmpty(),
added = lines.count { it.kind == DiffKind.ADD },
removed = lines.count { it.kind == DiffKind.REMOVE },
lines = lines,
lines = lines.take(MAX_RENDERED_DIFF_LINES),
omitted = (lines.size - MAX_RENDERED_DIFF_LINES).coerceAtLeast(0),
)
}
@@ -177,4 +192,12 @@ private fun lineDiff(old: List<String>, new: List<String>): List<DiffLine> {
return result
}
/**
* How many diff lines are rendered before the remainder is summarised. Small
* enough to stay readable on a phone, large enough that ordinary edits are
* always shown in full. [MAX_DIFF_LINES] is the larger algorithm guard that
* bounds the quadratic LCS table.
*/
internal const val MAX_RENDERED_DIFF_LINES = 200
private const val MAX_DIFF_LINES = 400
@@ -821,12 +821,13 @@ private fun ToolPanel(part: ChatPart, onDismiss: () -> Unit, modifier: Modifier
tint = if (failed) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary,
)
Text(
text = toolLabel(part),
text = diff?.file?.takeIf { it.isNotBlank() } ?: toolLabel(part),
style = MaterialTheme.typography.labelLarge,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
diff?.let { DiffStat(it) }
toolDurationMillis(part)?.let {
Text(formatToolDuration(it), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
@@ -887,6 +888,18 @@ private fun DiffView(diff: ToolDiff) {
.verticalScroll(rememberScrollState())
.padding(horizontal = CHAT_GUTTER, vertical = CHAT_ITEM_GAP),
) {
if (diff.omitted > 0) {
// A very large edit is summarised rather than rendered in full, so
// the card stays short and readable on a phone. The note sits first
// so the truncation is visible without scrolling to the end.
Text(
text = "… ${diff.omitted} more changed " +
(if (diff.omitted == 1) "line" else "lines") + " not shown",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.padding(bottom = CHAT_ITEM_GAP),
)
}
diff.lines.forEach { line ->
val (symbol, color) = when (line.kind) {
DiffKind.ADD -> "+" to added
@@ -87,6 +87,8 @@ class ChatFormatTest {
assertEquals("/tmp/a.txt", diff.file)
assertEquals(2, diff.added)
assertEquals(1, diff.removed)
// A small edit is shown whole, nothing summarised.
assertEquals(0, diff.omitted)
assertEquals(
listOf(
DiffLine(DiffKind.CONTEXT, "one"),
@@ -99,6 +101,30 @@ class ChatFormatTest {
)
}
@Test
fun toolEditDiffSummarisesAnOversizedEditInsteadOfRenderingEveryLine() {
// Wholly disjoint old/new blocks: every line is a change, so the full diff
// is twice the block size — far past what a phone should render.
val changed = MAX_RENDERED_DIFF_LINES + 50
val old = (1..changed).joinToString("\n") { "old $it" }
val new = (1..changed).joinToString("\n") { "new $it" }
val diff = toolEditDiff(
JsonObject(
mapOf(
"filePath" to JsonPrimitive("/tmp/big.txt"),
"oldString" to JsonPrimitive(old),
"newString" to JsonPrimitive(new),
),
),
)!!
// The stat still describes the whole edit...
assertEquals(changed, diff.added)
assertEquals(changed, diff.removed)
// ...but only a readable prefix is rendered, the rest counted as omitted.
assertEquals(MAX_RENDERED_DIFF_LINES, diff.lines.size)
assertEquals(2 * changed - MAX_RENDERED_DIFF_LINES, diff.omitted)
}
@Test
fun toolEditDiffIsNullForNonEdits() {
assertNull(toolEditDiff(null))