Skip to content

Commit 47b0af8

Browse files
only21milclaude
andcommitted
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 <[email protected]>
1 parent 5ffe045 commit 47b0af8

2 files changed

Lines changed: 38 additions & 15 deletions

File tree

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ describe("normalizeReviewGuide", () => {
121121
expect(result.stops).toHaveLength(1);
122122
});
123123

124+
it("merges excerpts that a later excerpt bridges into one", () => {
125+
const result = normalizeReviewGuide(
126+
guide({
127+
stops: [
128+
stop({
129+
excerpts: [
130+
excerpt("src/a.ts", 40, 41),
131+
{ ...excerpt("src/a.ts", 50, 51), caption: "Second" },
132+
excerpt("src/a.ts", 41, 50),
133+
],
134+
}),
135+
],
136+
}),
137+
change,
138+
);
139+
expect(result.stops[0]?.excerpts).toEqual([
140+
{ ...excerpt("src/a.ts", 40, 51), caption: "Second" },
141+
]);
142+
});
143+
124144
it("drops notes outside the stop's remaining excerpts", () => {
125145
const result = normalizeReviewGuide(
126146
guide({

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,29 @@ function clampExcerpt(
7474
function mergeExcerpts(
7575
excerpts: ReadonlyArray<PullRequestReviewGuideExcerpt>,
7676
): Array<PullRequestReviewGuideExcerpt> {
77-
const merged: Array<PullRequestReviewGuideExcerpt> = [];
77+
let merged: Array<PullRequestReviewGuideExcerpt> = [];
7878
for (const excerpt of excerpts) {
79-
const index = merged.findIndex(
80-
(other) =>
81-
other.path === excerpt.path &&
82-
other.side === excerpt.side &&
83-
other.start <= excerpt.end + 1 &&
84-
excerpt.start <= other.end + 1,
85-
);
86-
const other = merged[index];
87-
if (other === undefined) {
79+
const touches = (other: PullRequestReviewGuideExcerpt) =>
80+
other.path === excerpt.path &&
81+
other.side === excerpt.side &&
82+
other.start <= excerpt.end + 1 &&
83+
excerpt.start <= other.end + 1;
84+
// An excerpt can bridge several earlier ones; fold them all into the first.
85+
const group = merged.filter(touches);
86+
const first = group[0];
87+
if (first === undefined) {
8888
merged.push(excerpt);
8989
continue;
9090
}
91-
merged[index] = {
92-
...other,
93-
start: Math.min(other.start, excerpt.start),
94-
end: Math.max(other.end, excerpt.end),
95-
caption: other.caption || excerpt.caption,
91+
const combined = {
92+
...first,
93+
start: Math.min(excerpt.start, ...group.map((other) => other.start)),
94+
end: Math.max(excerpt.end, ...group.map((other) => other.end)),
95+
caption: group.find((other) => other.caption)?.caption ?? excerpt.caption,
9696
};
97+
merged = merged.flatMap((other) =>
98+
other === first ? [combined] : touches(other) ? [] : [other],
99+
);
97100
}
98101
return merged.map((excerpt) => ({
99102
...excerpt,

0 commit comments

Comments
 (0)