diff --git a/changelog.d/fixed-16263-single-status-writer.md b/changelog.d/fixed-16263-single-status-writer.md new file mode 100644 index 0000000000..9ef179f6b2 --- /dev/null +++ b/changelog.d/fixed-16263-single-status-writer.md @@ -0,0 +1 @@ +- All hive:status comment writers go through one serialised writer with compare-and-retry and duplicate reconciliation, so concurrent writers no longer lose sections or create duplicate status comments (#16263). diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 1b7f6f9741..a4b5f3be5f 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1476,11 +1476,7 @@ func regressionNotable(reason string) bool { // as a comment posted. Returns the number of comments created. func (c *Engine) postStageNote(ctx context.Context, policy StageLabelPolicy, obs stageObservation, key, body string) (int, error) { if policy.QuietNotifications { - done, fresh, err := c.editStatusSection(ctx, obs.Owner, obs.Repo, obs.Number, key, body) - if err != nil || done { - return 0, err - } - body = fresh + return c.writeStatusSection(ctx, obs.Owner, obs.Repo, obs.Number, key, body) } if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { return 0, err @@ -1488,29 +1484,62 @@ func (c *Engine) postStageNote(ctx context.Context, policy StageLabelPolicy, obs return 1, nil } -// editStatusSection rewrites section key of the PR's hive status comment -// (prstatus.Marker) to text when that comment exists, and reports done. When -// the PR has no status comment yet nothing is written: the caller creates -// one from the returned body, so the create stays a single call site. -func (c *Engine) editStatusSection(ctx context.Context, owner, repo string, number int, key, text string) (done bool, fresh string, err error) { - comments, err := c.listIssueComments(ctx, owner, repo, number) +// writeStatusSection rewrites section key of the PR's hive status comment +// to text through prstatus.Write, the one status writer +// the GitHub client shares: it is serialised per PR, the edit is retried +// when the comment changed underneath, and the comment is created only when +// the PR has none. Returns the number of comments created. +func (c *Engine) writeStatusSection(ctx context.Context, owner, repo string, number int, key, text string) (int, error) { + f := &engineStatusForge{e: c, owner: owner, repo: repo, number: number} + res, err := prstatus.Write(ctx, f, prstatus.ItemKey(owner+"/"+repo, number), key, text) + if res.Created { + return 1, err + } + return 0, err +} + +// engineStatusForge adapts the engine's comment calls on one PR to +// prstatus.Forge. +type engineStatusForge struct { + e *Engine + owner string + repo string + number int +} + +func (f *engineStatusForge) ListComments(ctx context.Context) ([]prstatus.Comment, error) { + comments, err := f.e.listIssueComments(ctx, f.owner, f.repo, f.number) if err != nil { - return false, "", err + return nil, err } + out := make([]prstatus.Comment, 0, len(comments)) for _, cm := range comments { - if cm == nil || !prstatus.IsStatusComment(cm.GetBody()) { + if cm == nil { continue } - next := prstatus.Upsert(cm.GetBody(), key, text) - if next == cm.GetBody() { - return true, "", nil - } - if _, _, err := c.gh.Issues.EditComment(ctx, owner, repo, cm.GetID(), &gh.IssueComment{Body: gh.Ptr(next)}); err != nil { - return true, "", fmt.Errorf("editing hive status comment on %s/%s#%d: %w", owner, repo, number, err) - } - return true, "", nil + own := strings.TrimSpace(cm.GetUser().GetLogin()) != "" && !f.e.isPersonLogin(cm.GetUser()) + out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: own}) + } + return out, nil +} + +func (f *engineStatusForge) EditComment(ctx context.Context, id int64, body string) error { + if _, _, err := f.e.gh.Issues.EditComment(ctx, f.owner, f.repo, id, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + return fmt.Errorf("editing hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err) } - return false, prstatus.Upsert("", key, text), nil + return nil +} + +func (f *engineStatusForge) CreateComment(ctx context.Context, body string) error { + _, _, err := f.e.gh.Issues.CreateComment(ctx, f.owner, f.repo, f.number, &gh.IssueComment{Body: gh.Ptr(body)}) + return err +} + +func (f *engineStatusForge) DeleteComment(ctx context.Context, id int64) error { + if _, err := f.e.gh.Issues.DeleteComment(ctx, f.owner, f.repo, id); err != nil { + return fmt.Errorf("deleting hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err) + } + return nil } // ensureStageLabels provisions (or recolours) the family's labels in a repo diff --git a/src/pkg/github/pr_status_comment.go b/src/pkg/github/pr_status_comment.go index e1ea1587dc..e78da1ad29 100644 --- a/src/pkg/github/pr_status_comment.go +++ b/src/pkg/github/pr_status_comment.go @@ -2,37 +2,75 @@ package github import ( "context" - "strings" + "fmt" + gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/prstatus" ) // UpsertPRStatusSection rewrites section key of repo#number's single hive -// status comment (prstatus.Marker) to text, creating the comment only when +// status comment to text, creating the comment only when // the PR has none. Editing a comment notifies nobody; creating one emails // the author and every watcher, so this is the quiet-mode delivery for // every note the hive used to post as its own comment. An empty text // removes the section. Reports whether a comment was created. +// +// The write goes through prstatus.Write, the one status writer the +// auto-merge engine shares, so it is serialised per item, retried when the +// comment changed between read and edit, and duplicate status comments are +// merged into the oldest. func (c *Client) UpsertPRStatusSection(ctx context.Context, repo string, number int, key, text string) (bool, error) { if c == nil { return false, ErrNoGitHubClient } - current, err := c.UpsertIssueMarkerComment(ctx, repo, number, prstatus.Marker, "", false) + owner, repoName := c.splitRepo(repo) + f := &clientStatusForge{c: c, owner: owner, repo: repoName, number: number} + res, err := prstatus.Write(ctx, f, prstatus.ItemKey(owner+"/"+repoName, number), key, text) + return res.Created, err +} + +// 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. +type clientStatusForge struct { + c *Client + owner string + repo string + number int +} + +func (f *clientStatusForge) ListComments(ctx context.Context) ([]prstatus.Comment, error) { + comments, err := f.c.listIssueComments(ctx, f.owner, f.repo, f.number) if err != nil { - return false, err + return nil, err } - if !current.Found && strings.TrimSpace(text) == "" { - return false, nil + out := make([]prstatus.Comment, 0, len(comments)) + for _, cm := range comments { + if cm == nil { + continue + } + out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: f.c.hiveAuthoredLogin(cm.GetUser().GetLogin())}) } - next := prstatus.Upsert(current.Previous, key, text) - if current.Found && next == current.Previous { - return false, nil + return out, nil +} + +func (f *clientStatusForge) EditComment(ctx context.Context, id int64, body string) error { + if _, _, err := f.c.client.Issues.EditComment(ctx, f.owner, f.repo, id, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + return fmt.Errorf("editing hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err) } - res, err := c.UpsertIssueMarkerComment(ctx, repo, number, prstatus.Marker, next, true) - if err != nil { - return false, err + f.c.noteCommentEdited() + return nil +} + +func (f *clientStatusForge) CreateComment(ctx context.Context, body string) error { + return f.c.CreateIssueComment(ctx, f.owner+"/"+f.repo, f.number, body) +} + +func (f *clientStatusForge) DeleteComment(ctx context.Context, id int64) error { + if _, err := f.c.client.Issues.DeleteComment(ctx, f.owner, f.repo, id); err != nil { + return fmt.Errorf("deleting hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err) } - return res.Created, nil + return nil } // PostNoteOrFold delivers a hive note on repo#number. In quiet mode diff --git a/src/pkg/github/pr_status_comment_test.go b/src/pkg/github/pr_status_comment_test.go index 8ded938bf6..947a98d03e 100644 --- a/src/pkg/github/pr_status_comment_test.go +++ b/src/pkg/github/pr_status_comment_test.go @@ -23,6 +23,7 @@ type statusCommentServer struct { comments []map[string]any creates int edits int + deletes []string } func (s *statusCommentServer) start(t *testing.T) *httptest.Server { @@ -52,6 +53,17 @@ func (s *statusCommentServer) start(t *testing.T) *httptest.Server { } } _ = json.NewEncoder(w).Encode(map[string]any{"id": 1}) + case r.Method == http.MethodDelete && strings.Contains(r.URL.Path, "/issues/comments/"): + id := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:] + s.deletes = append(s.deletes, id) + kept := s.comments[:0] + for _, cm := range s.comments { + if jsonNumber(cm["id"]) != id { + kept = append(kept, cm) + } + } + s.comments = kept + w.WriteHeader(http.StatusNoContent) default: w.WriteHeader(http.StatusNotFound) } @@ -107,6 +119,41 @@ func TestUpsertPRStatusSectionCreatesOnceThenEdits(t *testing.T) { } } +// A PR carrying two hive status comments (two writers both created one) +// is reconciled on the next write: every section lands in the oldest, the +// newer hive-authored copy is deleted, and the edit is tallied as an edit, +// not a created comment. +func TestUpsertPRStatusSectionMergesDuplicateStatusComments(t *testing.T) { + bot := map[string]any{"login": "hive-app[bot]", "type": "Bot"} + s := &statusCommentServer{comments: []map[string]any{ + {"id": 11, "body": prstatus.Upsert("", prstatus.KeyStageStalled, "stalled"), "user": bot}, + {"id": 12, "body": prstatus.Upsert("", prstatus.KeyStageRegressed, "regressed"), "user": bot}, + }} + c := testClient(t, s.start(t).URL) + created, err := c.UpsertPRStatusSection(context.Background(), "o/r", 5, prstatus.KeyEvidence, "evidence") + if err != nil || created { + t.Fatalf("created=%v err=%v", created, err) + } + if s.creates != 0 || len(s.deletes) != 1 || s.deletes[0] != "12" || len(s.comments) != 1 { + t.Fatalf("creates=%d deletes=%v comments=%d, want the newer status comment deleted", s.creates, s.deletes, len(s.comments)) + } + body, _ := s.comments[0]["body"].(string) + for _, key := range []string{prstatus.KeyStageStalled, prstatus.KeyStageRegressed, prstatus.KeyEvidence} { + n := 0 + for _, sec := range prstatus.Parse(body) { + if sec.Key == key { + n++ + } + } + if n != 1 { + t.Fatalf("section %q appears %d times in %q", key, n, body) + } + } + if created, edited, _ := c.NotificationCounts(); created != 0 || edited != 2 { + t.Fatalf("tally created=%d edited=%d, want 0 created and 2 edits", created, edited) + } +} + // 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) { diff --git a/src/pkg/prstatus/prstatus.go b/src/pkg/prstatus/prstatus.go index f1d29d1fc3..4053f7426a 100644 --- a/src/pkg/prstatus/prstatus.go +++ b/src/pkg/prstatus/prstatus.go @@ -4,8 +4,11 @@ // nothing. So instead of one comment per note (stage stalled, stage // regressed, the trusted-human 071 note, the duplicate-PR redirect, …) the // hive keeps a single comment tagged Marker and rewrites one keyed section -// of it in place. The package is pure string handling; the GitHub client -// and the auto-merge engine each own the list/edit/create calls around it. +// of it in place. Write (writer.go) is the one path every writer goes +// through: the GitHub client and the auto-merge engine each adapt their +// list/edit/create/delete calls to its Forge, and Write serialises them +// per item, retries an edit whose base changed underneath, and merges +// duplicate status comments into the oldest. package prstatus import ( diff --git a/src/pkg/prstatus/writer.go b/src/pkg/prstatus/writer.go new file mode 100644 index 0000000000..fc50450a1b --- /dev/null +++ b/src/pkg/prstatus/writer.go @@ -0,0 +1,264 @@ +package prstatus + +import ( + "context" + "errors" + "fmt" + "sort" + "strings" + "sync" +) + +// Comment is one issue/PR comment as the status writer sees it. +type Comment struct { + ID int64 + Body string + // Own reports the hive authored the comment. Only an Own duplicate is + // ever deleted by reconciliation; a status marker pasted by a person + // is merged from but left in place. + Own bool +} + +// Forge is the comment API the status writer drives for one item. The +// GitHub client and the auto-merge engine each adapt their own transport to +// it, so both go through Write and share its per-item serialisation. +type Forge interface { + ListComments(ctx context.Context) ([]Comment, error) + EditComment(ctx context.Context, id int64, body string) error + CreateComment(ctx context.Context, body string) error + DeleteComment(ctx context.Context, id int64) error +} + +// WriteResult reports what Write changed on GitHub. +type WriteResult struct { + // Created is set when Write posted a new status comment (the only + // write that notifies anyone). + Created bool + // Edited is set when Write edited a status comment in place. + Edited bool + // Deleted counts duplicate status comments removed by reconciliation. + Deleted int +} + +// MaxWriteAttempts bounds the compare-and-retry loop: a status comment that +// keeps changing between read and edit this many times is reported as +// ErrStatusConflict rather than overwritten. +const MaxWriteAttempts = 4 + +var ( + // ErrStatusConflict means the status comment changed under every + // attempt to edit it. + ErrStatusConflict = errors.New("hive status comment kept changing between read and edit") + // ErrStatusCommentGone means the status comment disappeared between + // read and edit. + ErrStatusCommentGone = errors.New("hive status comment disappeared between read and edit") +) + +// Write is the single authoritative hive:status writer: it sets section key +// of item's status comment to text (an empty text removes it), creating the +// comment only when the item has none. item names the issue or PR +// ("owner/repo#number") and keys the per-process lock every writer shares, +// so two writers on one item never interleave their read and edit. +// +// Every edit is compare-and-retry: the comment is re-read immediately +// before the edit, and when its body is no longer the one the section was +// computed from the section is recomputed on the fresh body, so a section +// another writer (or another hive) added in between survives. After a +// create, and whenever more than one status comment is found, the +// duplicates are reconciled: their sections are merged into the oldest and +// the newer hive-authored copies deleted. +func Write(ctx context.Context, f Forge, item, key, text string) (WriteResult, error) { + unlock := lockItem(item) + defer unlock() + var res WriteResult + status, err := listStatusComments(ctx, f) + if err != nil { + return res, err + } + if len(status) == 0 { + if strings.TrimSpace(text) == "" { + return res, nil + } + if err := f.CreateComment(ctx, Upsert("", key, text)); err != nil { + return res, err + } + res.Created = true + err := reconcile(ctx, f, &res) + return res, err + } + if len(status) > 1 { + if err := reconcile(ctx, f, &res); err != nil { + return res, err + } + if status, err = listStatusComments(ctx, f); err != nil { + return res, err + } + if len(status) == 0 { + return res, ErrStatusCommentGone + } + } + target := status[0] + edited, err := editCompareAndRetry(ctx, f, target.ID, target.Body, func(cur string) string { + return Upsert(cur, key, text) + }) + res.Edited = res.Edited || edited + return res, err +} + +// Reconcile merges every status comment on the item into the oldest and +// deletes the newer hive-authored copies, under the same per-item lock as +// Write. It is a no-op when the item has at most one status comment. +func Reconcile(ctx context.Context, f Forge, item string) (WriteResult, error) { + unlock := lockItem(item) + defer unlock() + var res WriteResult + err := reconcile(ctx, f, &res) + return res, err +} + +func reconcile(ctx context.Context, f Forge, res *WriteResult) error { + status, err := listStatusComments(ctx, f) + if err != nil || len(status) < 2 { + return err + } + oldest, newer := status[0], status[1:] + others := make([]string, 0, len(newer)) + for _, c := range newer { + others = append(others, c.Body) + } + edited, err := editCompareAndRetry(ctx, f, oldest.ID, oldest.Body, func(cur string) string { + return Merge(append([]string{cur}, others...)...) + }) + res.Edited = res.Edited || edited + if err != nil { + return err + } + for _, c := range newer { + if !c.Own { + continue + } + if err := f.DeleteComment(ctx, c.ID); err != nil { + return fmt.Errorf("deleting duplicate hive status comment %d: %w", c.ID, err) + } + res.Deleted++ + } + return nil +} + +// editCompareAndRetry edits comment id to compute(body), where body is the +// comment as last read. Immediately before each edit the comment is read +// again; if it changed, compute runs on the fresh body and the edit is +// retried, up to MaxWriteAttempts reads. +func editCompareAndRetry(ctx context.Context, f Forge, id int64, base string, compute func(string) string) (bool, error) { + for attempt := 1; ; attempt++ { + next := compute(base) + if next == base { + return false, nil + } + fresh, found, err := readComment(ctx, f, id) + if err != nil { + return false, err + } + if !found { + return false, ErrStatusCommentGone + } + if fresh == base { + if err := f.EditComment(ctx, id, next); err != nil { + return false, err + } + return true, nil + } + if attempt >= MaxWriteAttempts { + return false, ErrStatusConflict + } + base = fresh + } +} + +func readComment(ctx context.Context, f Forge, id int64) (string, bool, error) { + comments, err := f.ListComments(ctx) + if err != nil { + return "", false, err + } + for _, c := range comments { + if c.ID == id { + return c.Body, true, nil + } + } + return "", false, nil +} + +// listStatusComments returns the item's status comments, oldest first. +// GitHub comment IDs increase with creation time. +func listStatusComments(ctx context.Context, f Forge) ([]Comment, error) { + comments, err := f.ListComments(ctx) + if err != nil { + return nil, err + } + var out []Comment + for _, c := range comments { + if IsStatusComment(c.Body) { + out = append(out, c) + } + } + sort.SliceStable(out, func(i, j int) bool { return out[i].ID < out[j].ID }) + return out, nil +} + +// Merge renders one status body holding every section of bodies, each key +// exactly once. bodies are oldest first and the first occurrence of a key +// wins: the oldest comment is the one every pass edits, so its copy of a +// section is the current one. +func Merge(bodies ...string) string { + seen := map[string]bool{} + var out []Section + for _, b := range bodies { + for _, s := range Parse(b) { + if seen[s.Key] { + continue + } + seen[s.Key] = true + out = append(out, s) + } + } + return Render(out) +} + +// ItemKey is the lock key Write uses for repo#number. +func ItemKey(repo string, number int) string { + return fmt.Sprintf("%s#%d", strings.ToLower(strings.TrimSpace(repo)), number) +} + +type itemLock struct { + mu sync.Mutex + refs int +} + +var itemLocks = struct { + mu sync.Mutex + m map[string]*itemLock +}{m: map[string]*itemLock{}} + +// lockItem serialises status writes per item within the process and +// returns the unlock. Entries are dropped once no writer holds or waits on +// them, so the table stays as small as the set of items being written. +func lockItem(item string) func() { + itemLocks.mu.Lock() + l := itemLocks.m[item] + if l == nil { + l = &itemLock{} + itemLocks.m[item] = l + } + l.refs++ + itemLocks.mu.Unlock() + l.mu.Lock() + return func() { + l.mu.Unlock() + itemLocks.mu.Lock() + l.refs-- + if l.refs == 0 { + delete(itemLocks.m, item) + } + itemLocks.mu.Unlock() + } +} diff --git a/src/pkg/prstatus/writer_errors_test.go b/src/pkg/prstatus/writer_errors_test.go new file mode 100644 index 0000000000..cad213d3e0 --- /dev/null +++ b/src/pkg/prstatus/writer_errors_test.go @@ -0,0 +1,269 @@ +package prstatus + +import ( + "context" + "errors" + "strings" + "testing" +) + +// errForge fails the calls a test names. listErr gets the 1-based +// ListComments call number; the other errors fail every such call. +type errForge struct { + *fakeForge + listCalls int + listErr func(n int) error + createErr error + editErr error + deleteErr error +} + +func (e *errForge) ListComments(ctx context.Context) ([]Comment, error) { + e.listCalls++ + if e.listErr != nil { + if err := e.listErr(e.listCalls); err != nil { + return nil, err + } + } + return e.fakeForge.ListComments(ctx) +} + +func (e *errForge) EditComment(ctx context.Context, id int64, body string) error { + if e.editErr != nil { + return e.editErr + } + return e.fakeForge.EditComment(ctx, id, body) +} + +func (e *errForge) CreateComment(ctx context.Context, body string) error { + if e.createErr != nil { + return e.createErr + } + return e.fakeForge.CreateComment(ctx, body) +} + +func (e *errForge) DeleteComment(ctx context.Context, id int64) error { + if e.deleteErr != nil { + return e.deleteErr + } + return e.fakeForge.DeleteComment(ctx, id) +} + +var errBoom = errors.New("boom") + +func failListAt(target int) func(int) error { + return func(n int) error { + if n == target { + return errBoom + } + return nil + } +} + +func TestParseEdgeCases(t *testing.T) { + cases := []struct { + name string + body string + want []Section + }{ + {"no sections", Marker + "\n" + Heading, nil}, + {"open without suffix", sectionOpenPrefix + "key", nil}, + {"unterminated section keeps rest", sectionOpenPrefix + " k " + sectionOpenSuffix + "\n body \n", []Section{{Key: "k", Body: "body"}}}, + {"two sections with noise between", "x" + sectionOpenPrefix + "a" + sectionOpenSuffix + "1" + sectionClose + "junk" + + sectionOpenPrefix + "b" + sectionOpenSuffix + "2" + sectionClose, []Section{{Key: "a", Body: "1"}, {Key: "b", Body: "2"}}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := Parse(tc.body) + if len(got) != len(tc.want) { + t.Fatalf("Parse = %+v, want %+v", got, tc.want) + } + for i := range got { + if got[i] != tc.want[i] { + t.Fatalf("Parse[%d] = %+v, want %+v", i, got[i], tc.want[i]) + } + } + }) + } +} + +func TestGetMissingSection(t *testing.T) { + body := Upsert("", "a", "x") + if got, ok := Get(body, "nope"); ok || got != "" { + t.Fatalf("Get = %q, %v", got, ok) + } + if IsStatusComment("plain") { + t.Fatal("plain text is not a status comment") + } +} + +func TestUpsertCollapsesDuplicateKeys(t *testing.T) { + dup := Render([]Section{{Key: "a", Body: "one"}, {Key: "b", Body: "b"}, {Key: "a", Body: "two"}}) + body := Upsert(dup, "a", "new") + if sectionCount(body, "a") != 1 { + t.Fatalf("duplicate key survived:\n%s", body) + } + if got, _ := Get(body, "a"); got != "new" { + t.Fatalf("a = %q", got) + } + body = Upsert(dup, "a", "") + if sectionCount(body, "a") != 0 || sectionCount(body, "b") != 1 { + t.Fatalf("empty text did not remove every copy:\n%s", body) + } +} + +func TestMergeFirstOccurrenceWins(t *testing.T) { + got := Merge(Upsert(Upsert("", "a", "old"), "b", "b"), Upsert(Upsert("", "a", "new"), "c", "c"), "no sections") + for _, k := range []string{"a", "b", "c"} { + if sectionCount(got, k) != 1 { + t.Fatalf("section %q not exactly once:\n%s", k, got) + } + } + if v, _ := Get(got, "a"); v != "old" { + t.Fatalf("a = %q, want the first copy", v) + } +} + +func TestItemKeyNormalises(t *testing.T) { + if got := ItemKey(" Owner/Repo ", 12); got != "owner/repo#12" { + t.Fatalf("ItemKey = %q", got) + } +} + +func TestWriteNoChangeDoesNotEdit(t *testing.T) { + f := &fakeForge{} + f.add(Upsert("", "a", "same"), true) + res, err := Write(context.Background(), f, ItemKey("o/r", 20), "a", "same") + if err != nil || res != (WriteResult{}) || f.edits != 0 { + t.Fatalf("Write = %+v, %v, edits=%d", res, err, f.edits) + } +} + +func TestWriteReleasesItemLock(t *testing.T) { + item := ItemKey("o/r", 21) + f := &fakeForge{} + if _, err := Write(context.Background(), f, item, "a", "x"); err != nil { + t.Fatal(err) + } + itemLocks.mu.Lock() + _, held := itemLocks.m[item] + itemLocks.mu.Unlock() + if held { + t.Fatal("lock table entry not dropped after Write") + } +} + +func TestWriteErrorPaths(t *testing.T) { + two := func(f *fakeForge) { + f.add(Upsert("", "a", "a"), true) + f.add(Upsert("", "b", "b"), true) + } + one := func(f *fakeForge) { f.add(Upsert("", "a", "old"), true) } + cases := []struct { + name string + seed func(*fakeForge) + setup func(*errForge) + want error + }{ + {"first list fails", one, func(e *errForge) { e.listErr = failListAt(1) }, errBoom}, + {"create fails", func(*fakeForge) {}, func(e *errForge) { e.createErr = errBoom }, errBoom}, + {"post-create reconcile list fails", func(*fakeForge) {}, func(e *errForge) { e.listErr = failListAt(2) }, errBoom}, + {"pre-edit re-read fails", one, func(e *errForge) { e.listErr = failListAt(2) }, errBoom}, + {"edit fails", one, func(e *errForge) { e.editErr = errBoom }, errBoom}, + {"comment gone before edit", one, func(e *errForge) { + e.beforeList = func(n int, f *fakeForge) { + if n == 2 { + f.comments = nil + } + } + }, ErrStatusCommentGone}, + {"duplicate reconcile list fails", two, func(e *errForge) { e.listErr = failListAt(2) }, errBoom}, + {"duplicate reconcile re-read fails", two, func(e *errForge) { e.listErr = failListAt(3) }, errBoom}, + {"duplicate reconcile edit fails", two, func(e *errForge) { e.editErr = errBoom }, errBoom}, + {"duplicate delete fails", two, func(e *errForge) { e.deleteErr = errBoom }, errBoom}, + {"list after reconcile fails", two, func(e *errForge) { e.listErr = failListAt(4) }, errBoom}, + {"status comment vanishes after reconcile", two, func(e *errForge) { + e.beforeList = func(n int, f *fakeForge) { + if n == 4 { + f.comments = nil + } + } + }, ErrStatusCommentGone}, + } + for i, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + e := &errForge{fakeForge: &fakeForge{}} + tc.seed(e.fakeForge) + tc.setup(e) + _, err := Write(context.Background(), e, ItemKey("o/r", 100+i), "k", "v") + if !errors.Is(err, tc.want) { + t.Fatalf("err = %v, want %v", err, tc.want) + } + }) + } +} + +func TestWriteDeleteErrorNamesComment(t *testing.T) { + e := &errForge{fakeForge: &fakeForge{}, deleteErr: errBoom} + e.add(Upsert("", "a", "a"), true) + dup := e.add(Upsert("", "b", "b"), true) + _, err := Write(context.Background(), e, ItemKey("o/r", 30), "c", "c") + if err == nil || !errors.Is(err, errBoom) || !strings.Contains(err.Error(), "duplicate hive status comment") { + t.Fatalf("err = %v", err) + } + if !strings.Contains(err.Error(), "2") || dup != 2 { + t.Fatalf("error should name comment %d: %v", dup, err) + } +} + +func TestReconcile(t *testing.T) { + t.Run("none or one is a no-op", func(t *testing.T) { + f := &fakeForge{} + f.add("unrelated", false) + res, err := Reconcile(context.Background(), f, ItemKey("o/r", 40)) + if err != nil || res != (WriteResult{}) { + t.Fatalf("Reconcile = %+v, %v", res, err) + } + f.add(Upsert("", "a", "a"), true) + res, err = Reconcile(context.Background(), f, ItemKey("o/r", 40)) + if err != nil || res != (WriteResult{}) || f.edits != 0 { + t.Fatalf("Reconcile = %+v, %v, edits=%d", res, err, f.edits) + } + }) + t.Run("merges and deletes own duplicates only", func(t *testing.T) { + f := &fakeForge{} + f.add(Upsert("", "a", "a"), true) + f.add(Upsert("", "b", "b"), true) + f.add(Upsert("", "c", "c"), false) + res, err := Reconcile(context.Background(), f, ItemKey("o/r", 41)) + if err != nil || !res.Edited || res.Deleted != 1 || res.Created { + t.Fatalf("Reconcile = %+v, %v", res, err) + } + if len(f.comments) != 2 { + t.Fatalf("comments left = %d, want 2", len(f.comments)) + } + for _, k := range []string{"a", "b", "c"} { + if sectionCount(f.comments[0].Body, k) != 1 { + t.Fatalf("section %q not merged into the oldest:\n%s", k, f.comments[0].Body) + } + } + }) + t.Run("list error", func(t *testing.T) { + e := &errForge{fakeForge: &fakeForge{}, listErr: failListAt(1)} + if _, err := Reconcile(context.Background(), e, ItemKey("o/r", 42)); !errors.Is(err, errBoom) { + t.Fatalf("err = %v", err) + } + }) + t.Run("conflict leaves duplicates alone", func(t *testing.T) { + f := &fakeForge{} + f.add(Upsert("", "a", "a"), true) + f.add(Upsert("", "b", "b"), true) + f.beforeList = func(n int, f *fakeForge) { + f.comments[0].Body = Upsert(f.comments[0].Body, "churn", strings.Repeat("x", n)) + } + res, err := Reconcile(context.Background(), f, ItemKey("o/r", 43)) + if !errors.Is(err, ErrStatusConflict) || res.Deleted != 0 || len(f.deleted) != 0 { + t.Fatalf("Reconcile = %+v, %v", res, err) + } + }) +} diff --git a/src/pkg/prstatus/writer_test.go b/src/pkg/prstatus/writer_test.go new file mode 100644 index 0000000000..e440e637e3 --- /dev/null +++ b/src/pkg/prstatus/writer_test.go @@ -0,0 +1,381 @@ +package prstatus + +import ( + "context" + "errors" + "io/fs" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" +) + +// fakeForge keeps one item's comments in memory. beforeList runs before +// every ListComments (with the 1-based call number) so a test can change +// the item between the writer's read and its edit; inFlight/overlap prove +// the per-item lock keeps two writers from interleaving. +type fakeForge struct { + mu sync.Mutex + comments []Comment + nextID int64 + lists int + edits int + creates int + deleted []int64 + + beforeList func(n int, f *fakeForge) + beforeCreate func(f *fakeForge) + + inFlight atomic.Int32 + overlap atomic.Bool + pause time.Duration +} + +func (f *fakeForge) enter() func() { + if f.inFlight.Add(1) > 1 { + f.overlap.Store(true) + } + if f.pause > 0 { + <-time.After(f.pause) + } + return func() { f.inFlight.Add(-1) } +} + +func (f *fakeForge) add(body string, own bool) int64 { + f.nextID++ + f.comments = append(f.comments, Comment{ID: f.nextID, Body: body, Own: own}) + return f.nextID +} + +func (f *fakeForge) ListComments(ctx context.Context) ([]Comment, error) { + defer f.enter()() + f.mu.Lock() + defer f.mu.Unlock() + f.lists++ + if f.beforeList != nil { + f.beforeList(f.lists, f) + } + return append([]Comment(nil), f.comments...), nil +} + +func (f *fakeForge) EditComment(ctx context.Context, id int64, body string) error { + defer f.enter()() + f.mu.Lock() + defer f.mu.Unlock() + for i := range f.comments { + if f.comments[i].ID == id { + f.comments[i].Body = body + f.edits++ + return nil + } + } + return errors.New("no such comment") +} + +func (f *fakeForge) CreateComment(ctx context.Context, body string) error { + defer f.enter()() + f.mu.Lock() + defer f.mu.Unlock() + if f.beforeCreate != nil { + f.beforeCreate(f) + } + f.creates++ + f.add(body, true) + return nil +} + +func (f *fakeForge) DeleteComment(ctx context.Context, id int64) error { + defer f.enter()() + f.mu.Lock() + defer f.mu.Unlock() + for i := range f.comments { + if f.comments[i].ID == id { + f.comments = append(f.comments[:i], f.comments[i+1:]...) + f.deleted = append(f.deleted, id) + return nil + } + } + return errors.New("no such comment") +} + +func (f *fakeForge) statusBodies() []string { + f.mu.Lock() + defer f.mu.Unlock() + var out []string + for _, c := range f.comments { + if IsStatusComment(c.Body) { + out = append(out, c.Body) + } + } + return out +} + +func sectionCount(body, key string) int { + n := 0 + for _, s := range Parse(body) { + if s.Key == key { + n++ + } + } + return n +} + +// Two writers upserting different sections of one item at the same time +// both land, in one status comment, and their calls never interleave. +func TestWriteConcurrentWritersDifferentKeysBothSurvive(t *testing.T) { + f := &fakeForge{pause: 2 * time.Millisecond} + f.add(Upsert("", "seed", "seed"), true) + item := ItemKey("o/r", 1) + keys := []string{"a", "b", "c", "d"} + var wg sync.WaitGroup + errs := make(chan error, len(keys)) + for _, k := range keys { + wg.Add(1) + go func(k string) { + defer wg.Done() + _, err := Write(context.Background(), f, item, k, "text "+k) + errs <- err + }(k) + } + wg.Wait() + close(errs) + for err := range errs { + if err != nil { + t.Fatal(err) + } + } + if f.overlap.Load() { + t.Fatal("two status writers on one item interleaved their calls") + } + bodies := f.statusBodies() + if len(bodies) != 1 { + t.Fatalf("want one status comment, got %d", len(bodies)) + } + for _, k := range append([]string{"seed"}, keys...) { + if sectionCount(bodies[0], k) != 1 { + t.Fatalf("section %q lost or duplicated:\n%s", k, bodies[0]) + } + } +} + +// Two writers racing to create the status comment on an item that has +// none: one creates it, the other edits it; nothing is posted twice. +func TestWriteConcurrentCreateMakesOneComment(t *testing.T) { + f := &fakeForge{pause: time.Millisecond} + item := ItemKey("o/r", 2) + var wg sync.WaitGroup + var created atomic.Int32 + for _, k := range []string{"a", "b"} { + wg.Add(1) + go func(k string) { + defer wg.Done() + res, err := Write(context.Background(), f, item, k, "text "+k) + if err != nil { + t.Error(err) + } + if res.Created { + created.Add(1) + } + }(k) + } + wg.Wait() + bodies := f.statusBodies() + if f.creates != 1 || created.Load() != 1 || len(bodies) != 1 { + t.Fatalf("creates=%d reported=%d status comments=%d, want exactly one", f.creates, created.Load(), len(bodies)) + } + if sectionCount(bodies[0], "a") != 1 || sectionCount(bodies[0], "b") != 1 { + t.Fatalf("a section was lost:\n%s", bodies[0]) + } +} + +// Another writer (another hive instance) adds a section between Write's +// read and its edit: the pre-edit re-read sees the change, the section is +// recomputed on the fresh body, and the third-party section survives. +func TestWriteRetriesWhenBodyChangedBetweenReadAndEdit(t *testing.T) { + f := &fakeForge{} + id := f.add(Upsert("", "mine", "old"), true) + f.beforeList = func(n int, f *fakeForge) { + if n == 2 { + for i := range f.comments { + if f.comments[i].ID == id { + f.comments[i].Body = Upsert(f.comments[i].Body, "theirs", "third-party") + } + } + } + } + res, err := Write(context.Background(), f, ItemKey("o/r", 3), "mine", "new") + if err != nil || !res.Edited || res.Created { + t.Fatalf("Write = %+v, %v", res, err) + } + if f.lists < 3 { + t.Fatalf("changed body must trigger a re-read and retry, lists=%d", f.lists) + } + if f.edits != 1 { + t.Fatalf("edits=%d, want 1", f.edits) + } + body := f.statusBodies()[0] + if got, _ := Get(body, "theirs"); got != "third-party" { + t.Fatalf("third-party section lost:\n%s", body) + } + if got, _ := Get(body, "mine"); got != "new" { + t.Fatalf("own section not written:\n%s", body) + } +} + +// A comment that changes under every attempt is reported, not overwritten. +func TestWriteGivesUpAfterMaxAttempts(t *testing.T) { + f := &fakeForge{} + id := f.add(Upsert("", "mine", "old"), true) + f.beforeList = func(n int, f *fakeForge) { + for i := range f.comments { + if f.comments[i].ID == id { + f.comments[i].Body = Upsert(f.comments[i].Body, "churn", strings.Repeat("x", n)) + } + } + } + if _, err := Write(context.Background(), f, ItemKey("o/r", 4), "mine", "new"); !errors.Is(err, ErrStatusConflict) { + t.Fatalf("err = %v, want ErrStatusConflict", err) + } + if f.edits != 0 { + t.Fatalf("a conflicting comment was edited %d times", f.edits) + } +} + +// Two status comments: every section is merged into the oldest, the newer +// hive-authored copy is deleted, and each section appears exactly once. +func TestWriteMergesDuplicateStatusCommentsIntoOldest(t *testing.T) { + f := &fakeForge{} + f.add("unrelated comment", false) + oldest := f.add(Upsert(Upsert("", "a", "a-oldest"), "shared", "s-oldest"), true) + newer := f.add(Upsert(Upsert("", "b", "b-newer"), "shared", "s-newer"), true) + res, err := Write(context.Background(), f, ItemKey("o/r", 5), "c", "c-text") + if err != nil || res.Created || res.Deleted != 1 { + t.Fatalf("Write = %+v, %v", res, err) + } + if len(f.deleted) != 1 || f.deleted[0] != newer { + t.Fatalf("deleted %v, want the newer status comment %d", f.deleted, newer) + } + bodies := f.statusBodies() + if len(bodies) != 1 { + t.Fatalf("want one status comment left, got %d", len(bodies)) + } + body := bodies[0] + if f.comments[1].ID != oldest { + t.Fatalf("the oldest status comment must be the survivor") + } + for _, k := range []string{"a", "b", "shared", "c"} { + if sectionCount(body, k) != 1 { + t.Fatalf("section %q not exactly once:\n%s", k, body) + } + } + if got, _ := Get(body, "shared"); got != "s-oldest" { + t.Fatalf("the oldest comment's copy of a shared section must win, got %q", got) + } +} + +// Another instance creates a status comment while this one is creating +// its own: the post-create reconciliation folds this one's section into +// the older comment and deletes the newer one. +func TestWriteReconcilesDuplicateCreate(t *testing.T) { + f := &fakeForge{} + f.beforeCreate = func(f *fakeForge) { + f.add(Upsert("", "other", "other-instance"), true) + } + res, err := Write(context.Background(), f, ItemKey("o/r", 6), "mine", "mine-text") + if err != nil || !res.Created || res.Deleted != 1 { + t.Fatalf("Write = %+v, %v", res, err) + } + bodies := f.statusBodies() + if len(bodies) != 1 { + t.Fatalf("duplicate status comment left behind: %d", len(bodies)) + } + if sectionCount(bodies[0], "other") != 1 || sectionCount(bodies[0], "mine") != 1 { + t.Fatalf("sections not merged:\n%s", bodies[0]) + } +} + +// A status marker in a comment a person wrote is merged from but never +// deleted. +func TestWriteNeverDeletesForeignStatusComment(t *testing.T) { + f := &fakeForge{} + f.add(Upsert("", "a", "a"), true) + f.add(Upsert("", "pasted", "by a person"), false) + res, err := Write(context.Background(), f, ItemKey("o/r", 7), "b", "b") + if err != nil || res.Deleted != 0 || len(f.deleted) != 0 { + t.Fatalf("Write = %+v, %v, deleted=%v", res, err, f.deleted) + } + if got, _ := Get(f.comments[0].Body, "pasted"); got != "by a person" { + t.Fatalf("foreign section not merged into the oldest:\n%s", f.comments[0].Body) + } +} + +func TestWriteEmptyTextWithoutCommentWritesNothing(t *testing.T) { + f := &fakeForge{} + res, err := Write(context.Background(), f, ItemKey("o/r", 8), "a", " ") + if err != nil || res != (WriteResult{}) || f.creates != 0 { + t.Fatalf("Write = %+v, %v, creates=%d", res, err, f.creates) + } +} + +// Write is the only hive:status writer. Every other non-test Go file in the +// tree may read a status comment but must not build one: rendering or +// upserting a status body, or naming the marker, outside this package is a +// second writer that bypasses the lock, the compare-and-retry and the +// duplicate reconciliation. +func TestNoStatusWriterOutsideWrite(t *testing.T) { + root, err := filepath.Abs(filepath.Join("..", "..")) + if err != nil { + t.Fatal(err) + } + self, err := filepath.Abs(".") + if err != nil { + t.Fatal(err) + } + forbidden := []string{ + "prstatus.Marker", + "prstatus.Upsert(", + "prstatus.Render(", + "prstatus.Merge(", + Marker, + "hive:status -->", + } + var offenders []string + err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + switch d.Name() { + case ".git", "node_modules", "vendor", "testdata": + return filepath.SkipDir + } + if path == self { + return filepath.SkipDir + } + return nil + } + if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { + return nil + } + raw, err := os.ReadFile(path) + if err != nil { + return err + } + for _, s := range forbidden { + if strings.Contains(string(raw), s) { + rel, _ := filepath.Rel(root, path) + offenders = append(offenders, rel+": "+s) + } + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if len(offenders) > 0 { + t.Fatalf("hive:status comments must be written through prstatus.Write only:\n%s", strings.Join(offenders, "\n")) + } +}