Skip to content

Commit 44579d9

Browse files
fix(web): stale file contents no longer crash diff expansion
Expanding unchanged lines loads the files as they are now, but the partial patch being hydrated was computed earlier. When an agent had edited the file since, Pierre built hunks that did not fit the loaded lines and rendering threw, sometimes taking the whole app down. The shared diff contents loader now rejects contents that no longer reproduce the partial patch: every hunk's old and new ranges must fit and match the loaded lines (whitespace-insensitive, since `git diff -w` prints context from one side), and the unchanged tail after the last hunk must be the same length on both sides. Pierre already catches loader errors, so the file keeps showing its patch until the next diff refresh brings a fresh one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent faad769 commit 44579d9

2 files changed

Lines changed: 102 additions & 6 deletions

File tree

‎apps/web/src/lib/diffFileContents.test.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ function toContents(lines: ReadonlyArray<string>) {
1313
return `${lines.join("\n")}\n`;
1414
}
1515

16+
function parsePartialFileDiff(patch: string): FileDiffMetadata {
17+
const renderable = getRenderablePatch(patch, "diff-panel:light", {
18+
compactPartialHunkOffsets: true,
19+
});
20+
if (renderable?.kind !== "files") throw new Error("Expected a parsed patch.");
21+
return renderable.files[0]!;
22+
}
23+
1624
function partialFileDiff(): FileDiffMetadata {
1725
const patch = [
1826
"diff --git a/src/file01.ts b/src/file01.ts",
@@ -30,11 +38,7 @@ function partialFileDiff(): FileDiffMetadata {
3038
" line 33",
3139
"",
3240
].join("\n");
33-
const renderable = getRenderablePatch(patch, "diff-panel:light", {
34-
compactPartialHunkOffsets: true,
35-
});
36-
if (renderable?.kind !== "files") throw new Error("Expected a parsed patch.");
37-
return renderable.files[0]!;
41+
return parsePartialFileDiff(patch);
3842
}
3943

4044
function loaderReturning(oldContents: string, newContents: string) {
@@ -85,4 +89,28 @@ describe("createGitDiffFileContentsLoader", () => {
8589

8690
await expect(loadOutcome(loader, partialFileDiff())).resolves.toBe("rejected");
8791
});
92+
93+
it("loads a whitespace-ignored patch whose context shows one side's whitespace", async () => {
94+
const patch = [
95+
"diff --git a/src/file01.ts b/src/file01.ts",
96+
"index 1111111..2222222 100644",
97+
"--- a/src/file01.ts",
98+
"+++ b/src/file01.ts",
99+
"@@ -2,4 +2,4 @@ a",
100+
" b",
101+
" c",
102+
" d",
103+
"-e",
104+
"\\ No newline at end of file",
105+
"+E",
106+
"\\ No newline at end of file",
107+
"",
108+
].join("\n");
109+
const loaded = await loaderReturning(
110+
"a\nb\n c\nd\ne",
111+
"a\nb\nc\nd\nE",
112+
)(parsePartialFileDiff(patch));
113+
114+
expect(loaded.oldFile?.contents).toBe("a\nb\n c\nd\ne");
115+
});
88116
});

‎apps/web/src/lib/diffFileContents.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FileDiffContentsLoader } from "@pierre/diffs";
1+
import type { FileDiffContentsLoader, FileDiffMetadata } from "@pierre/diffs";
22
import {
33
squashAtomCommandFailure,
44
type AtomCommandResult,
@@ -41,6 +41,69 @@ type GetPullRequestDiffFileContents<E> = (request: {
4141
readonly input: PullRequestDiffFileContentsInput;
4242
}) => Promise<AtomCommandResult<PullRequestDiffFileContentsResult, E>>;
4343

44+
// Mirrors Pierre's splitFileContents so line counts match its hydration.
45+
function splitLines(contents: string): ReadonlyArray<string> {
46+
return contents === "" ? [] : contents.split(/(?<=\n)/);
47+
}
48+
49+
// Whitespace-insensitive: `git diff -w` prints context lines from only one side.
50+
function sameLine(patchLine: string | undefined, fileLine: string | undefined): boolean {
51+
return (
52+
patchLine !== undefined &&
53+
fileLine !== undefined &&
54+
patchLine.replace(/\s/g, "") === fileLine.replace(/\s/g, "")
55+
);
56+
}
57+
58+
function hunkSideEnd(start: number, count: number): number {
59+
return start - (count === 0 ? 0 : 1) + count;
60+
}
61+
62+
function hunkSideMatches(
63+
patchLines: ReadonlyArray<string>,
64+
patchIndex: number,
65+
fileLines: ReadonlyArray<string>,
66+
start: number,
67+
count: number,
68+
): boolean {
69+
const fileIndex = hunkSideEnd(start, count) - count;
70+
for (let offset = 0; offset < count; offset++) {
71+
if (!sameLine(patchLines[patchIndex + offset], fileLines[fileIndex + offset])) return false;
72+
}
73+
return true;
74+
}
75+
76+
// Pierre hydrates a partial patch by trusting its hunk positions, so contents that
77+
// changed since the patch was computed make it render corrupt hunks or throw.
78+
function reproducesPatch(fileDiff: FileDiffMetadata, oldContents: string, newContents: string) {
79+
const lastHunk = fileDiff.hunks.at(-1);
80+
if (lastHunk === undefined) return true;
81+
const oldLines = splitLines(oldContents);
82+
const newLines = splitLines(newContents);
83+
const hunksMatch = fileDiff.hunks.every(
84+
(hunk) =>
85+
hunkSideMatches(
86+
fileDiff.deletionLines,
87+
hunk.deletionLineIndex,
88+
oldLines,
89+
hunk.deletionStart,
90+
hunk.deletionCount,
91+
) &&
92+
hunkSideMatches(
93+
fileDiff.additionLines,
94+
hunk.additionLineIndex,
95+
newLines,
96+
hunk.additionStart,
97+
hunk.additionCount,
98+
),
99+
);
100+
return (
101+
hunksMatch &&
102+
oldLines.length - hunkSideEnd(lastHunk.deletionStart, lastHunk.deletionCount) ===
103+
newLines.length - hunkSideEnd(lastHunk.additionStart, lastHunk.additionCount)
104+
);
105+
}
106+
44107
function createDiffFileContentsLoader(
45108
load: (input: {
46109
readonly changeType: PullRequestDiffFileContentsInput["changeType"];
@@ -63,6 +126,11 @@ function createDiffFileContentsLoader(
63126
if (fileDiff.type === "rename-pure") {
64127
return { oldFile: null, newFile };
65128
}
129+
if (!reproducesPatch(fileDiff, contents.oldContents, contents.newContents)) {
130+
throw new Error(
131+
"This file changed since the diff was loaded. Refresh the diff to expand it.",
132+
);
133+
}
66134
return {
67135
oldFile: {
68136
name: oldPath,

0 commit comments

Comments
 (0)