diff --git a/changelog.d/fixed-16348.md b/changelog.d/fixed-16348.md new file mode 100644 index 0000000000..41cbe40e36 --- /dev/null +++ b/changelog.d/fixed-16348.md @@ -0,0 +1 @@ +- fix(prstatus): only treat status comments by this hive's own App login as editable or deletable (#16348) diff --git a/src/pkg/github/attention_test.go b/src/pkg/github/attention_test.go index ba1658dca8..93ad09d9e0 100644 --- a/src/pkg/github/attention_test.go +++ b/src/pkg/github/attention_test.go @@ -42,6 +42,7 @@ func TestAttentionNonAdmitterOnlyFoldsStatus(t *testing.T) { server := &statusCommentServer{} srv := server.start(t) c := testClient(t, srv.URL) + c.SetAppBotLogin("hive-app[bot]") c.SetWritebackGate(writeback.New(writeback.Config{}, writeback.Deps{}, "")) c.SetAttention("other-hive", func() attention.Config { return attention.Config{Admitter: map[string]string{"o/r": "owner-hive"}} }) item := writeback.Item{Number: 5, HiveAuthored: true} diff --git a/src/pkg/github/automerge/automerge_quiet_notifications_test.go b/src/pkg/github/automerge/automerge_quiet_notifications_test.go index c28c45c0c1..5e40513f4a 100644 --- a/src/pkg/github/automerge/automerge_quiet_notifications_test.go +++ b/src/pkg/github/automerge/automerge_quiet_notifications_test.go @@ -180,3 +180,34 @@ func TestQuietNotificationsHuman071MentionOncePerCause(t *testing.T) { t.Fatalf("still one comment expected, got %q", api.comments) } } + +// Own is this hive's App login, not "any bot": a status comment another App +// posted is neither edited nor deleted (a fresh one is created), while one by +// this hive's login is edited in place. +func TestWriteStatusSectionOwnsOnlyThisAppLogin(t *testing.T) { + statusBody := prstatus.Upsert("", prstatus.KeyStageStalled, "old") + for _, tc := range []struct { + name string + login string + wantCreated int + wantEdits int + }{ + {"other bot", "other-app[bot]", 1, 0}, + {"this hive", testHiveAppBotLogin, 0, 1}, + } { + t.Run(tc.name, func(t *testing.T) { + api := &stageAPI{issueComments: map[int][]map[string]any{ + 7: {{"id": 55, "body": statusBody, "user": map[string]any{"login": tc.login, "type": "Bot"}}}, + }} + clock := &contributorLaneClock{at: time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)} + e, _ := quietStageEngine(t, api, clock) + created, err := e.writeStatusSectionWith(context.Background(), "acme", "widget", 7, prstatus.KeyStageStalled, "new", prstatus.Write) + if err != nil { + t.Fatal(err) + } + if created != tc.wantCreated || len(api.commentEdits) != tc.wantEdits { + t.Fatalf("created=%d edits=%d, want created=%d edits=%d", created, len(api.commentEdits), tc.wantCreated, tc.wantEdits) + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 7df7f9a741..3f3e167b73 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1528,12 +1528,14 @@ func (f *engineStatusForge) ListComments(ctx context.Context) ([]prstatus.Commen if err != nil { return nil, err } + // Own is an exact match on this hive's App login; unknown fails closed. + appLogin := strings.TrimSpace(f.e.transport.AppBotLogin()) out := make([]prstatus.Comment, 0, len(comments)) for _, cm := range comments { if cm == nil { continue } - own := strings.TrimSpace(cm.GetUser().GetLogin()) != "" && !f.e.isPersonLogin(cm.GetUser()) + own := appLogin != "" && strings.EqualFold(strings.TrimSpace(cm.GetUser().GetLogin()), appLogin) out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: own}) } return out, nil diff --git a/src/pkg/github/issue_status_card_test.go b/src/pkg/github/issue_status_card_test.go index 53b3a0473e..abac0d075e 100644 --- a/src/pkg/github/issue_status_card_test.go +++ b/src/pkg/github/issue_status_card_test.go @@ -60,6 +60,7 @@ func TestIssueStatusCardPilotFoldsRoutineNotes(t *testing.T) { ctx := context.Background() s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetAttentionPilot(func() bool { return true }) logger := slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil)) noLegacy := func(context.Context) error { @@ -126,6 +127,7 @@ func TestIssueStatusCardAttentionAfterCardExists(t *testing.T) { ctx := context.Background() s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetAttentionPilot(func() bool { return true }) noLegacy := func(context.Context) error { t.Error("legacy write ran with the attention pilot on") @@ -174,6 +176,7 @@ func TestIssueStatusCardAttentionCreatesCardOnce(t *testing.T) { ctx := context.Background() s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetAttentionPilot(func() bool { return true }) noLegacy := func(context.Context) error { t.Error("legacy write ran with the attention pilot on") @@ -202,6 +205,7 @@ func TestIssueStatusCardAttentionGateRefusalRetries(t *testing.T) { ctx := context.Background() s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetAttentionPilot(func() bool { return true }) noLegacy := func(context.Context) error { t.Error("legacy write ran with the attention pilot on") diff --git a/src/pkg/github/notification_fold_test.go b/src/pkg/github/notification_fold_test.go index fc33b4c200..6203f61dbd 100644 --- a/src/pkg/github/notification_fold_test.go +++ b/src/pkg/github/notification_fold_test.go @@ -94,6 +94,7 @@ func TestPostUnblockedNoteIdempotentPerCause(t *testing.T) { t.Run("quiet", func(t *testing.T) { s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetNotificationsQuiet(func() bool { return true }) c.postUnblockedNote(ctx, "o/r", 5, note, logger) c.postUnblockedNote(ctx, "o/r", 5, note, logger) @@ -141,6 +142,7 @@ func TestPostNoteOrFoldQuietEditsNormalCreates(t *testing.T) { ctx := context.Background() s := &statusCommentServer{} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") c.SetNotificationsQuiet(func() bool { return true }) key := prstatus.KeyAgentNotePrefix + agentNoteAwaitingCI if created, err := c.PostNoteOrFold(ctx, "o/r", 3, key, "hive/awaiting-ci: CI pending — sweep will merge when green."); err != nil || !created { diff --git a/src/pkg/github/pr_status_comment.go b/src/pkg/github/pr_status_comment.go index e5e3e8f188..d8cc3ec806 100644 --- a/src/pkg/github/pr_status_comment.go +++ b/src/pkg/github/pr_status_comment.go @@ -3,6 +3,7 @@ package github import ( "context" "fmt" + "strings" gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/prstatus" @@ -29,6 +30,15 @@ func (c *Client) UpsertPRStatusSection(ctx context.Context, repo string, number return res.Created, err } +// ownStatusCommentLogin reports whether login is exactly this hive's App bot +// login. Any other account, including another bot, is not the owner, and an +// unknown App login fails closed so nothing is ever edited or deleted. +func (c *Client) ownStatusCommentLogin(login string) bool { + login = strings.TrimSpace(login) + app := strings.TrimSpace(c.appBotLogin) + return login != "" && app != "" && strings.EqualFold(login, app) +} + // clientStatusForge adapts the client's comment calls on one item to // prstatus.Forge. Creates go through CreateIssueComment (canary gate, // effects journal, created tally); edits are tallied as edited. @@ -49,7 +59,7 @@ func (f *clientStatusForge) ListComments(ctx context.Context) ([]prstatus.Commen if cm == nil { continue } - out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: f.c.hiveAuthoredLogin(cm.GetUser().GetLogin())}) + out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: f.c.ownStatusCommentLogin(cm.GetUser().GetLogin())}) } return out, nil } diff --git a/src/pkg/github/pr_status_comment_test.go b/src/pkg/github/pr_status_comment_test.go index 40ffd15f28..b235a3c421 100644 --- a/src/pkg/github/pr_status_comment_test.go +++ b/src/pkg/github/pr_status_comment_test.go @@ -94,6 +94,7 @@ func TestUpsertPRStatusSectionCreatesOnceThenEdits(t *testing.T) { s := &statusCommentServer{} srv := s.start(t) c := testClient(t, srv.URL) + c.SetAppBotLogin("hive-app[bot]") ctx := context.Background() created, err := c.UpsertPRStatusSection(ctx, "o/r", 5, prstatus.KeyStageStalled, "stalled at 080") @@ -140,6 +141,7 @@ func TestUpsertPRStatusSectionMergesDuplicateStatusComments(t *testing.T) { {"id": 12, "body": prstatus.Upsert("", prstatus.KeyStageRegressed, "regressed"), "user": bot}, }} c := testClient(t, s.start(t).URL) + c.SetAppBotLogin("hive-app[bot]") created, err := c.UpsertPRStatusSection(context.Background(), "o/r", 5, prstatus.KeyEvidence, "evidence") if err != nil || created { t.Fatalf("created=%v err=%v", created, err) @@ -164,12 +166,38 @@ func TestUpsertPRStatusSectionMergesDuplicateStatusComments(t *testing.T) { } } +// Own is this hive's App login, not "any bot": another App's status comment +// is never edited or deleted, and an unknown App login fails closed. +func TestUpsertPRStatusSectionIgnoresOtherBotsComments(t *testing.T) { + for _, tc := range []struct{ name, appLogin string }{ + {"other bot", "hive-app[bot]"}, + {"unknown own login", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + other := map[string]any{"login": "other-app[bot]", "type": "Bot"} + s := &statusCommentServer{comments: []map[string]any{ + {"id": 11, "body": prstatus.Upsert("", prstatus.KeyStageStalled, "stalled"), "user": other}, + }} + c := testClient(t, s.start(t).URL) + c.SetAppBotLogin(tc.appLogin) + created, err := c.UpsertPRStatusSection(context.Background(), "o/r", 5, prstatus.KeyEvidence, "evidence") + if err != nil || !created { + t.Fatalf("created=%v err=%v, want a fresh comment", created, err) + } + if s.edits != 0 || len(s.deletes) != 0 { + t.Fatalf("edits=%d deletes=%v, want the other bot's comment untouched", s.edits, s.deletes) + } + }) + } +} + // Quiet mode: the duplicate-guard redirect lands in the status comment of // the PR it points at and a retry edits it rather than stacking comments. func TestCreatePR_DuplicateGuardQuietModeUsesStatusComment(t *testing.T) { g := &guardServer{open: []guardPR{{Number: 7, Ref: "a", Body: "Fixes #42", User: "human"}}} srv := g.start(t) c := NewClientForTest(srv.URL, "o", nil, prTestLogger()) + c.SetAppBotLogin("hive-app[bot]") c.SetNotificationsQuiet(func() bool { return true }) res, err := c.CreatePR(context.Background(), "o/r", "new-branch", "main", "title", "Closes #42") if err != nil || res.Number != 7 || res.DuplicateReason != DuplicateReasonClosesIssue { diff --git a/src/pkg/github/review_evidence_read_test.go b/src/pkg/github/review_evidence_read_test.go index 4bd06cb43b..0492420bd6 100644 --- a/src/pkg/github/review_evidence_read_test.go +++ b/src/pkg/github/review_evidence_read_test.go @@ -294,6 +294,8 @@ func newEvidenceMergeServer(t *testing.T, f *evidenceMergeFixture) *httptest.Ser func evidenceMergeClient(t *testing.T, f *evidenceMergeFixture, settings *ReviewEvidenceSettings) *Client { t.Helper() c := newTestClient(t, newEvidenceMergeServer(t, f), "o", []string{"o/r"}) + // The fixture stamps comments as hive-app[bot]; make the client own them. + c.SetAppBotLogin("hive-app[bot]") if settings != nil { s := *settings c.SetReviewEvidence(func(string) ReviewEvidenceSettings { return s })