@devries-cameron20 / t3code Mirror · read-only / commits / 9474bcbae1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 10:45 UTC

fix(web): preserve trailing whitespace in review diffs (#25303)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

Victor Vogel committed Oct 30, 2026 at 16:13 UTC 9474bcbae1ed2b4296d119a3af3258c6a1dd4cea
4 files changed +53 -19
apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx
+1 -1
@@ -66,5 +66,5 @@ it.each([
66 expect(result()).toEqual({ loading: true, files: [] });
67
68 await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
69 - expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });
69 + expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });
70 });
apps/web/src/lib/diffRendering.test.ts
+44
@@ -219,6 +219,50 @@ describe("getRenderablePatch", () => {
219 if (parsed?.kind !== "files") return;
220 expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
221 });
222 +
223 + it("keeps whitespace at the end of the last hunk", () => {
224 + const patch = (lastLines: string[]) =>
225 + [
226 + "diff --git a/example.ts b/example.ts",
227 + "--- a/example.ts",
228 + "+++ b/example.ts",
229 + ...lastLines,
230 + "",
231 + ].join("\n");
232 +
233 + const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review");
234 + expect(trailing?.kind).toBe("files");
235 + if (trailing?.kind !== "files") return;
236 + expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]);
237 +
238 + const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review");
239 + expect(blankContext?.kind).toBe("files");
240 + if (blankContext?.kind !== "files") return;
241 + expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]);
242 + expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3);
243 +
244 + expect(getRenderablePatch(" \n\t\n", "review")).toBeNull();
245 + });
246 +
247 + it("caches patches that differ only in trailing whitespace separately", () => {
248 + const patch = (added: string) =>
249 + [
250 + "diff --git a/example.ts b/example.ts",
251 + "--- a/example.ts",
252 + "+++ b/example.ts",
253 + "@@ -1 +1 @@",
254 + "-a",
255 + `+${added}`,
256 + "",
257 + ].join("\n");
258 +
259 + const plain = getRenderablePatch(patch("b"), "review");
260 + const tabbed = getRenderablePatch(patch("b\t"), "review");
261 + expect(plain?.kind).toBe("files");
262 + expect(tabbed?.kind).toBe("files");
263 + if (plain?.kind !== "files" || tabbed?.kind !== "files") return;
264 + expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey);
265 + });
266 });
267
268 describe("diff file reconciliation", () => {
apps/web/src/lib/diffRendering.ts
+7 -17
@@ -34,14 +34,9 @@ export function fnv1a32(
34 }
35
36 function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
37 - const normalizedPatch = patch.trim();
38 - const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
39 - const secondary = fnv1a32(
40 - normalizedPatch,
41 - SECONDARY_HASH_SEED,
42 - SECONDARY_HASH_MULTIPLIER,
43 - ).toString(36);
44 - return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
37 + const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
38 + const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36);
39 + return `${scope}:${patch.length}:${primary}:${secondary}`;
40 }
41
42 export type RenderablePatch =
@@ -164,15 +159,10 @@ export function getRenderablePatch(
159 cacheScope = "diff-panel",
160 options: RenderablePatchOptions = {},
161 ): RenderablePatch | null {
167 - if (!patch) return null;
168 - const normalizedPatch = patch.trim();
169 - if (normalizedPatch.length === 0) return null;
162 + if (!patch?.trim()) return null;
163
164 try {
172 - const parsedPatches = parsePatchFiles(
173 - normalizedPatch,
174 - buildPatchCacheKey(normalizedPatch, cacheScope),
175 - );
165 + const parsedPatches = parsePatchFiles(patch, buildPatchCacheKey(patch, cacheScope));
166 const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
167 const files = sourceFiles.map((file) => {
168 const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;
@@ -184,13 +174,13 @@ export function getRenderablePatch(
174
175 return {
176 kind: "raw",
187 - text: normalizedPatch,
177 + text: patch,
178 reason: "Unsupported diff format. Showing raw patch.",
179 };
180 } catch {
181 return {
182 kind: "raw",
193 - text: normalizedPatch,
183 + text: patch,
184 reason: "Failed to parse patch. Showing raw patch.",
185 };
186 }
apps/web/src/lib/toolCallDiff.test.ts
+1 -1
@@ -54,7 +54,7 @@ describe("toolCallDiff", () => {
54 expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
55 expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
56 expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
57 - recordedContent.replace(/\n$/, ""),
57 + recordedContent,
58 );
59 }
60 }