From 9a41b410119935e04b792d4fa0fb8da782a8dd98 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 30 Oct 2026 00:23:05 +1100 Subject: [PATCH] fix(prreview): classify comment lines against the diff before posting --- internal/domain/workflow/prreview/diff.go | 16 ++++++--- .../domain/workflow/prreview/diff_test.go | 35 +++++++++++++++++++ internal/domain/workflow/prreview_stages.go | 10 +++--- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 internal/domain/workflow/prreview/diff_test.go diff --git a/internal/domain/workflow/prreview/diff.go b/internal/domain/workflow/prreview/diff.go index 613f9edd8..bcc1a95d5 100644 --- a/internal/domain/workflow/prreview/diff.go +++ b/internal/domain/workflow/prreview/diff.go @@ -674,9 +674,15 @@ var languages = map[string]string{ ".css": "css", } -// Commentable reports whether line in path is a line the change's diff shows -// (added or context), the only lines a forge accepts an inline comment on. -func Commentable(files []FileChange, path string, line int) bool { +// MapLine classifies one line of one file as added, context or outside the +// diff. Added and context lines are lines the diff shows, the only lines a +// forge accepts an inline review comment on; an outside line -- one no hunk +// contains, any line of a file the diff does not touch, or no line at all -- +// is where a comment belongs in the review body rather than inline. +func MapLine(files []FileChange, path string, line int) LineKind { + if line <= 0 { + return LineOutside + } for _, file := range files { if file.Path != path { continue @@ -684,10 +690,10 @@ func Commentable(files []FileChange, path string, line int) bool { for _, hunk := range file.Hunks { for _, placed := range hunk.positions() { if placed.Line == line { - return true + return placed.Kind } } } } - return false + return LineOutside } diff --git a/internal/domain/workflow/prreview/diff_test.go b/internal/domain/workflow/prreview/diff_test.go new file mode 100644 index 000000000..8caea338d --- /dev/null +++ b/internal/domain/workflow/prreview/diff_test.go @@ -0,0 +1,35 @@ +package prreview + +import "testing" + +func TestMapLine(t *testing.T) { + files := []FileChange{{ + Path: "calc/calc.go", + Hunks: []Hunk{ + {NewStart: 10, Lines: []string{" ctx", "+added", "-gone"}}, + {NewStart: 30, Lines: []string{"+later"}}, + }, + }} + tests := []struct { + name string + path string + line int + want LineKind + }{ + {"added line", "calc/calc.go", 11, LineAdded}, + {"context line", "calc/calc.go", 10, LineContext}, + {"added line in a later hunk", "calc/calc.go", 30, LineAdded}, + {"line the hunk does not contain", "calc/calc.go", 12, LineOutside}, + {"line between hunks", "calc/calc.go", 20, LineOutside}, + {"line before the hunk", "calc/calc.go", 5, LineOutside}, + {"file not in the diff", "README.md", 1, LineOutside}, + {"no line", "calc/calc.go", 0, LineOutside}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MapLine(files, tt.path, tt.line); got != tt.want { + t.Fatalf("MapLine(%q, %d) = %q, want %q", tt.path, tt.line, got, tt.want) + } + }) + } +} diff --git a/internal/domain/workflow/prreview_stages.go b/internal/domain/workflow/prreview_stages.go index 636a57615..40b72fbd5 100644 --- a/internal/domain/workflow/prreview_stages.go +++ b/internal/domain/workflow/prreview_stages.go @@ -699,15 +699,15 @@ func postApprovedReview(ctx context.Context, tc *TaskContext, gate task.ReviewGa } // forgeReview renders findings as one forge review: findings on a line the -// diff shows as inline comments, the rest as a list in the review body (a -// forge rejects the whole review over one comment it cannot place), and -// REQUEST_CHANGES when any finding blocks, unless the task opened the pull -// request itself. +// diff shows (added or context) as inline comments, findings on a line +// outside the diff as a list in the review body (a forge rejects the whole +// review over one comment it cannot place), and REQUEST_CHANGES when any +// finding blocks, unless the task opened the pull request itself. func forgeReview(findings []prreview.ScoredFinding, files []prreview.FileChange, ownPR bool) Review { review := Review{Event: string(reviewEvent(findings, ownPR))} var unanchored []string for _, f := range findings { - if f.LineStart <= 0 || !prreview.Commentable(files, f.File, f.LineStart) { + if prreview.MapLine(files, f.File, f.LineStart) == prreview.LineOutside { where := f.File if f.LineStart > 0 { where = fmt.Sprintf("%s:%d", f.File, f.LineStart)