fix(workflow): review a task's own pull request as a comment

GitHub refuses REQUEST_CHANGES from a pull request's author, so a blocking
finding on the PR a task opened lost every finding. The retry's identical
refusal is now reported once, and forge errors drop the gRPC framing.
This commit is contained in:
sam committed 2026-10-25 17:57:20 +11:00
1 parent 10e79c4894
commit eabb7886f8
4 files changed
+62 -8

No files matched your search

@@ -92,6 +92,7 @@ func stagePRReviewAndOpenPR(decisionStages []Stage, body func(*TaskContext) stri
if tc.PRSource == nil {
tc.PRSource = &localPRSource{tc: tc}
}
tc.prReview.ownPR = true
defer removePRSnapshot(tc)
for _, stage := range decisionStages {
if err := stage.Run(ctx, tc); err != nil {
+24 -1
View File
@@ -25,7 +25,7 @@ func TestForgeReviewPlacesOnlyDiffLinesInline(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
review := forgeReview([]prreview.ScoredFinding{{File: tt.file, LineStart: tt.line, Body: "b"}}, files)
review := forgeReview([]prreview.ScoredFinding{{File: tt.file, LineStart: tt.line, Body: "b"}}, files, false)
if got := len(review.Comments) == 1; got != tt.inline {
t.Fatalf("inline = %v, want %v (body %q)", got, tt.inline, review.Body)
}
@@ -35,3 +35,26 @@ func TestForgeReviewPlacesOnlyDiffLinesInline(t *testing.T) {
})
}
}
// A forge refuses REQUEST_CHANGES from a pull request's own author, which is
// every review of a pull request the task opened (task 18 lost its findings).
func TestForgeReviewEvent(t *testing.T) {
tests := []struct {
name string
blocking bool
ownPR bool
want prreview.ReviewEvent
}{
{"a blocking finding on someone else's pull request requests changes", true, false, prreview.ReviewEventRequestChanges},
{"a blocking finding on the task's own pull request comments", true, true, prreview.ReviewEventComment},
{"no blocking finding comments", false, false, prreview.ReviewEventComment},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
review := forgeReview([]prreview.ScoredFinding{{File: "a.go", Body: "b", Blocking: tt.blocking}}, nil, tt.ownPR)
if review.Event != string(tt.want) {
t.Fatalf("event = %q, want %q", review.Event, tt.want)
}
})
}
}
+24 -6
View File
@@ -41,6 +41,10 @@ type PRMetadata struct {
// phase's output for the phases after it to consume. Unexported, following
// the pattern the feasibility workflow's decision field set.
type prReviewState struct {
// ownPR marks a review of the pull request this task opened, which the
// forge lets its author comment on but never request changes on.
ownPR bool
metadata PRMetadata
diff string
files []prreview.FileChange
@@ -636,7 +640,7 @@ func runPROutputPhase(ctx context.Context, tc *TaskContext) (string, error) {
if err := postPRReview(ctx, tc); err != nil {
return "", fmt.Errorf("post review: %w", err)
}
detail := fmt.Sprintf("posted %d finding(s) as %s", len(tc.prReview.scored), prreview.ReviewEventFor(tc.prReview.scored))
detail := fmt.Sprintf("posted %d finding(s) as %s", len(tc.prReview.scored), reviewEvent(tc.prReview.scored, tc.prReview.ownPR))
if len(tc.prReview.skippedPhases) > 0 {
detail += fmt.Sprintf("; skipped: %s", strings.Join(tc.prReview.skippedPhases, ", "))
}
@@ -697,9 +701,10 @@ 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.
func forgeReview(findings []prreview.ScoredFinding, files []prreview.FileChange) Review {
review := Review{Event: string(prreview.ReviewEventFor(findings))}
// 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) {
@@ -718,6 +723,15 @@ func forgeReview(findings []prreview.ScoredFinding, files []prreview.FileChange)
return review
}
// reviewEvent is the event a review posts as: a forge refuses REQUEST_CHANGES
// from the pull request's author.
func reviewEvent(findings []prreview.ScoredFinding, ownPR bool) prreview.ReviewEvent {
if ownPR {
return prreview.ReviewEventComment
}
return prreview.ReviewEventFor(findings)
}
// postPRReview posts every finding as one forge review.
func postPRReview(ctx context.Context, tc *TaskContext) error {
if len(tc.prReview.scored) == 0 {
@@ -730,7 +744,7 @@ func postPRReview(ctx context.Context, tc *TaskContext) error {
// placement rejects the whole review, so on failure the findings are posted
// again with none inline.
func postReview(ctx context.Context, tc *TaskContext, owner, repo string, number int, headSHA string, findings []prreview.ScoredFinding) error {
review := forgeReview(findings, tc.prReview.files)
review := forgeReview(findings, tc.prReview.files, tc.prReview.ownPR)
places := make([]string, 0, len(review.Comments))
for _, c := range review.Comments {
places = append(places, fmt.Sprintf("%s:%d", c.Path, c.Line))
@@ -744,7 +758,11 @@ func postReview(ctx context.Context, tc *TaskContext, owner, repo string, number
return fmt.Errorf("%w (no inline comments; %d diff files)", err, len(tc.prReview.files))
}
tc.Log.Warn("forge refused inline review comments; posting them in the body", "err", err, "inline", places)
if retry := tc.Forge.CreateReviewComments(ctx, owner, repo, number, headSHA, forgeReview(findings, nil)); retry != nil {
if retry := tc.Forge.CreateReviewComments(ctx, owner, repo, number, headSHA, forgeReview(findings, nil, tc.prReview.ownPR)); retry != nil {
// The same refusal twice was never about placement; say it once.
if retry.Error() == err.Error() {
return err
}
return fmt.Errorf("%w (inline at %s); without inline comments: %w", err, strings.Join(places, ", "), retry)
}
return nil
+13 -1
View File
@@ -128,11 +128,23 @@ func call[T any](ctx context.Context, f *Forge, op string, fn func(forgev1.Forge
return zero, fmt.Errorf("this forge does not support %s", op)
}
if err != nil {
return zero, fmt.Errorf("forge %s: %w", op, err)
return zero, extensionError{op: op, err: err}
}
return out, nil
}
// extensionError prints the extension's own message without the gRPC framing
// around it, and still unwraps to the status for callers that branch on it.
type extensionError struct {
op string
err error
}
func (e extensionError) Error() string {
return "forge " + e.op + ": " + status.Convert(e.err).Message()
}
func (e extensionError) Unwrap() error { return e.err }
func ref(owner, repo string) *forgev1.RepoRef { return &forgev1.RepoRef{Owner: owner, Repo: repo} }
func prRef(owner, repo string, number int) *forgev1.PullRequestRef {