From ef3e332b2c3d0d1e92421f0af6413e2ad79cd5ba Mon Sep 17 00:00:00 2001 From: fullsend-code Date: Tue, 26 May 2026 22:17:33 +0000 Subject: [PATCH 1/2] fix(#1559): preserve prior successful review on agent failure When the review agent encounters a tool-failure after previously posting a successful review, the sticky comment was overwritten with a blanket "NOT reviewed" notice, collapsing the valid review findings into a hidden
block. This created a misleading signal for anyone checking the PR's review status. postFailureNotice() now checks for an existing sticky comment with a prior successful review before posting. When one exists, the failure is shown as a warning banner while the previous review findings remain prominently visible. Prior failures and stale-head notices are not preserved (replaced as before). Added ExtractCurrentContent() to the sticky package to extract the top-level content from a sticky comment body, stripping the marker, footer, and collapsed history blocks. Closes #1559 Signed-off-by: fullsend-code --- internal/cli/postreview.go | 26 +++++++ internal/cli/postreview_test.go | 116 ++++++++++++++++++++++++++++++++ internal/sticky/sticky.go | 25 +++++++ internal/sticky/sticky_test.go | 34 ++++++++++ 4 files changed, 201 insertions(+) diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index 9f17ab3beb..4681e2fe9e 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -218,6 +218,9 @@ The review agent reviewed commit `+"`%s`"+` but the PR HEAD is now `+"`%s`"+`. T } // postFailureNotice posts a failure comment as a sticky comment. +// When a prior successful review exists, the failure is shown as a +// warning banner while preserving the previous review findings +// prominently (not collapsed into history). func postFailureNotice(ctx context.Context, client forge.Client, owner, repo string, pr int, parsed ReviewResult, cfg sticky.Config, printer *ui.Printer) error { printer.StepStart("Review agent reported failure, posting notice") @@ -239,6 +242,21 @@ func postFailureNotice(ctx context.Context, client forge.Client, owner, repo str This PR was NOT reviewed. Do not count this as an approval.`, reason) } + // When a prior successful review exists, preserve it prominently + // and show the failure as a warning banner instead of replacing the + // review findings with "NOT reviewed". See #1559. + botUser, _ := client.GetAuthenticatedUser(ctx) + comments, listErr := client.ListIssueComments(ctx, owner, repo, pr) + if listErr == nil { + if existing := sticky.FindMarkedComment(comments, cfg.Marker, botUser); existing != nil { + priorContent := sticky.ExtractCurrentContent(existing.Body, cfg) + if priorContent != "" && !isFailureOrStaleContent(priorContent) { + body = fmt.Sprintf("## Review\n\n> **⚠️ Latest review run failed** (%s). Showing results from previous successful review.\n\n%s", reason, priorContent) + printer.StepInfo("Preserving prior successful review") + } + } + } + if _, err := sticky.Post(ctx, client, owner, repo, pr, body, cfg, printer); err != nil { return fmt.Errorf("posting failure notice: %w", err) } @@ -246,6 +264,14 @@ This PR was NOT reviewed. Do not count this as an approval.`, reason) return nil } +// isFailureOrStaleContent returns true if the content looks like a +// failure notice or stale-head notice rather than a successful review. +func isFailureOrStaleContent(content string) bool { + return strings.Contains(content, "This PR was NOT reviewed") || + strings.Contains(content, "**Reason:** stale-head") || + strings.Contains(content, "⚠️ **Latest review run failed**") +} + // submitFormalReview minimizes stale reviews by the same user, then // submits a new GitHub PR review. When commitSHA is non-empty, the // review is pinned to that commit via the commit_id field, closing diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index 18d5a5f791..8311558cbc 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -190,6 +190,122 @@ func TestPostFailureNotice_EmptyReason(t *testing.T) { assert.Contains(t, comments[0].Body, "NOT reviewed") } +func TestPostFailureNotice_PreservesPriorSuccessfulReview(t *testing.T) { + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + // Seed an existing sticky comment with a successful review. + fc.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ + ID: 100, + HTMLURL: "https://github.com/o/r/issues/1#issuecomment-100", + Body: "\n## Review\n\n**Verdict:** Approved\n\nLooks good, no issues found.", + Author: "bot", + }}, + } + printer := ui.New(io.Discard) + + cfg := sticky.Config{Marker: ""} + parsed := ReviewResult{Action: "failure", Reason: "tool-failure"} + err := postFailureNotice(context.Background(), fc, "o", "r", 1, parsed, cfg, printer) + require.NoError(t, err) + + require.Len(t, fc.UpdatedComments, 1) + updated := fc.UpdatedComments[0].Body + // The prior review findings should be visible (not just in collapsed history). + assert.Contains(t, updated, "Looks good, no issues found.") + // A warning banner should be present. + assert.Contains(t, updated, "Latest review run failed") + assert.Contains(t, updated, "tool-failure") + // Should NOT contain the blanket "NOT reviewed" message at top level. + // The top-level content should show the preserved review. + topLevel := sticky.ExtractCurrentContent(updated, cfg) + assert.NotContains(t, topLevel, "NOT reviewed") +} + +func TestPostFailureNotice_NoPriorReview(t *testing.T) { + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + printer := ui.New(io.Discard) + + cfg := sticky.Config{Marker: ""} + parsed := ReviewResult{Action: "failure", Reason: "tool-failure"} + err := postFailureNotice(context.Background(), fc, "o", "r", 1, parsed, cfg, printer) + require.NoError(t, err) + + comments := fc.IssueComments["o/r/1"] + require.Len(t, comments, 1) + assert.Contains(t, comments[0].Body, "NOT reviewed") + assert.Contains(t, comments[0].Body, "tool-failure") +} + +func TestPostFailureNotice_PriorFailureNotPreserved(t *testing.T) { + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + // Seed an existing sticky comment that is itself a failure notice. + fc.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ + ID: 100, + Body: "\n## Review\n\n**Reason:** token-limit\n\nThis PR was NOT reviewed. Do not count this as an approval.", + Author: "bot", + }}, + } + printer := ui.New(io.Discard) + + cfg := sticky.Config{Marker: ""} + parsed := ReviewResult{Action: "failure", Reason: "tool-failure"} + err := postFailureNotice(context.Background(), fc, "o", "r", 1, parsed, cfg, printer) + require.NoError(t, err) + + require.Len(t, fc.UpdatedComments, 1) + updated := fc.UpdatedComments[0].Body + topLevel := sticky.ExtractCurrentContent(updated, cfg) + // When the prior content was also a failure, the new failure should + // replace it (current behavior, not preserved). + assert.Contains(t, topLevel, "NOT reviewed") + assert.Contains(t, topLevel, "tool-failure") +} + +func TestPostFailureNotice_PriorStaleHeadNotPreserved(t *testing.T) { + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + fc.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ + ID: 100, + Body: "\n## Review\n\n**Reason:** stale-head\n\nThe review agent reviewed commit `abc` but the PR HEAD is now `def`.", + Author: "bot", + }}, + } + printer := ui.New(io.Discard) + + cfg := sticky.Config{Marker: ""} + parsed := ReviewResult{Action: "failure", Reason: "tool-failure"} + err := postFailureNotice(context.Background(), fc, "o", "r", 1, parsed, cfg, printer) + require.NoError(t, err) + + require.Len(t, fc.UpdatedComments, 1) + topLevel := sticky.ExtractCurrentContent(fc.UpdatedComments[0].Body, cfg) + assert.Contains(t, topLevel, "NOT reviewed") +} + +func TestIsFailureOrStaleContent(t *testing.T) { + tests := []struct { + name string + content string + want bool + }{ + {"successful review", "## Review\n\n**Verdict:** Approved", false}, + {"failure notice", "## Review\n\nThis PR was NOT reviewed.", true}, + {"stale-head", "## Review\n\n**Reason:** stale-head\n\nDiscarded.", true}, + {"prior preserved failure", "## Review\n\n> ⚠️ **Latest review run failed** (x).", true}, + {"empty", "", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, isFailureOrStaleContent(tt.content)) + }) + } +} + func TestCheckStaleHead_CaseInsensitive(t *testing.T) { fc := forge.NewFakeClient() fc.PullRequestHeadSHA = "abc1234567890abcdef1234567890abcdef123456" diff --git a/internal/sticky/sticky.go b/internal/sticky/sticky.go index 3d2d735405..995b3f7052 100644 --- a/internal/sticky/sticky.go +++ b/internal/sticky/sticky.go @@ -201,6 +201,31 @@ func BuildUpdatedBody(oldBody, newBody string, cfg Config) string { return combined } +// ExtractCurrentContent returns the top-level (non-history) content from +// a sticky comment body, stripping the marker prefix, footer, and any +// collapsed history blocks. Returns an empty string if the body has no +// meaningful content after stripping. +func ExtractCurrentContent(body string, cfg Config) string { + content, _ := strings.CutPrefix(body, cfg.Marker+"\n") + content, _ = strings.CutPrefix(content, cfg.Marker) + + // Strip footer if configured. + if cfg.FooterMarker != "" { + if idx := strings.Index(content, cfg.FooterMarker); idx >= 0 { + content = strings.TrimRight(content[:idx], "\n") + } + } + + // Remove history blocks (try sentinel-delimited first, then legacy). + matches := detailsRe.FindAllString(content, -1) + activeRe := detailsRe + if len(matches) == 0 { + activeRe = legacyDetailsRe + } + content = activeRe.ReplaceAllString(content, "") + return strings.TrimSpace(content) +} + // TruncateBody trims body to fit within maxSize, keeping the current // content at the top and trimming history from the end. The truncation // point is aligned to a valid UTF-8 boundary. diff --git a/internal/sticky/sticky_test.go b/internal/sticky/sticky_test.go index ec3d0ad5bd..3648f90da5 100644 --- a/internal/sticky/sticky_test.go +++ b/internal/sticky/sticky_test.go @@ -301,6 +301,40 @@ func TestPost_UpdateExisting_EmptyHTMLURL(t *testing.T) { assert.Contains(t, client.UpdatedComments[0].Body, "New.") } +func TestExtractCurrentContent_Basic(t *testing.T) { + cfg := Config{Marker: ""} + body := "\nReview findings here." + got := ExtractCurrentContent(body, cfg) + assert.Equal(t, "Review findings here.", got) +} + +func TestExtractCurrentContent_WithHistory(t *testing.T) { + cfg := Config{Marker: ""} + + body1 := "\nRun 1 content." + body2 := "\nRun 2 content." + combined := BuildUpdatedBody(body1, body2, cfg) + + got := ExtractCurrentContent(combined, cfg) + assert.Equal(t, "Run 2 content.", got) + assert.NotContains(t, got, "Run 1 content.") +} + +func TestExtractCurrentContent_WithFooter(t *testing.T) { + cfg := Config{Marker: "", FooterMarker: ""} + body := "\nReview content.\n\n\nFooter info." + got := ExtractCurrentContent(body, cfg) + assert.Equal(t, "Review content.", got) + assert.NotContains(t, got, "Footer info.") +} + +func TestExtractCurrentContent_Empty(t *testing.T) { + cfg := Config{Marker: ""} + body := "" + got := ExtractCurrentContent(body, cfg) + assert.Empty(t, got) +} + func TestPost_DryRunExisting(t *testing.T) { client := forge.NewFakeClient() client.IssueComments = map[string][]forge.IssueComment{ From d67e1609b3afe639d020b0ec34c54d7936a9f4b0 Mon Sep 17 00:00:00 2001 From: fullsend-fix Date: Wed, 27 May 2026 06:25:30 +0000 Subject: [PATCH 2/2] fix: align failure detection string with format string in postFailureNotice - Fix isFailureOrStaleContent to match the actual format string produced by postFailureNotice (emoji inside bold markers, not outside) - Update test case to use the correct format string - Log warning when GetAuthenticatedUser fails, consistent with sticky.Post Addresses review feedback on #1560 Signed-off-by: fullsend-fix --- internal/cli/postreview.go | 7 +++++-- internal/cli/postreview_test.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index 4681e2fe9e..12233e041c 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -245,7 +245,10 @@ This PR was NOT reviewed. Do not count this as an approval.`, reason) // When a prior successful review exists, preserve it prominently // and show the failure as a warning banner instead of replacing the // review findings with "NOT reviewed". See #1559. - botUser, _ := client.GetAuthenticatedUser(ctx) + botUser, authErr := client.GetAuthenticatedUser(ctx) + if authErr != nil { + printer.StepInfo("Could not determine bot user, marker spoofing protection degraded") + } comments, listErr := client.ListIssueComments(ctx, owner, repo, pr) if listErr == nil { if existing := sticky.FindMarkedComment(comments, cfg.Marker, botUser); existing != nil { @@ -269,7 +272,7 @@ This PR was NOT reviewed. Do not count this as an approval.`, reason) func isFailureOrStaleContent(content string) bool { return strings.Contains(content, "This PR was NOT reviewed") || strings.Contains(content, "**Reason:** stale-head") || - strings.Contains(content, "⚠️ **Latest review run failed**") + strings.Contains(content, "**⚠️ Latest review run failed**") } // submitFormalReview minimizes stale reviews by the same user, then diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index 8311558cbc..f5f26dae07 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -296,7 +296,7 @@ func TestIsFailureOrStaleContent(t *testing.T) { {"successful review", "## Review\n\n**Verdict:** Approved", false}, {"failure notice", "## Review\n\nThis PR was NOT reviewed.", true}, {"stale-head", "## Review\n\n**Reason:** stale-head\n\nDiscarded.", true}, - {"prior preserved failure", "## Review\n\n> ⚠️ **Latest review run failed** (x).", true}, + {"prior preserved failure", "## Review\n\n> **⚠️ Latest review run failed** (x).", true}, {"empty", "", false}, } for _, tt := range tests {