From 47b0af85bd32ad43bff2635d85b2c498cfdb58e1 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Thu, 22 Oct 2026 06:06:55 -0500 Subject: [PATCH 1/3] fix(server): merge review-guide excerpts that a later excerpt bridges mergeExcerpts folded each excerpt into the first earlier excerpt it touched and never looked again. When a later excerpt spanned two earlier ones, the first grew to reach the second but both stayed, so the stop showed the shared lines twice. Fold every touching excerpt into the first one instead. Co-Authored-By: Claude Opus 5.5 --- .../pullRequestReviewGuideNormalize.test.ts | 20 +++++++++++ .../pullRequestReviewGuideNormalize.ts | 33 ++++++++++--------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts index 69479b1ccfeed..3a72869e0bbb9 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts @@ -121,6 +121,26 @@ describe("normalizeReviewGuide", () => { expect(result.stops).toHaveLength(1); }); + it("merges excerpts that a later excerpt bridges into one", () => { + const result = normalizeReviewGuide( + guide({ + stops: [ + stop({ + excerpts: [ + excerpt("src/a.ts", 40, 41), + { ...excerpt("src/a.ts", 50, 51), caption: "Second" }, + excerpt("src/a.ts", 41, 50), + ], + }), + ], + }), + change, + ); + expect(result.stops[0]?.excerpts).toEqual([ + { ...excerpt("src/a.ts", 40, 51), caption: "Second" }, + ]); + }); + it("drops notes outside the stop's remaining excerpts", () => { const result = normalizeReviewGuide( guide({ diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts index 4075f19ba9ba9..92bc1ff5235f9 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts @@ -74,26 +74,29 @@ function clampExcerpt( function mergeExcerpts( excerpts: ReadonlyArray, ): Array { - const merged: Array = []; + let merged: Array = []; for (const excerpt of excerpts) { - const index = merged.findIndex( - (other) => - other.path === excerpt.path && - other.side === excerpt.side && - other.start <= excerpt.end + 1 && - excerpt.start <= other.end + 1, - ); - const other = merged[index]; - if (other === undefined) { + const touches = (other: PullRequestReviewGuideExcerpt) => + other.path === excerpt.path && + other.side === excerpt.side && + other.start <= excerpt.end + 1 && + excerpt.start <= other.end + 1; + // An excerpt can bridge several earlier ones; fold them all into the first. + const group = merged.filter(touches); + const first = group[0]; + if (first === undefined) { merged.push(excerpt); continue; } - merged[index] = { - ...other, - start: Math.min(other.start, excerpt.start), - end: Math.max(other.end, excerpt.end), - caption: other.caption || excerpt.caption, + const combined = { + ...first, + start: Math.min(excerpt.start, ...group.map((other) => other.start)), + end: Math.max(excerpt.end, ...group.map((other) => other.end)), + caption: group.find((other) => other.caption)?.caption ?? excerpt.caption, }; + merged = merged.flatMap((other) => + other === first ? [combined] : touches(other) ? [] : [other], + ); } return merged.map((excerpt) => ({ ...excerpt, From dc795e9d16b4f5ff91f38376599161c1beb1a45f Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Fri, 30 Oct 2026 10:13:15 +0000 Subject: [PATCH 2/3] fix(server): preserve wide review guide excerpt coverage --- .../pullRequestReviewGuideNormalize.test.ts | 25 +++++++++++++++++++ .../pullRequestReviewGuideNormalize.ts | 15 ++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts index 3a72869e0bbb9..d11cf1873539f 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts @@ -139,6 +139,31 @@ describe("normalizeReviewGuide", () => { expect(result.stops[0]?.excerpts).toEqual([ { ...excerpt("src/a.ts", 40, 51), caption: "Second" }, ]); + + const tailNote = { path: "src/a.ts", line: 40, title: "Tail", body: "Keep this note." }; + const wide = normalizeReviewGuide( + guide({ + stops: [ + stop({ + excerpts: [ + excerpt("src/a.ts", 1, 25), + { ...excerpt("src/a.ts", 30, 45), caption: "Second" }, + excerpt("src/a.ts", 24, 32), + ], + notes: [tailNote], + }), + ], + }), + parseReviewGuideChange( + "src/a.ts\0", + "diff --git a/src/a.ts b/src/a.ts\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -0,0 +1,60 @@\n+added", + ), + ); + expect(wide.stops[0]?.excerpts).toEqual([ + { ...excerpt("src/a.ts", 1, 25), caption: "Second" }, + { ...excerpt("src/a.ts", 26, 45), caption: "Second" }, + ]); + expect(wide.stops[0]?.notes).toEqual([tailNote]); }); it("drops notes outside the stop's remaining excerpts", () => { diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts index 92bc1ff5235f9..c3dad882e2218 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts @@ -98,10 +98,17 @@ function mergeExcerpts( other === first ? [combined] : touches(other) ? [] : [other], ); } - return merged.map((excerpt) => ({ - ...excerpt, - end: Math.min(excerpt.end, excerpt.start + MAX_EXCERPT_LINES - 1), - })); + return merged.flatMap((excerpt) => { + const chunks: Array = []; + for (let start = excerpt.start; start <= excerpt.end; start += MAX_EXCERPT_LINES) { + chunks.push({ + ...excerpt, + start, + end: Math.min(excerpt.end, start + MAX_EXCERPT_LINES - 1), + }); + } + return chunks; + }); } const nonEmpty = (value: string) => value.trim() !== ""; From dc3e13ec265524a05e021cf3af8a6241ffa819fc Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Sat, 31 Oct 2026 07:15:42 +0000 Subject: [PATCH 3/3] fix(server): retain review guide excerpt length clamp --- .../pullRequestReviewGuideNormalize.test.ts | 7 ++----- .../pullRequestReviewGuideNormalize.ts | 15 ++++----------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts index f8b81110bae05..92704c51102fe 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts @@ -190,11 +190,8 @@ describe("normalizeReviewGuide", () => { "diff --git a/src/a.ts b/src/a.ts\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -0,0 +1,60 @@\n+added", ), ); - expect(wide.stops[0]?.excerpts).toEqual([ - { ...excerpt("src/a.ts", 1, 25), caption: "Second" }, - { ...excerpt("src/a.ts", 26, 45), caption: "Second" }, - ]); - expect(wide.stops[0]?.notes).toEqual([tailNote]); + expect(wide.stops[0]?.excerpts).toEqual([{ ...excerpt("src/a.ts", 1, 25), caption: "Second" }]); + expect(wide.stops[0]?.notes).toEqual([]); }); it("drops notes outside the stop's remaining excerpts", () => { diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts index b8a39202ff912..82edfc5fb8159 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts @@ -111,17 +111,10 @@ function mergeExcerpts( other === first ? [combined] : touches(other) ? [] : [other], ); } - return merged.flatMap((excerpt) => { - const chunks: Array = []; - for (let start = excerpt.start; start <= excerpt.end; start += MAX_EXCERPT_LINES) { - chunks.push({ - ...excerpt, - start, - end: Math.min(excerpt.end, start + MAX_EXCERPT_LINES - 1), - }); - } - return chunks; - }); + return merged.map((excerpt) => ({ + ...excerpt, + end: Math.min(excerpt.end, excerpt.start + MAX_EXCERPT_LINES - 1), + })); } const nonEmpty = (value: string) => value.trim() !== "";