diff --git a/docs/guides/user/issues-commands.md b/docs/guides/user/issues-commands.md index 138983f6a7..d773e09841 100644 --- a/docs/guides/user/issues-commands.md +++ b/docs/guides/user/issues-commands.md @@ -253,6 +253,20 @@ HTML comment in the body. For Jira, the marker is stored as a comment entity property (Jira has no HTML comments, so a body-embedded marker would be visible to users). +On GitHub, when you post with one of these agent result markers, your +identity's earlier comments with the same marker are minimized as +outdated. They collapse, and a reader can still expand them. + +- `` +- `` +- `` +- `` +- `` + +Minimizing is best effort: if it fails, the earlier comments stay +expanded. GitLab and Jira cannot minimize comments, and other markers +never minimize anything. + ```bash echo "Triage complete. See PR #99." | fullsend issues post-comment \ --fullsend-dir .fullsend \ diff --git a/internal/cli/issues.go b/internal/cli/issues.go index c618ca79ff..62e5bebfc7 100644 --- a/internal/cli/issues.go +++ b/internal/cli/issues.go @@ -426,6 +426,16 @@ For --tracker jira, the marker is stored as an invisible comment entity property rather than embedded in the visible comment body, so marker character restrictions do not apply. +On GitHub, once the new comment is posted, this identity's earlier +comments with the same marker are minimized as outdated (collapsed, +still expandable) for these agent result markers: + "" + "" + "" + "" + "" +GitLab and Jira cannot minimize comments; other markers never do. + With --only-if-exists, the comment is posted only when an earlier comment with this marker exists (written by the identity this command posts as — the login on GitHub and GitLab, the account ID on @@ -502,8 +512,9 @@ func runIssuesPostComment(ctx context.Context, cfg *issuesPostCommentConfig) err printer.Header("Post Comment") stickyCfg := sticky.Config{ - Marker: cfg.marker, - DryRun: cfg.dryRun, + Marker: cfg.marker, + DryRun: cfg.dryRun, + MinimizeSuperseded: supersedingResultMarkers[cfg.marker], } // Jira stores markers as comment entity properties instead @@ -603,6 +614,10 @@ func postJiraStickyComment(ctx context.Context, jc *tracker.JiraClient, project // anyone could plant with the same marker is ignored). When that login // cannot be resolved (after a short retry) it returns an error and posts // nothing. +// +// With cfg.MinimizeSuperseded, a tracker implementing +// tracker.SupersededMinimizer folds the poster's earlier comments with +// this marker once the new one is created. Nothing posted, nothing folded. func postTrackerStickyComment(ctx context.Context, tc tracker.Client, project string, number int, body string, cfg sticky.Config, onlyIfExists bool, printer *ui.Printer) (string, error) { if strings.TrimSpace(body) == "" { return "", fmt.Errorf("comment body is empty") @@ -647,6 +662,10 @@ func postTrackerStickyComment(ctx context.Context, tc tracker.Client, project st return "", fmt.Errorf("creating comment: %w", err) } printer.StepDone("Comment created") + // Only trackers that can fold comments do; the rest leave them visible. + if m, ok := tc.(tracker.SupersededMinimizer); ok && cfg.MinimizeSuperseded { + m.MinimizeSuperseded(ctx, project, number, cfg.Marker, created, printer) + } return created.HTMLURL, nil } diff --git a/internal/cli/issues_minimize_test.go b/internal/cli/issues_minimize_test.go new file mode 100644 index 0000000000..dca8caf84d --- /dev/null +++ b/internal/cli/issues_minimize_test.go @@ -0,0 +1,176 @@ +package cli + +import ( + "bytes" + "context" + "io" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/fullsend-ai/fullsend/internal/forge" + "github.com/fullsend-ai/fullsend/internal/tracker" + "github.com/fullsend-ai/fullsend/internal/ui" +) + +// Issue #10092: issues post-comment --tracker github folds the poster's +// earlier results for superseding markers, as post-comment does (#9957). + +func minimizedCommentIDs(fc *forge.FakeClient) []string { + var ids []string + for _, m := range fc.MinimizedComments { + ids = append(ids, m.NodeID) + } + return ids +} + +// postTwiceOnGitHub runs issues post-comment twice with marker through a +// fake GitHub tracker and returns the fake and the thread afterwards. +func postTwiceOnGitHub(t *testing.T, fc *forge.FakeClient, marker string) []forge.IssueComment { + t.Helper() + ctx := context.Background() + if fc.AuthenticatedUser == "" { + fc.AuthenticatedUser = "bot" + } + cfg := &issuesPostCommentConfig{ + trackerName: trackerGitHub, + project: "acme/widgets", + number: 42, + marker: marker, + testClient: tracker.NewForgeClient(fc), + testPrinter: ui.New(io.Discard), + testBody: "first run", + } + require.NoError(t, runIssuesPostComment(ctx, cfg)) + cfg.testBody = "second run" + require.NoError(t, runIssuesPostComment(ctx, cfg)) + return fc.IssueComments["acme/widgets/42"] +} + +func TestRunIssuesPostComment_GitHubMinimizesSupersededResult(t *testing.T) { + for marker := range supersedingResultMarkers { + t.Run(marker, func(t *testing.T) { + fc := forge.NewFakeClient() + comments := postTwiceOnGitHub(t, fc, marker) + + require.Len(t, comments, 2) + assert.Equal(t, []string{comments[0].NodeID}, minimizedCommentIDs(fc), + "the first result is folded, the newest stays expanded") + assert.Equal(t, "OUTDATED", fc.MinimizedComments[0].Reason) + }) + } +} + +func TestRunIssuesPostComment_OtherMarkerMinimizesNothing(t *testing.T) { + fc := forge.NewFakeClient() + comments := postTwiceOnGitHub(t, fc, "") + + require.Len(t, comments, 2) + assert.Empty(t, fc.MinimizedComments, "only superseding result markers fold earlier comments") +} + +func TestRunIssuesPostComment_NeverMinimizesAnotherAuthorsComment(t *testing.T) { + const marker = "" + fc := forge.NewFakeClient() + fc.IssueComments = map[string][]forge.IssueComment{ + "acme/widgets/42": {{ID: 1, NodeID: "IC_planted", Body: marker + "\nplanted", Author: "mallory"}}, + } + comments := postTwiceOnGitHub(t, fc, marker) + + require.Len(t, comments, 3) + assert.Equal(t, []string{comments[1].NodeID}, minimizedCommentIDs(fc), + "only the poster's own earlier result is folded") +} + +func TestRunIssuesPostComment_MinimizeNotSupportedStillPosts(t *testing.T) { + // GitLab's forge client reports minimize as not supported. + fc := forge.NewFakeClient() + fc.Errors = map[string]error{"MinimizeComment": forge.ErrNotSupported} + comments := postTwiceOnGitHub(t, fc, "") + + require.Len(t, comments, 2) + assert.Empty(t, fc.MinimizedComments) +} + +func TestRunIssuesPostComment_DryRunMinimizesNothing(t *testing.T) { + const marker = "" + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + fc.IssueComments = map[string][]forge.IssueComment{ + "acme/widgets/42": {{ID: 1, NodeID: "IC_1", Body: marker + "\nfirst", Author: "bot"}}, + } + cfg := &issuesPostCommentConfig{ + trackerName: trackerGitHub, + project: "acme/widgets", + number: 42, + marker: marker, + dryRun: true, + testClient: tracker.NewForgeClient(fc), + testPrinter: ui.New(io.Discard), + testBody: "second", + } + require.NoError(t, runIssuesPostComment(context.Background(), cfg)) + assert.Empty(t, fc.MinimizedComments) +} + +func TestRunIssuesPostComment_OnlyIfExistsNothingPostedMinimizesNothing(t *testing.T) { + const marker = "" + fastSelfLookup(t) + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + // Another author's result does not open the gate, so nothing is posted. + fc.IssueComments = map[string][]forge.IssueComment{ + "acme/widgets/42": {{ID: 1, NodeID: "IC_1", Body: marker + "\nplanted", Author: "mallory"}}, + } + var out bytes.Buffer + cfg := &issuesPostCommentConfig{ + trackerName: trackerGitHub, + project: "acme/widgets", + number: 42, + marker: marker, + onlyIfExists: true, + testClient: tracker.NewForgeClient(fc), + testPrinter: ui.New(&out), + testBody: "all clear", + } + require.NoError(t, runIssuesPostComment(context.Background(), cfg)) + assert.Contains(t, out.String(), "nothing to post") + assert.Len(t, fc.IssueComments["acme/widgets/42"], 1) + assert.Empty(t, fc.MinimizedComments) +} + +func TestRunIssuesPostComment_JiraSupersedingMarkerUnchanged(t *testing.T) { + // Jira does not implement tracker.SupersededMinimizer: both results stay. + tc, _, err := tracker.NewFakeJiraClientWithFake("https://acme.atlassian.net") + require.NoError(t, err) + _, ok := any(tc).(tracker.SupersededMinimizer) + require.False(t, ok, "Jira cannot fold comments") + + ctx := context.Background() + cfg := &issuesPostCommentConfig{ + trackerName: trackerJira, + project: "PROJ", + number: 42, + marker: "", + testClient: tc, + testPrinter: ui.New(io.Discard), + testBody: "first run", + } + require.NoError(t, runIssuesPostComment(ctx, cfg)) + cfg.testBody = "second run" + require.NoError(t, runIssuesPostComment(ctx, cfg)) + + comments, err := tc.ListComments(ctx, "PROJ", 42) + require.NoError(t, err) + require.Len(t, comments, 2) + assert.Contains(t, string(comments[0].Body), "first run") + assert.Contains(t, string(comments[1].Body), "second run") +} + +func TestNewIssuesPostCommentCmd_HelpNamesSupersedingMarkers(t *testing.T) { + long := newIssuesPostCommentCmd().Long + for marker := range supersedingResultMarkers { + assert.Contains(t, long, marker, "the help text names every marker that folds earlier results") + } +} diff --git a/internal/cli/postcomment.go b/internal/cli/postcomment.go index 4a59bd2202..32a6f6f37c 100644 --- a/internal/cli/postcomment.go +++ b/internal/cli/postcomment.go @@ -12,21 +12,6 @@ import ( "github.com/fullsend-ai/fullsend/internal/ui" ) -// supersedingResultMarkers are the agent result markers whose newest -// comment replaces the earlier ones on the thread. When a comment with one -// of them is posted, the poster's earlier comments with the same marker are -// minimized as outdated (#9957). The review result and review-skip note -// are posted by post-review and review-dedup; triage, prioritize and the -// risk assessment are posted through post-comment by the agents' post -// scripts. Other markers (notices, custom agents) are left visible. -var supersedingResultMarkers = map[string]bool{ - reviewMarker: true, - reviewSkipMarker: true, - "": true, - "": true, - "": true, -} - func newPostCommentCmd() *cobra.Command { var ( repo string @@ -54,10 +39,15 @@ the thread itself is the history. The --marker flag identifies this agent's comments. Each agent should use a unique marker (e.g. ""). -For the triage, prioritize and risk-assessment result markers, the -poster's earlier comments with the same marker are minimized as -outdated once the new comment is posted, so only the newest result -stays expanded. +On GitHub, once the new comment is posted, this identity's earlier +comments with the same marker are minimized as outdated (collapsed, +still expandable) for these agent result markers: + "" + "" + "" + "" + "" +Other markers never minimize anything. The --result flag accepts a file path or "-" for stdin.`, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/internal/cli/superseding_markers.go b/internal/cli/superseding_markers.go new file mode 100644 index 0000000000..a421da0747 --- /dev/null +++ b/internal/cli/superseding_markers.go @@ -0,0 +1,17 @@ +package cli + +// supersedingResultMarkers are the agent result markers whose newest +// comment replaces the earlier ones on the thread. When a comment with one +// of them is posted, the poster's earlier comments with the same marker are +// minimized as outdated (#9957). The review result and review-skip note +// are posted by post-review and review-dedup; triage, prioritize and the +// risk assessment are posted by the agents' post scripts through +// post-comment or issues post-comment. Other markers (notices, custom +// agents) are left visible. +var supersedingResultMarkers = map[string]bool{ + reviewMarker: true, + reviewSkipMarker: true, + "": true, + "": true, + "": true, +} diff --git a/internal/sticky/minimize_superseded_test.go b/internal/sticky/minimize_superseded_test.go index 4c42d4ae77..986df0b9e8 100644 --- a/internal/sticky/minimize_superseded_test.go +++ b/internal/sticky/minimize_superseded_test.go @@ -1,10 +1,7 @@ package sticky import ( - "bytes" "context" - "errors" - "fmt" "io" "testing" @@ -17,7 +14,8 @@ import ( // Issue #9957: every run appends its result (#9334), so a result that // supersedes earlier ones folds them as outdated, the way post-review -// already minimizes the bot's earlier formal reviews. +// already minimizes the bot's earlier formal reviews. The folding rules +// are tested in internal/supersede; these tests cover sticky's use of it. const testMarker = "" @@ -100,116 +98,3 @@ func TestMinimizeSuperseded_SkipsCommentsPostedAfterNewest(t *testing.T) { MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(io.Discard)) assert.Equal(t, []string{"IC_100"}, minimizedNodeIDs(client)) } - -func TestMinimizeSuperseded_NewestMissingFromListing(t *testing.T) { - // A lagging listing that lacks newest still must not fold a comment a - // concurrent run posted after it. - client := forge.NewFakeClient() - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": { - {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, - {ID: 102, NodeID: "IC_102", Body: testMarker + "\nA concurrent run posted later.", Author: "bot"}, - }, - } - newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} - - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(io.Discard)) - assert.Equal(t, []string{"IC_100"}, minimizedNodeIDs(client)) -} - -func TestMinimizeSuperseded_UnknownIDMinimizesNothing(t *testing.T) { - client := forge.NewFakeClient() - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": {{ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}}, - } - newest := forge.IssueComment{NodeID: "IC_101", Author: "bot"} - - var out bytes.Buffer - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) - assert.Empty(t, client.MinimizedComments) - assert.Contains(t, out.String(), "New comment ID unknown") -} - -func TestMinimizeSuperseded_UnknownAuthorMinimizesNothing(t *testing.T) { - for name, newest := range map[string]*forge.IssueComment{ - "nil comment": nil, - "empty author": {ID: 101, NodeID: "IC_101"}, - } { - t.Run(name, func(t *testing.T) { - client := forge.NewFakeClient() - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": {{ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: ""}}, - } - var out bytes.Buffer - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, newest, ui.New(&out)) - assert.Empty(t, client.MinimizedComments) - assert.Contains(t, out.String(), "Posting identity unknown") - }) - } -} - -func TestMinimizeSuperseded_EmptyMarkerMinimizesNothing(t *testing.T) { - client := forge.NewFakeClient() - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": {{ID: 100, NodeID: "IC_100", Body: "Older.", Author: "bot"}}, - } - newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} - MinimizeSuperseded(context.Background(), client, "o", "r", 1, " ", &newest, ui.New(io.Discard)) - assert.Empty(t, client.MinimizedComments) -} - -func TestMinimizeSuperseded_ListFailureIsBestEffort(t *testing.T) { - client := forge.NewFakeClient() - client.Errors = map[string]error{"ListIssueComments": errors.New("boom")} - newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} - - var out bytes.Buffer - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) - assert.Empty(t, client.MinimizedComments) - assert.Contains(t, out.String(), "Could not list comments") -} - -func TestMinimizeSuperseded_NotSupportedStopsAfterFirstAttempt(t *testing.T) { - client := &countingMinimizeClient{FakeClient: forge.NewFakeClient(), err: forge.ErrNotSupported} - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": { - {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, - {ID: 101, NodeID: "IC_101", Body: testMarker + "\nOld.", Author: "bot"}, - }, - } - newest := forge.IssueComment{ID: 102, NodeID: "IC_102", Author: "bot"} - - var out bytes.Buffer - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) - assert.Equal(t, 1, client.calls, "an unsupported forge is not asked again for each comment") - assert.Contains(t, out.String(), "Minimize not supported") -} - -func TestMinimizeSuperseded_MinimizeFailureContinues(t *testing.T) { - client := &countingMinimizeClient{FakeClient: forge.NewFakeClient(), err: fmt.Errorf("rate limited")} - client.IssueComments = map[string][]forge.IssueComment{ - "o/r/1": { - {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, - {ID: 101, NodeID: "IC_101", Body: testMarker + "\nOld.", Author: "bot"}, - }, - } - newest := forge.IssueComment{ID: 102, NodeID: "IC_102", Author: "bot"} - - var out bytes.Buffer - MinimizeSuperseded(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) - assert.Equal(t, 2, client.calls, "one failed minimize does not stop the rest") - assert.Contains(t, out.String(), "Could not minimize comment 100") -} - -// countingMinimizeClient fails every MinimizeComment with err and counts -// the calls. -type countingMinimizeClient struct { - *forge.FakeClient - err error - calls int -} - -func (c *countingMinimizeClient) MinimizeComment(context.Context, string, string) error { - c.calls++ - return c.err -} diff --git a/internal/sticky/sticky.go b/internal/sticky/sticky.go index fe9fbb6014..4a763d9144 100644 --- a/internal/sticky/sticky.go +++ b/internal/sticky/sticky.go @@ -10,6 +10,7 @@ import ( "github.com/fullsend-ai/fullsend/internal/forge" "github.com/fullsend-ai/fullsend/internal/statuscomment" + "github.com/fullsend-ai/fullsend/internal/supersede" "github.com/fullsend-ai/fullsend/internal/ui" ) @@ -110,48 +111,10 @@ func PostComment(ctx context.Context, client forge.Client, owner, repo string, n // posted, so a listing or minimize failure is logged and the earlier // comments stay visible. Forges that cannot minimize (GitLab, Forgejo) // leave every earlier comment as it is. +// +// The rules live in supersede.Minimize, shared with tracker.ForgeClient. func MinimizeSuperseded(ctx context.Context, client forge.Client, owner, repo string, number int, marker string, newest *forge.IssueComment, printer *ui.Printer) { - if newest == nil || newest.Author == "" || strings.TrimSpace(marker) == "" { - printer.StepInfo("Posting identity unknown; earlier comments left visible") - return - } - // Without newest's ID there is no way to tell older comments from newer. - if newest.ID == 0 { - printer.StepInfo("New comment ID unknown; earlier comments left visible") - return - } - comments, err := client.ListIssueComments(ctx, owner, repo, number) - if err != nil { - printer.StepWarn(fmt.Sprintf("Could not list comments to fold superseded results: %v", err)) - return - } - var stale []forge.IssueComment - for _, c := range comments { - // Comment IDs increase, so a higher ID was posted later (by a - // concurrent run) and is not superseded. Comparing IDs holds even - // when a lagging listing lacks newest. - if c.ID >= newest.ID { - continue - } - if c.Author == newest.Author && c.NodeID != "" && strings.HasPrefix(c.Body, marker) { - stale = append(stale, c) - } - } - if len(stale) == 0 { - return - } - - printer.StepStart(fmt.Sprintf("Minimizing %d superseded comment(s)", len(stale))) - for _, c := range stale { - if err := client.MinimizeComment(ctx, c.NodeID, "OUTDATED"); err != nil { - if forge.IsNotSupported(err) { - printer.StepInfo("Minimize not supported on this forge; earlier comments left visible") - return - } - printer.StepWarn(fmt.Sprintf("Could not minimize comment %d: %v", c.ID, err)) - } - } - printer.StepDone("Superseded comments minimized") + supersede.Minimize(ctx, client, owner, repo, number, marker, newest, printer) } // UpdateComment replaces the body of a comment created in this same run diff --git a/internal/supersede/supersede.go b/internal/supersede/supersede.go new file mode 100644 index 0000000000..cca1d317d9 --- /dev/null +++ b/internal/supersede/supersede.go @@ -0,0 +1,71 @@ +// Package supersede folds an agent's earlier result comments once a newer +// one is posted (#9957). It depends only on forge and ui, so both sticky +// and tracker can share it: tracker cannot import sticky, which imports +// tracker through statuscomment. +package supersede + +import ( + "context" + "fmt" + "strings" + + "github.com/fullsend-ai/fullsend/internal/forge" + "github.com/fullsend-ai/fullsend/internal/ui" +) + +// Minimize folds, as OUTDATED, the comments on the thread that the newly +// created comment newest supersedes: comments posted before it whose body +// starts with marker and whose author is exactly newest's author — the +// identity that just posted. newest itself stays visible, and so does any +// comment from another author or one posted after newest. When newest's +// author is unknown nothing is minimized: matching on the marker alone +// would fold a comment anyone could write. Nothing is minimized either +// when newest's ID is unknown (0). +// +// It is best effort and returns nothing: the new comment is already +// posted, so a listing or minimize failure is logged and the earlier +// comments stay visible. Forges that cannot minimize (GitLab, Forgejo) +// leave every earlier comment as it is. +func Minimize(ctx context.Context, client forge.Client, owner, repo string, number int, marker string, newest *forge.IssueComment, printer *ui.Printer) { + if newest == nil || newest.Author == "" || strings.TrimSpace(marker) == "" { + printer.StepInfo("Posting identity unknown; earlier comments left visible") + return + } + // Without newest's ID there is no way to tell older comments from newer. + if newest.ID == 0 { + printer.StepInfo("New comment ID unknown; earlier comments left visible") + return + } + comments, err := client.ListIssueComments(ctx, owner, repo, number) + if err != nil { + printer.StepWarn(fmt.Sprintf("Could not list comments to fold superseded results: %v", err)) + return + } + var stale []forge.IssueComment + for _, c := range comments { + // Comment IDs increase, so a higher ID was posted later (by a + // concurrent run) and is not superseded. Comparing IDs holds even + // when a lagging listing lacks newest. + if c.ID >= newest.ID { + continue + } + if c.Author == newest.Author && c.NodeID != "" && strings.HasPrefix(c.Body, marker) { + stale = append(stale, c) + } + } + if len(stale) == 0 { + return + } + + printer.StepStart(fmt.Sprintf("Minimizing %d superseded comment(s)", len(stale))) + for _, c := range stale { + if err := client.MinimizeComment(ctx, c.NodeID, "OUTDATED"); err != nil { + if forge.IsNotSupported(err) { + printer.StepInfo("Minimize not supported on this forge; earlier comments left visible") + return + } + printer.StepWarn(fmt.Sprintf("Could not minimize comment %d: %v", c.ID, err)) + } + } + printer.StepDone("Superseded comments minimized") +} diff --git a/internal/supersede/supersede_test.go b/internal/supersede/supersede_test.go new file mode 100644 index 0000000000..6daeec925a --- /dev/null +++ b/internal/supersede/supersede_test.go @@ -0,0 +1,187 @@ +package supersede + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/fullsend-ai/fullsend/internal/forge" + "github.com/fullsend-ai/fullsend/internal/ui" +) + +const testMarker = "" + +func minimizedNodeIDs(client *forge.FakeClient) []string { + var ids []string + for _, m := range client.MinimizedComments { + ids = append(ids, m.NodeID) + } + return ids +} + +func TestMinimize_FoldsOwnEarlierResultsOnly(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": { + {ID: 100, NodeID: "IC_100", Body: testMarker + "\nFirst result.", Author: "bot"}, + {ID: 101, NodeID: "IC_101", Body: testMarker + "\nPlanted by someone else.", Author: "mallory"}, + {ID: 102, NodeID: "IC_102", Body: "\nAnother agent's result.", Author: "bot"}, + {ID: 103, NodeID: "IC_103", Body: "Quoting " + testMarker + " mid-body.", Author: "bot"}, + {ID: 104, NodeID: "", Body: testMarker + "\nNo node ID.", Author: "bot"}, + {ID: 105, NodeID: "IC_105", Body: testMarker + "\nSecond result.", Author: "bot"}, + }, + } + newest := forge.IssueComment{ID: 106, NodeID: "IC_106", Author: "bot"} + + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) + assert.Equal(t, []string{"IC_100", "IC_105"}, minimizedNodeIDs(client)) + for _, m := range client.MinimizedComments { + assert.Equal(t, "OUTDATED", m.Reason) + } + assert.Contains(t, out.String(), "Superseded comments minimized") +} + +func TestMinimize_NothingStaleMinimizesNothing(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ID: 100, NodeID: "IC_100", Body: "Unrelated.", Author: "bot"}}, + } + newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} + + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(io.Discard)) + assert.Empty(t, client.MinimizedComments) +} + +func TestMinimize_SkipsCommentsPostedAfterNewest(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": { + {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, + {ID: 101, NodeID: "IC_101", Body: testMarker + "\nThis run.", Author: "bot"}, + {ID: 102, NodeID: "IC_102", Body: testMarker + "\nA concurrent run posted later.", Author: "bot"}, + }, + } + newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} + + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(io.Discard)) + assert.Equal(t, []string{"IC_100"}, minimizedNodeIDs(client)) +} + +func TestMinimize_NewestMissingFromListing(t *testing.T) { + // A lagging listing that lacks newest still must not fold a comment a + // concurrent run posted after it. + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": { + {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, + {ID: 102, NodeID: "IC_102", Body: testMarker + "\nA concurrent run posted later.", Author: "bot"}, + }, + } + newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} + + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(io.Discard)) + assert.Equal(t, []string{"IC_100"}, minimizedNodeIDs(client)) +} + +func TestMinimize_UnknownIDMinimizesNothing(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}}, + } + newest := forge.IssueComment{NodeID: "IC_101", Author: "bot"} + + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) + assert.Empty(t, client.MinimizedComments) + assert.Contains(t, out.String(), "New comment ID unknown") +} + +func TestMinimize_UnknownAuthorMinimizesNothing(t *testing.T) { + for name, newest := range map[string]*forge.IssueComment{ + "nil comment": nil, + "empty author": {ID: 101, NodeID: "IC_101"}, + } { + t.Run(name, func(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: ""}}, + } + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, newest, ui.New(&out)) + assert.Empty(t, client.MinimizedComments) + assert.Contains(t, out.String(), "Posting identity unknown") + }) + } +} + +func TestMinimize_EmptyMarkerMinimizesNothing(t *testing.T) { + client := forge.NewFakeClient() + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": {{ID: 100, NodeID: "IC_100", Body: "Older.", Author: "bot"}}, + } + newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} + Minimize(context.Background(), client, "o", "r", 1, " ", &newest, ui.New(io.Discard)) + assert.Empty(t, client.MinimizedComments) +} + +func TestMinimize_ListFailureIsBestEffort(t *testing.T) { + client := forge.NewFakeClient() + client.Errors = map[string]error{"ListIssueComments": errors.New("boom")} + newest := forge.IssueComment{ID: 101, NodeID: "IC_101", Author: "bot"} + + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) + assert.Empty(t, client.MinimizedComments) + assert.Contains(t, out.String(), "Could not list comments") +} + +func TestMinimize_NotSupportedStopsAfterFirstAttempt(t *testing.T) { + client := &countingMinimizeClient{FakeClient: forge.NewFakeClient(), err: forge.ErrNotSupported} + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": { + {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, + {ID: 101, NodeID: "IC_101", Body: testMarker + "\nOld.", Author: "bot"}, + }, + } + newest := forge.IssueComment{ID: 102, NodeID: "IC_102", Author: "bot"} + + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) + assert.Equal(t, 1, client.calls, "an unsupported forge is not asked again for each comment") + assert.Contains(t, out.String(), "Minimize not supported") +} + +func TestMinimize_MinimizeFailureContinues(t *testing.T) { + client := &countingMinimizeClient{FakeClient: forge.NewFakeClient(), err: fmt.Errorf("rate limited")} + client.IssueComments = map[string][]forge.IssueComment{ + "o/r/1": { + {ID: 100, NodeID: "IC_100", Body: testMarker + "\nOlder.", Author: "bot"}, + {ID: 101, NodeID: "IC_101", Body: testMarker + "\nOld.", Author: "bot"}, + }, + } + newest := forge.IssueComment{ID: 102, NodeID: "IC_102", Author: "bot"} + + var out bytes.Buffer + Minimize(context.Background(), client, "o", "r", 1, testMarker, &newest, ui.New(&out)) + assert.Equal(t, 2, client.calls, "one failed minimize does not stop the rest") + assert.Contains(t, out.String(), "Could not minimize comment 100") +} + +// countingMinimizeClient fails every MinimizeComment with err and counts +// the calls. +type countingMinimizeClient struct { + *forge.FakeClient + err error + calls int +} + +func (c *countingMinimizeClient) MinimizeComment(context.Context, string, string) error { + c.calls++ + return c.err +} diff --git a/internal/tracker/forge_client.go b/internal/tracker/forge_client.go index ad6cc030d5..814706eb76 100644 --- a/internal/tracker/forge_client.go +++ b/internal/tracker/forge_client.go @@ -7,6 +7,8 @@ import ( "strings" "github.com/fullsend-ai/fullsend/internal/forge" + "github.com/fullsend-ai/fullsend/internal/supersede" + "github.com/fullsend-ai/fullsend/internal/ui" ) // ForgeClient adapts a forge.Client to the tracker.Client interface. It @@ -197,6 +199,27 @@ func (c *ForgeClient) DeleteCommentReaction(ctx context.Context, project string, return c.forge.DeleteIssueCommentReaction(ctx, owner, repo, id, reactionID) } +// MinimizeSuperseded implements SupersededMinimizer with the same rules as +// sticky.MinimizeSuperseded (see supersede.Minimize). A forge that cannot +// minimize, such as GitLab, leaves the earlier comments visible. +func (c *ForgeClient) MinimizeSuperseded(ctx context.Context, project string, number int, marker string, newest *Comment, printer *ui.Printer) { + owner, repo, err := splitProject(project) + if err != nil { + printer.StepWarn(fmt.Sprintf("Could not fold superseded results: %v", err)) + return + } + var fc *forge.IssueComment + if newest != nil { + // A non-numeric ID becomes 0, which minimizes nothing. + id, err := strconv.Atoi(newest.ID) + if err != nil { + id = 0 + } + fc = &forge.IssueComment{ID: id, Author: newest.Author, Body: string(newest.Body), HTMLURL: newest.HTMLURL} + } + supersede.Minimize(ctx, c.forge, owner, repo, number, marker, fc, printer) +} + // MoveIssue implements Mover. Both forge-backed trackers have a native // move — GitHub's GraphQL transferIssue mutation, GitLab's // POST /projects/:id/issues/:iid/move — but neither is exposed on diff --git a/internal/tracker/forge_client_minimize_test.go b/internal/tracker/forge_client_minimize_test.go new file mode 100644 index 0000000000..d3a9e300d8 --- /dev/null +++ b/internal/tracker/forge_client_minimize_test.go @@ -0,0 +1,82 @@ +package tracker + +import ( + "bytes" + "context" + "io" + "slices" + "strings" + "testing" + + "github.com/fullsend-ai/fullsend/internal/forge" + "github.com/fullsend-ai/fullsend/internal/ui" +) + +var _ SupersededMinimizer = (*ForgeClient)(nil) + +const minimizeTestMarker = "" + +func minimizedIDs(fc *forge.FakeClient) []string { + var ids []string + for _, m := range fc.MinimizedComments { + ids = append(ids, m.NodeID) + } + return ids +} + +func TestForgeClient_MinimizeSuperseded_FoldsOwnEarlierComments(t *testing.T) { + fc := forge.NewFakeClient() + fc.AuthenticatedUser = "bot" + fc.IssueComments = map[string][]forge.IssueComment{ + "acme/widgets/1": { + {ID: 100, NodeID: "IC_100", Body: minimizeTestMarker + "\nFirst.", Author: "bot"}, + {ID: 101, NodeID: "IC_101", Body: minimizeTestMarker + "\nPlanted.", Author: "mallory"}, + }, + } + tc := NewForgeClient(fc) + ctx := context.Background() + + created, err := tc.CreateComment(ctx, "acme/widgets", 1, Body(minimizeTestMarker+"\nSecond.")) + if err != nil { + t.Fatalf("CreateComment: %v", err) + } + tc.MinimizeSuperseded(ctx, "acme/widgets", 1, minimizeTestMarker, created, ui.New(io.Discard)) + + if got, want := minimizedIDs(fc), []string{"IC_100"}; !slices.Equal(got, want) { + t.Errorf("minimized = %v, want %v", got, want) + } + if fc.MinimizedComments[0].Reason != "OUTDATED" { + t.Errorf("reason = %q, want OUTDATED", fc.MinimizedComments[0].Reason) + } +} + +func TestForgeClient_MinimizeSuperseded_UnusableNewestMinimizesNothing(t *testing.T) { + for name, newest := range map[string]*Comment{ + "nil comment": nil, + "non-numeric ID": {ID: "abc", Author: "bot"}, + } { + t.Run(name, func(t *testing.T) { + fc := forge.NewFakeClient() + fc.IssueComments = map[string][]forge.IssueComment{ + "acme/widgets/1": {{ID: 100, NodeID: "IC_100", Body: minimizeTestMarker + "\nFirst.", Author: "bot"}}, + } + NewForgeClient(fc).MinimizeSuperseded(context.Background(), "acme/widgets", 1, minimizeTestMarker, newest, ui.New(io.Discard)) + if len(fc.MinimizedComments) != 0 { + t.Errorf("minimized = %v, want none", minimizedIDs(fc)) + } + }) + } +} + +func TestForgeClient_MinimizeSuperseded_InvalidProjectIsBestEffort(t *testing.T) { + fc := forge.NewFakeClient() + var out bytes.Buffer + NewForgeClient(fc).MinimizeSuperseded(context.Background(), "no-slash", 1, minimizeTestMarker, + &Comment{ID: "101", Author: "bot"}, ui.New(&out)) + if len(fc.MinimizedComments) != 0 { + t.Errorf("minimized = %v, want none", minimizedIDs(fc)) + } + if !strings.Contains(out.String(), "Could not fold superseded results") { + t.Errorf("output %q lacks the warning", out.String()) + } +} diff --git a/internal/tracker/tracker.go b/internal/tracker/tracker.go index 2fbcc5c40a..a5ae37860a 100644 --- a/internal/tracker/tracker.go +++ b/internal/tracker/tracker.go @@ -20,6 +20,8 @@ import ( "context" "encoding/json" "errors" + + "github.com/fullsend-ai/fullsend/internal/ui" ) // ErrNotFound indicates a requested issue or comment was not found. @@ -157,6 +159,17 @@ type StatusCommentClient interface { IsStatusComment(ctx context.Context, project string, number int, commentID string) (bool, error) } +// SupersededMinimizer is an optional capability for trackers that can fold +// a poster's earlier result comments once a newer one is posted (#9957). +// ForgeClient implements it; Jira does not, so earlier Jira comments stay +// visible. Callers type-assert their Client and skip it when absent. +type SupersededMinimizer interface { + // MinimizeSuperseded folds the comments on the issue that newest, just + // created by this client, supersedes: same author, body starting with + // marker, posted before newest. It is best effort and logs failures. + MinimizeSuperseded(ctx context.Context, project string, number int, marker string, newest *Comment, printer *ui.Printer) +} + // StatusMarkerPrefix is the marker name shared by every run-status comment // marker (""). It lives here rather // than in internal/statuscomment, which builds the markers, because that