@devries-cameron20 / t3code Mirror · read-only / commits / 38d50e3e5a
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 23, 01:45 UTC

fix(web): keep review comments inside the pull request's hunks (#12583)

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Allan Gomes committed Oct 19, 2026 at 17:21 UTC 38d50e3e5af24f99e40c207f2533db061a862415
2 files changed +91 -1
apps/web/src/reviewCommentContext.test.ts
+76
@@ -1,3 +1,4 @@
1 +import { hydratePartialDiff } from "@pierre/diffs";
2 import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
3 import { describe, expect, it } from "vite-plus/test";
4
@@ -6,6 +7,7 @@ import {
7 buildFileReviewComment,
8 formatReviewCommentFence,
9 inferReviewCommentFenceLanguage,
10 + resolveDiffReviewPosition,
11 restoreDiffReviewCommentRange,
12 } from "./reviewCommentContext";
13
@@ -112,4 +114,78 @@ describe("review comment context parsing", () => {
114 endSide: "additions",
115 });
116 });
117 +
118 + it("anchors review comments only on lines inside the host's hunks", () => {
119 + const oldLines = Array.from({ length: 30 }, (_, index) => `line ${index + 1}\n`);
120 + const newLines = [
121 + ...oldLines.slice(0, 6),
122 + ...Array.from({ length: 10 }, (_, index) => `new ${index + 1}\n`),
123 + ...oldLines.slice(6),
124 + ].map((line) => (line === "line 25\n" ? "line twenty-five\n" : line));
125 + // What the host returns: new lines 4 to 19 and 32 to 38. The insertion shifts the new side,
126 + // so new line 25 (old line 15) sits between the hunks while old line 25 is inside one.
127 + const fileDiff = parsePatchFiles(
128 + [
129 + "diff --git a/src/app.ts b/src/app.ts",
130 + "--- a/src/app.ts",
131 + "+++ b/src/app.ts",
132 + "@@ -4,6 +4,16 @@",
133 + " line 4",
134 + " line 5",
135 + " line 6",
136 + ...Array.from({ length: 10 }, (_, index) => `+new ${index + 1}`),
137 + " line 7",
138 + " line 8",
139 + " line 9",
140 + "@@ -22,7 +32,7 @@",
141 + " line 22",
142 + " line 23",
143 + " line 24",
144 + "-line 25",
145 + "+line twenty-five",
146 + " line 26",
147 + " line 27",
148 + " line 28",
149 + ].join("\n"),
150 + "review-position-hunks",
151 + )[0]!.files[0]!;
152 + // The Code tab tries the original partial patch before the hydrated viewer file.
153 + expect(resolveDiffReviewPosition(fileDiff, 25, "additions")).toBeNull();
154 + expect(resolveDiffReviewPosition(fileDiff, 23, "additions")).toBeNull();
155 + expect(resolveDiffReviewPosition(fileDiff, 15, "deletions")).toBeNull();
156 + // Expanding the file loads both sides whole, so the gaps around the hunks become lines
157 + // the viewer can select but the host will not anchor a comment to.
158 + hydratePartialDiff("merge", fileDiff, {
159 + oldFile: { name: "src/app.ts", contents: oldLines.join("") },
160 + newFile: { name: "src/app.ts", contents: newLines.join("") },
161 + });
162 + expect(fileDiff.isPartial).toBe(false);
163 +
164 + expect(resolveDiffReviewPosition(fileDiff, 1, "additions")).toBeNull();
165 + expect(resolveDiffReviewPosition(fileDiff, 25, "additions")).toBeNull();
166 + expect(resolveDiffReviewPosition(fileDiff, 39, "additions")).toBeNull();
167 + expect(resolveDiffReviewPosition(fileDiff, 1, "deletions")).toBeNull();
168 + expect(resolveDiffReviewPosition(fileDiff, 15, "deletions")).toBeNull();
169 + expect(resolveDiffReviewPosition(fileDiff, 29, "deletions")).toBeNull();
170 + expect(resolveDiffReviewPosition(fileDiff, 33, "additions")).toEqual({
171 + kind: "context",
172 + oldLine: 23,
173 + newLine: 33,
174 + side: "right",
175 + });
176 + expect(resolveDiffReviewPosition(fileDiff, 23, "deletions")).toEqual({
177 + kind: "context",
178 + oldLine: 23,
179 + newLine: 33,
180 + side: "left",
181 + });
182 + expect(resolveDiffReviewPosition(fileDiff, 7, "additions")).toEqual({
183 + kind: "added",
184 + newLine: 7,
185 + });
186 + expect(resolveDiffReviewPosition(fileDiff, 25, "deletions")).toEqual({
187 + kind: "deleted",
188 + oldLine: 25,
189 + });
190 + });
191 });
apps/web/src/reviewCommentContext.ts
+15 -1
@@ -2,6 +2,8 @@ import type { FileDiffMetadata, SelectedLineRange, SelectionSide } from "@pierre
2 import { PullRequestContextMetadata, type PullRequestReviewPosition } from "@t3tools/contracts";
3 import * as Schema from "effect/Schema";
4
5 +import { isLineInFileDiff } from "./components/pullRequest/pullRequestDiff.logic";
6 +
7 const ReviewCommentSelectionSchema = Schema.Struct({
8 start: Schema.Number,
9 side: Schema.Literals(["additions", "deletions"]),
@@ -344,13 +346,25 @@ export function resolveDiffReviewPosition(
346 })[0];
347 if (line === undefined) return null;
348
349 + // An explicit selection must not resolve to a same-numbered line on the other side.
350 + if (
351 + (side === "additions" && line.newLineNumber !== lineNumber) ||
352 + (side === "deletions" && line.oldLineNumber !== lineNumber)
353 + ) {
354 + return null;
355 + }
356 +
357 switch (line.change) {
358 case "add":
359 return line.newLineNumber === null ? null : { kind: "added", newLine: line.newLineNumber };
360 case "delete":
361 return line.oldLineNumber === null ? null : { kind: "deleted", oldLine: line.oldLineNumber };
362 case "context":
353 - return line.oldLineNumber === null || line.newLineNumber === null
363 + // An expanded file also shows the gaps between hunks, and GitHub rejects a whole review
364 + // over one comment anchored there.
365 + return line.oldLineNumber === null ||
366 + line.newLineNumber === null ||
367 + !isLineInFileDiff(fileDiff, "right", line.newLineNumber)
368 ? null
369 : {
370 kind: "context",