From 235f82e93a58469fd36026f43b23229e7713cca2 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 25 Oct 2026 22:23:42 -0500 Subject: [PATCH 1/2] fix(web): preserve trailing whitespace in review diffs getRenderablePatch trimmed the whole patch before parsing and hashing it. That dropped spaces, tabs and blank context lines at the end of the last hunk, and gave patches differing only there the same cache key. Trim now only decides whether the patch is empty. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/lib/diffRendering.test.ts | 44 ++++++++++++++++++++++++++ apps/web/src/lib/diffRendering.ts | 24 ++++---------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.ts index 7634177ec7462..dc8ced6d25738 100644 --- a/apps/web/src/lib/diffRendering.test.ts +++ b/apps/web/src/lib/diffRendering.test.ts @@ -219,6 +219,50 @@ describe("getRenderablePatch", () => { if (parsed?.kind !== "files") return; expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47); }); + + it("keeps whitespace at the end of the last hunk", () => { + const patch = (lastLines: string[]) => + [ + "diff --git a/example.ts b/example.ts", + "--- a/example.ts", + "+++ b/example.ts", + ...lastLines, + "", + ].join("\n"); + + const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review"); + expect(trailing?.kind).toBe("files"); + if (trailing?.kind !== "files") return; + expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]); + + const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review"); + expect(blankContext?.kind).toBe("files"); + if (blankContext?.kind !== "files") return; + expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]); + expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3); + + expect(getRenderablePatch(" \n\t\n", "review")).toBeNull(); + }); + + it("caches patches that differ only in trailing whitespace separately", () => { + const patch = (added: string) => + [ + "diff --git a/example.ts b/example.ts", + "--- a/example.ts", + "+++ b/example.ts", + "@@ -1 +1 @@", + "-a", + `+${added}`, + "", + ].join("\n"); + + const plain = getRenderablePatch(patch("b"), "review"); + const tabbed = getRenderablePatch(patch("b\t"), "review"); + expect(plain?.kind).toBe("files"); + expect(tabbed?.kind).toBe("files"); + if (plain?.kind !== "files" || tabbed?.kind !== "files") return; + expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey); + }); }); describe("diff file reconciliation", () => { diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.ts index 8cc9163e1b01b..664def0cd99fd 100644 --- a/apps/web/src/lib/diffRendering.ts +++ b/apps/web/src/lib/diffRendering.ts @@ -32,14 +32,9 @@ export function fnv1a32( } function buildPatchCacheKey(patch: string, scope = "diff-panel"): string { - const normalizedPatch = patch.trim(); - const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36); - const secondary = fnv1a32( - normalizedPatch, - SECONDARY_HASH_SEED, - SECONDARY_HASH_MULTIPLIER, - ).toString(36); - return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`; + const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36); + const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36); + return `${scope}:${patch.length}:${primary}:${secondary}`; } export type RenderablePatch = @@ -162,15 +157,10 @@ export function getRenderablePatch( cacheScope = "diff-panel", options: RenderablePatchOptions = {}, ): RenderablePatch | null { - if (!patch) return null; - const normalizedPatch = patch.trim(); - if (normalizedPatch.length === 0) return null; + if (!patch?.trim()) return null; try { - const parsedPatches = parsePatchFiles( - normalizedPatch, - buildPatchCacheKey(normalizedPatch, cacheScope), - ); + const parsedPatches = parsePatchFiles(patch, buildPatchCacheKey(patch, cacheScope)); const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files); const files = sourceFiles.map((file) => { const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file; @@ -182,13 +172,13 @@ export function getRenderablePatch( return { kind: "raw", - text: normalizedPatch, + text: patch, reason: "Unsupported diff format. Showing raw patch.", }; } catch { return { kind: "raw", - text: normalizedPatch, + text: patch, reason: "Failed to parse patch. Showing raw patch.", }; } From 0f3636f1bccb1c3731f62a517c7b577772e29301 Mon Sep 17 00:00:00 2001 From: maria-rcks <254055478+maria-rcks@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:44:55 +0000 Subject: [PATCH 2/2] test(web): diff tests expect the final line to keep its newline --- .../pullRequest/guide/useGuideDiffFiles.browser.test.tsx | 2 +- apps/web/src/lib/toolCallDiff.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx index cdb7d4099e5c7..8a92f2c2bf0d0 100644 --- a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx +++ b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx @@ -66,5 +66,5 @@ it.each([ expect(result()).toEqual({ loading: true, files: [] }); await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null)))); - expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] }); + expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] }); }); diff --git a/apps/web/src/lib/toolCallDiff.test.ts b/apps/web/src/lib/toolCallDiff.test.ts index cb33dd45cfb5a..8efd08ddfa38e 100644 --- a/apps/web/src/lib/toolCallDiff.test.ts +++ b/apps/web/src/lib/toolCallDiff.test.ts @@ -54,7 +54,7 @@ describe("toolCallDiff", () => { expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0); expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0); expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe( - recordedContent.replace(/\n$/, ""), + recordedContent, ); } }