From 2dc05ee2527d446d2f7ca2e324b4a8c6cf4618b6 Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 17:46:24 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(close-on-merge):=20a=20R?= =?UTF-8?q?efs-only=20PR=20never=20closes=20the=20issue=20it=20references?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit projectbluefin/utah-packages#490: PR #860 said "Refs #490" and the coverage comment said Hive would not close the issue, yet the backfill sweep closed it on merge. The backfill passes the configured short repo name; the Refs guard compared it with the parsed owner/repo and saw no reference. - every reference check in the close path compares owner/repo (refRepo) - the close path reads the coverage comment's decision (prLeavesIssueOpen: no closing keyword + Refs/Part of or stays-open wording) - an issue marked hive/verified-open or hive/partially-addressed closes only on a PR whose own text closes it - GET /api/close-on-merge/wrong-closes lists past wrong closes (read-only) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- changelog.d/fixed-close-on-merge-refs.md | 1 + dashboard/openapi.json | 90 ++++++++++++ src/docs/api-reference/dashboard-misc.md | 1 + src/docs/hive-merge.md | 14 ++ src/pkg/dashboard/api.go | 3 + src/pkg/dashboard/api_close_on_merge_audit.go | 48 ++++++ .../api_close_on_merge_audit_test.go | 46 ++++++ src/pkg/github/close_on_merge.go | 57 +++++++- src/pkg/github/close_on_merge_audit.go | 132 +++++++++++++++++ src/pkg/github/close_on_merge_audit_test.go | 82 +++++++++++ .../github/close_on_merge_refs_490_test.go | 137 ++++++++++++++++++ src/pkg/github/close_on_merge_test.go | 2 +- 12 files changed, 608 insertions(+), 5 deletions(-) create mode 100644 changelog.d/fixed-close-on-merge-refs.md create mode 100644 src/pkg/dashboard/api_close_on_merge_audit.go create mode 100644 src/pkg/dashboard/api_close_on_merge_audit_test.go create mode 100644 src/pkg/github/close_on_merge_audit.go create mode 100644 src/pkg/github/close_on_merge_audit_test.go create mode 100644 src/pkg/github/close_on_merge_refs_490_test.go diff --git a/changelog.d/fixed-close-on-merge-refs.md b/changelog.d/fixed-close-on-merge-refs.md new file mode 100644 index 0000000000..2351c601e4 --- /dev/null +++ b/changelog.d/fixed-close-on-merge-refs.md @@ -0,0 +1 @@ +- Close-on-merge no longer closes an issue a merged PR only references. The backfill sweep passed the configured short repository name (`utah-packages`) into the close path, and the `Refs` guard compared it with the parsed `owner/repo`, so `Refs #490` on projectbluefin/utah-packages#860 looked like no reference and #490 was closed with "Fixed by #860". References now compare as `owner/repo`. The close path reads the same non-closing decision as the coverage comment (`Refs`/`Part of`, or "#N stays open"). An issue carrying `hive/verified-open` or `hive/partially-addressed` is closed only by a PR whose own text closes it. `GET /api/close-on-merge/wrong-closes?days=N` lists issues closed the wrong way for maintainers to reopen. diff --git a/dashboard/openapi.json b/dashboard/openapi.json index ebaa166571..58262ba1c3 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -36940,6 +36940,96 @@ } } }, + "/api/close-on-merge/wrong-closes": { + "get": { + "tags": [ + "Governor" + ], + "summary": "List issues close-on-merge closed on a PR that left them open", + "description": "Read-only report for maintainers (projectbluefin/utah-packages#490): issues the hive closed in the last ?days=N (default 30, 1-90) with \"Fixed by #N\" where PR #N only references the issue (Refs / Part of) or says it stays open, or where the issue carried hive/verified-open or hive/partially-addressed and the PR had no closing keyword for it. The hive reopens nothing; maintainers decide.", + "parameters": [ + { + "name": "days", + "in": "query", + "schema": { + "type": "integer", + "minimum": 1, + "maximum": 90 + }, + "description": "Window in days (default 30)" + } + ], + "responses": { + "200": { + "description": "Wrongly closed issues", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "since": { + "type": "string", + "format": "date-time" + }, + "days": { + "type": "integer" + }, + "complete": { + "type": "boolean", + "description": "false when a repository listing failed; error says which" + }, + "error": { + "type": "string" + }, + "items": { + "type": "array", + "items": { + "type": "object", + "properties": { + "repo": { + "type": "string" + }, + "issue": { + "type": "integer" + }, + "title": { + "type": "string" + }, + "url": { + "type": "string" + }, + "pr": { + "type": "integer", + "description": "The merged PR the hive's \"Fixed by #N\" comment names" + }, + "closed_at": { + "type": "string", + "format": "date-time" + }, + "reason": { + "type": "string", + "enum": [ + "pr_leaves_issue_open", + "issue_marked_open" + ] + } + } + } + } + } + } + } + } + }, + "400": { + "description": "days out of range" + }, + "503": { + "description": "GitHub client not configured" + } + } + } + }, "/api/sentinel": { "get": { "tags": [ diff --git a/src/docs/api-reference/dashboard-misc.md b/src/docs/api-reference/dashboard-misc.md index 62f0b3e412..8f1ef7ea77 100644 --- a/src/docs/api-reference/dashboard-misc.md +++ b/src/docs/api-reference/dashboard-misc.md @@ -51,6 +51,7 @@ The monitor renders CI and Review concurrently on one shared PR row; see | `PUT` | `/api/config/upstream-watch` | Owner only | Upstream Watch Config Update: edit `upstream_watch` from the Features tab. Body `{enabled?, interval?, repos?: {: {upstream?, sources?, pr_labels?, max_issues_per_run?, label?} \| null}}` — absent keys are unchanged, `null` removes a repo, keys are stored as the bare repo name. Validated with the loader's rules before the config changes (400 on failure), then persisted and audited as `config_upstream_watch`. | `pkg/dashboard/api.go:322` | | `GET` | `/api/hotspots` | Dashboard auth/session | Merge-hotspot advisor report (`?repo=owner/name` optional): files repeatedly touched by concurrent PRs over the last `governor.hotspot_window_days`, each with `prs` (distinct PRs), `pr_numbers`, `overlaps` (PR pairs open at the same time), `conflicts` (rebase conflicts blamed on the file), `lines`, `kind` (`code`/`doc`/`lock`/`generated`/`data`), `score`, `symbol_groups`, `recommendation` (`split-code`/`split-doc`/`split-source`/`report-only`/`none`), `reason`, `generated_from`, `source_files` (files under `generated_from`, when known), `issue_number`/`issue_url` of the modularize issue filed, `last_updated`; top-level `count`/`actionable`/`filed`. Derived from the merge train's own PR file lists and the rebase follow-through's conflict records — never lists PRs or files from GitHub. See [operator-reference.md § Merge-hotspot advisor](../operator-reference.md#merge-hotspot-advisor-governorhotspot_). | `pkg/dashboard/api.go:567` | +| `GET` | `/api/close-on-merge/wrong-closes` | Dashboard auth/session | Read-only report of issues close-on-merge closed on a PR that did not close them (projectbluefin/utah-packages#490): for each watched repo, issues closed in the last `?days=N` (default 30, 1–90) whose hive "Fixed by #N" comment names a merged PR that only references the issue (`Refs`/`Part of`) or says it stays open (`reason: pr_leaves_issue_open`), or that carries `hive/verified-open` / `hive/partially-addressed` and was closed by a PR with no closing keyword for it (`reason: issue_marked_open`). Returns `{since, days, items[], complete}`, each item `{repo, issue, title, url, pr, closed_at, reason}`; `complete:false` with `error` when a listing failed. Maintainers reopen what they agree with; the hive reopens nothing | `pkg/dashboard/api_close_on_merge_audit.go:24` | | `GET` | `/api/widget` | Dashboard auth/session | Widget | `pkg/dashboard/api.go:130` | | `GET` | `/api/pane/{agent}` | Dashboard auth/session | Pane | `pkg/dashboard/api.go:135` | | `GET` | `/api/role` | Dashboard auth/session | Role | `pkg/dashboard/api.go:149` | diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index e5e95e0c17..d355275372 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -1144,6 +1144,20 @@ refuses tracking issues and honours the reporter-confirmation gate) and is counted by the direct-write ratchet, so no other code path can grow a close of its own. +A merged PR closes an issue only when it does not leave the issue open. The +close path reads the same decision the issue's coverage comment states ("the +PR body says this is partial/non-closing work … Hive will not add a closing +keyword"): no closing keyword for the issue, and a `Refs #N` / `Part of #N` +reference or wording such as "#N stays open". Such a PR labels the issue +`hive/partially-addressed` and leaves it open. An issue that carries +`hive/verified-open` or `hive/partially-addressed` is closed only by a PR +whose own text closes it, never by a branch name, claim or comment link +alone. Every sweep compares references as `owner/repo`, whatever name the +repository is configured under: the backfill sweep passed the short name and +closed projectbluefin/utah-packages#490 on a `Refs #490` PR. +`GET /api/close-on-merge/wrong-closes?days=N` lists the issues closed that way +for maintainers to reopen; the hive reopens nothing itself. + The tracker shows the consequence honestly. A follow-up or duplicate PR whose body names an issue an *earlier* PR already delivered — `Refs #15422` on #15425 after #15426 merged, `extending #15058` on #15431 after #15068 merged — diff --git a/src/pkg/dashboard/api.go b/src/pkg/dashboard/api.go index b9cc79373c..dc4e75f6d8 100644 --- a/src/pkg/dashboard/api.go +++ b/src/pkg/dashboard/api.go @@ -562,6 +562,9 @@ func (s *Server) RegisterAPI(deps *Dependencies) { // and the sentinel findings held internally, written or not. s.mux.HandleFunc("GET /api/writeback/suppressed", s.handleWritebackSuppressed) s.mux.HandleFunc("GET /api/sentinel", s.handleSentinel) + // Issues close-on-merge closed on a PR that left them open + // (api_close_on_merge_audit.go): a read-only list to reopen by hand. + s.mux.HandleFunc("GET /api/close-on-merge/wrong-closes", s.handleCloseOnMergeWrongCloses) // Merge-hotspot advisor (api_hotspots.go): files repeatedly touched by // concurrent PRs and the modularize issues filed for them. s.mux.HandleFunc("GET /api/hotspots", s.handleHotspots) diff --git a/src/pkg/dashboard/api_close_on_merge_audit.go b/src/pkg/dashboard/api_close_on_merge_audit.go new file mode 100644 index 0000000000..1de44409fe --- /dev/null +++ b/src/pkg/dashboard/api_close_on_merge_audit.go @@ -0,0 +1,48 @@ +package dashboard + +import ( + "context" + "net/http" + "strconv" + "strings" + "time" + + ghpkg "github.com/hivecommons/hive/pkg/github" +) + +// closeOnMergeWrongClosesFunc reads the report from GitHub; tests swap it. +var closeOnMergeWrongClosesFunc = func(ctx context.Context, c *ghpkg.Client, since time.Time) ([]ghpkg.CloseOnMergeWrongClose, error) { + return c.CloseOnMergeWrongCloses(ctx, since) +} + +// handleCloseOnMergeWrongCloses serves GET /api/close-on-merge/wrong-closes?days=N +// (default 30, at most 90): the issues the hive closed with "Fixed by #N" +// where PR #N only referenced them or said they stay open, or where the issue +// was marked hive/verified-open or hive/partially-addressed and the PR did not +// close it (projectbluefin/utah-packages#490). Read-only: maintainers reopen +// what they agree with; the hive reopens nothing. +func (s *Server) handleCloseOnMergeWrongCloses(w http.ResponseWriter, r *http.Request) { + days := 30 + if raw := strings.TrimSpace(r.URL.Query().Get("days")); raw != "" { + n, err := strconv.Atoi(raw) + if err != nil || n <= 0 || n > 90 { + jsonError(w, "days must be 1 to 90", http.StatusBadRequest) + return + } + days = n + } + if s.deps == nil || s.deps.GHClient == nil { + jsonError(w, "GitHub client not configured", http.StatusServiceUnavailable) + return + } + since := time.Now().Add(-time.Duration(days) * 24 * time.Hour) + items, err := closeOnMergeWrongClosesFunc(r.Context(), s.deps.GHClient, since) + if items == nil { + items = []ghpkg.CloseOnMergeWrongClose{} + } + resp := map[string]interface{}{"since": since.UTC().Format(time.RFC3339), "days": days, "items": items, "complete": err == nil} + if err != nil { + resp["error"] = err.Error() + } + jsonResponse(w, resp) +} diff --git a/src/pkg/dashboard/api_close_on_merge_audit_test.go b/src/pkg/dashboard/api_close_on_merge_audit_test.go new file mode 100644 index 0000000000..9de2f84f02 --- /dev/null +++ b/src/pkg/dashboard/api_close_on_merge_audit_test.go @@ -0,0 +1,46 @@ +package dashboard + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + ghpkg "github.com/hivecommons/hive/pkg/github" +) + +func TestCloseOnMergeWrongClosesEndpoint(t *testing.T) { + prev := closeOnMergeWrongClosesFunc + t.Cleanup(func() { closeOnMergeWrongClosesFunc = prev }) + var gotSince time.Time + closeOnMergeWrongClosesFunc = func(_ context.Context, _ *ghpkg.Client, since time.Time) ([]ghpkg.CloseOnMergeWrongClose, error) { + gotSince = since + return []ghpkg.CloseOnMergeWrongClose{{Repo: "projectbluefin/utah-packages", Issue: 490, PR: 860, Reason: "pr_leaves_issue_open"}}, nil + } + s, deps := apiServer(t) + deps.GHClient = &ghpkg.Client{} + rec := doGet(s, "/api/close-on-merge/wrong-closes?days=7") + if rec.Code != http.StatusOK { + t.Fatalf("code = %d body = %s", rec.Code, rec.Body.String()) + } + var resp struct { + Days int `json:"days"` + Complete bool `json:"complete"` + Items []ghpkg.CloseOnMergeWrongClose `json:"items"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatal(err) + } + if resp.Days != 7 || !resp.Complete || len(resp.Items) != 1 || resp.Items[0].Issue != 490 { + t.Fatalf("resp = %+v", resp) + } + if d := time.Since(gotSince); d < 7*24*time.Hour-time.Minute || d > 7*24*time.Hour+time.Minute { + t.Fatalf("since = %v, want 7 days ago", gotSince) + } + for _, bad := range []string{"0", "91", "x"} { + if rec := doGet(s, "/api/close-on-merge/wrong-closes?days="+bad); rec.Code != http.StatusBadRequest { + t.Errorf("days=%s: code = %d, want 400", bad, rec.Code) + } + } +} diff --git a/src/pkg/github/close_on_merge.go b/src/pkg/github/close_on_merge.go index 53c228907e..a43c3037af 100644 --- a/src/pkg/github/close_on_merge.go +++ b/src/pkg/github/close_on_merge.go @@ -579,6 +579,35 @@ func closeOnMergePRRefsIssue(pr *gh.PullRequest, prRepo, issueRepo string, issue return false } +// prLeavesIssueOpen is the closing-keyword decision the issue's coverage +// comment states ("partial/non-closing work … Hive will not add a closing +// keyword", EnsureIssueStagePRLink): the PR text names no closing keyword for +// the issue, and either references it (`Refs #N`, `Part of #N`) or says in +// words that it stays open. The close path reads the same decision, so a PR +// the hive called non-closing never closes the issue when it merges. +func prLeavesIssueOpen(text, prRepo, issueRepo string, issue int) bool { + if issue <= 0 { + return false + } + if _, closing := FindClosingKeywordReference(text, prRepo, issueRepo, issue); closing { + return false + } + for _, ref := range ParseReferencedIssues(text, prRepo) { + if ref.Issue == issue && strings.EqualFold(ref.Repo, issueRepo) { + return true + } + } + return strings.EqualFold(prRepo, issueRepo) && issueStaysOpenPhrase(stripClosingFalsePositiveText(text), issue) +} + +// closeOnMergeIssueMarkedOpen reports whether the issue carries a label that +// says work remains: hive/verified-open (an agent re-checked it and found it +// still open) or hive/partially-addressed (an earlier merge covered only part). +func closeOnMergeIssueMarkedOpen(issue *gh.Issue) bool { + labels := issueLabelNames(issue.Labels) + return labelPresent(labels, VerifiedOpenLabel) || labelPresent(labels, PartiallyAddressedLabel) +} + // closeOnMergeIssueNotFound reports whether reading the referenced issue // failed because GitHub does not know it (404), as opposed to a transient // API error worth retrying on the next sweep. @@ -597,6 +626,15 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR prRepo = pr.GetBase().GetRepo().GetFullName() } res := CloseOnMergeResult{Repo: issueRepo, Issue: issueNumber} + // The backfill sweep and webhooks pass the configured repo name, which + // may be short ("utah-packages"); references parse as owner/repo, so + // every reference check compares against refRepo. Comparing the short + // name made `Refs #490` look like no reference at all, and the guard + // below closed projectbluefin/utah-packages#490 on a Refs-only PR. + refRepo := issueRepo + if owner, name := c.splitRepo(issueRepo); owner != "" && name != "" { + refRepo = owner + "/" + name + } if pr != nil { res.PR = pr.GetNumber() res.MergeCommit = pr.GetMergeCommitSHA() @@ -621,11 +659,11 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR return res } } - if !claimMetadata && !closeOnMergePRClaimsIssue(pr, prRepo, issueRepo, issueNumber) { + if !claimMetadata && !closeOnMergePRClaimsIssue(pr, prRepo, refRepo, issueNumber) { res.Action, res.Reason = CloseOnMergeSkipped, "no_closing_or_claim_metadata" return res } - if claimMetadata && !closeOnMergePRClaimsIssue(pr, prRepo, issueRepo, issueNumber) && closeOnMergePRRefsIssue(pr, prRepo, issueRepo, issueNumber) { + if claimMetadata && !closeOnMergePRClaimsIssue(pr, prRepo, refRepo, issueNumber) && closeOnMergePRRefsIssue(pr, prRepo, refRepo, issueNumber) { res.WrongRefs = true if opts.Logger != nil { // This is PR metadata, not a failed runtime operation. WARN would @@ -710,7 +748,7 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR return res } if c.reporterConfirmationCloseGateReason(issue) != "" || closeOnMergeNeedsConfirmation(issue) { - if cov := c.closeOnMergeCoverage(ctx, prRepo, issueRepo, issueNumber, pr, issue, opts); cov.partial() { + if cov := c.closeOnMergeCoverage(ctx, prRepo, refRepo, issueNumber, pr, issue, opts); cov.partial() { c.markPartiallyAddressed(ctx, issueRepo, issueNumber, pr.GetNumber(), issue, cov, opts) res.Action, res.Reason = CloseOnMergeSkipped, "partially_addressed" return res @@ -738,11 +776,22 @@ func (c *Client) closeIssueForMergedPRClaimWithIssue(ctx context.Context, issueR res.Action, res.Reason = CloseOnMergeAwaiting, "needs_confirmation_or_human" return res } - if cov := c.closeOnMergeCoverage(ctx, prRepo, issueRepo, issueNumber, pr, issue, opts); cov.partialForDirectClose(closeOnMergePRRefsIssue(pr, prRepo, issueRepo, issueNumber)) { + cov := c.closeOnMergeCoverage(ctx, prRepo, refRepo, issueNumber, pr, issue, opts) + if cov.partialForDirectClose(prLeavesIssueOpen(pr.GetTitle()+"\n"+pr.GetBody(), prRepo, refRepo, issueNumber)) { c.markPartiallyAddressed(ctx, issueRepo, issueNumber, pr.GetNumber(), issue, cov, opts) res.Action, res.Reason = CloseOnMergeSkipped, "partially_addressed" return res } + if !cov.Closing && closeOnMergeIssueMarkedOpen(issue) { + // An agent re-verified the issue as still open, or an earlier merge + // left part of it: only a PR whose own text closes the issue may + // close it. A branch name, claim or comment link is not enough. + res.Action, res.Reason = CloseOnMergeSkipped, "issue_marked_open" + if opts.Logger != nil { + opts.Logger.Info("close-on-merge: issue is marked open and the PR does not close it; leaving it open", "repo", issueRepo, "issue", issueNumber, "pr", pr.GetNumber()) + } + return res + } // The issue-stage lane may already have said "Fixed by #N" for this // PR (mergedPRClosedStageMarker) and only failed to close; one closing // comment per issue is enough (hivecommons/hive#15727 vs #15843). diff --git a/src/pkg/github/close_on_merge_audit.go b/src/pkg/github/close_on_merge_audit.go new file mode 100644 index 0000000000..a7d4d9eec2 --- /dev/null +++ b/src/pkg/github/close_on_merge_audit.go @@ -0,0 +1,132 @@ +package github + +import ( + "context" + "regexp" + "sort" + "strconv" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// CloseOnMergeWrongClose is one issue close-on-merge closed on a PR that, by +// the closing-keyword decision the issue's coverage comment states +// (prLeavesIssueOpen), left it open, or that the issue's labels said still +// had work left. It is a report for maintainers to reopen by hand; the hive +// never reopens these itself. +type CloseOnMergeWrongClose struct { + Repo string `json:"repo"` + Issue int `json:"issue"` + Title string `json:"title,omitempty"` + URL string `json:"url,omitempty"` + PR int `json:"pr"` + ClosedAt time.Time `json:"closed_at"` + // Reason is "pr_leaves_issue_open" (the PR only references the issue or + // says it stays open) or "issue_marked_open" (no closing keyword and the + // issue carries hive/verified-open or hive/partially-addressed today). + Reason string `json:"reason"` +} + +var closeOnMergeFixedByRE = regexp.MustCompile(`Fixed by #(\d+) \(merged`) + +// CloseOnMergeWrongCloses lists the issues the hive closed since `since` with +// its close-on-merge comment ("Fixed by #N (merged …)") where PR #N did not +// close them (projectbluefin/utah-packages#490: `Refs #490` on #860). It only +// reads: closed issues per watched repo, the close comment, the PR. +func (c *Client) CloseOnMergeWrongCloses(ctx context.Context, since time.Time) ([]CloseOnMergeWrongClose, error) { + if c == nil || c.client == nil { + return nil, nil + } + ctx = WithRESTCaller(ctx, "hive:close_on_merge_audit") + var out []CloseOnMergeWrongClose + var firstErr error + for _, repo := range c.activeRepos() { + owner, name := c.splitRepo(repo) + full := owner + "/" + name + opts := &gh.IssueListByRepoOptions{State: "closed", Since: since, ListOptions: gh.ListOptions{PerPage: 100}} + for page := 0; page < claimSearchMaxPages; page++ { + issues, resp, err := c.client.Issues.ListByRepo(ctx, owner, name, opts) + if err != nil { + if firstErr == nil { + firstErr = err + } + break + } + for _, issue := range issues { + if row, ok := c.closeOnMergeWrongClose(ctx, owner, name, full, since, issue); ok { + out = append(out, row) + } + } + if resp == nil || resp.NextPage == 0 { + break + } + opts.ListOptions.Page = resp.NextPage + } + } + sort.Slice(out, func(i, j int) bool { + if out[i].Repo != out[j].Repo { + return out[i].Repo < out[j].Repo + } + return out[i].Issue < out[j].Issue + }) + return out, firstErr +} + +func (c *Client) closeOnMergeWrongClose(ctx context.Context, owner, name, full string, since time.Time, issue *gh.Issue) (CloseOnMergeWrongClose, bool) { + if issue == nil || issue.IsPullRequest() || !strings.EqualFold(issue.GetState(), "closed") { + return CloseOnMergeWrongClose{}, false + } + closedAt := issue.GetClosedAt().Time + if closedAt.Before(since) { + return CloseOnMergeWrongClose{}, false + } + pr := c.closeOnMergeCommentPR(ctx, owner, name, issue.GetNumber(), closedAt) + if pr <= 0 { + return CloseOnMergeWrongClose{}, false + } + merged, err := c.getPullRequestForCloseOnMerge(ctx, full, pr) + if err != nil || merged == nil { + return CloseOnMergeWrongClose{}, false + } + prRepo := full + if base := merged.GetBase().GetRepo().GetFullName(); base != "" { + prRepo = base + } + text := merged.GetTitle() + "\n" + merged.GetBody() + row := CloseOnMergeWrongClose{Repo: full, Issue: issue.GetNumber(), Title: issue.GetTitle(), URL: issue.GetHTMLURL(), PR: pr, ClosedAt: closedAt} + switch { + case prLeavesIssueOpen(text, prRepo, full, issue.GetNumber()): + row.Reason = "pr_leaves_issue_open" + case closeOnMergeIssueMarkedOpen(issue): + if _, closing := FindClosingKeywordReference(text, prRepo, full, issue.GetNumber()); closing { + return CloseOnMergeWrongClose{}, false + } + row.Reason = "issue_marked_open" + default: + return CloseOnMergeWrongClose{}, false + } + return row, true +} + +// closeOnMergeCommentPR finds the hive's close-on-merge comment posted around +// the close and returns the PR it names, or 0. +func (c *Client) closeOnMergeCommentPR(ctx context.Context, owner, name string, number int, closedAt time.Time) int { + opts := &gh.IssueListCommentsOptions{Since: gh.Ptr(closedAt.Add(-10 * time.Minute)), ListOptions: gh.ListOptions{PerPage: closeOnMergeCommentPageSize}} + comments, _, err := c.client.Issues.ListComments(ctx, owner, name, number, opts) + if err != nil { + return 0 + } + for _, cm := range comments { + body := cm.GetBody() + if !c.isHiveAccount(cm.GetUser()) || !strings.Contains(body, CloseOnMergeMarker) { + continue + } + if m := closeOnMergeFixedByRE.FindStringSubmatch(body); m != nil { + n, _ := strconv.Atoi(m[1]) + return n + } + } + return 0 +} diff --git a/src/pkg/github/close_on_merge_audit_test.go b/src/pkg/github/close_on_merge_audit_test.go new file mode 100644 index 0000000000..85c03e3658 --- /dev/null +++ b/src/pkg/github/close_on_merge_audit_test.go @@ -0,0 +1,82 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +// The report lists #490 (closed on a Refs-only PR) and an issue closed on a +// branch claim while marked verified-open; not one a closing PR closed (even +// when marked verified-open), one a person closed or only quoted the hive's +// note on, a PR, or one closed before the window. +func TestCloseOnMergeWrongClosesReport(t *testing.T) { + closedAt := "2026-11-01T12:22:26Z" + marker := func(pr string) []map[string]any { + return []map[string]any{{"user": map[string]string{"login": "hive[bot]"}, "body": CloseOnMergeMarker + "\nFixed by #" + pr + " (merged 5bd573a1565f). Closing. Reply /reopen if the problem persists."}} + } + pr := func(n int, body string) map[string]any { + return map[string]any{"number": n, "title": "t", "body": body, "merged": true, "merged_at": "2026-11-01T10:25:46Z", + "user": map[string]string{"login": "hive[bot]"}, "head": map[string]string{"ref": "hive/x"}, "base": map[string]any{"repo": map[string]string{"full_name": "o/r"}}} + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + enc := json.NewEncoder(w) + switch r.URL.Path { + case "/repos/o/r/issues": + if r.URL.Query().Get("state") != "closed" || r.URL.Query().Get("since") == "" { + t.Errorf("list query = %s", r.URL.RawQuery) + } + _ = enc.Encode([]map[string]any{ + {"number": 490, "state": "closed", "closed_at": closedAt, "title": "parity batch"}, + {"number": 491, "state": "closed", "closed_at": closedAt, "labels": []map[string]string{{"name": VerifiedOpenLabel}}}, + {"number": 492, "state": "closed", "closed_at": closedAt}, + {"number": 493, "state": "closed", "closed_at": closedAt}, + {"number": 494, "state": "closed", "closed_at": "2026-09-01T00:00:00Z"}, + {"number": 495, "state": "closed", "closed_at": closedAt, "pull_request": map[string]any{"url": "u"}}, + {"number": 496, "state": "closed", "closed_at": closedAt, "labels": []map[string]string{{"name": VerifiedOpenLabel}}}, + {"number": 497, "state": "closed", "closed_at": closedAt}, + }) + case "/repos/o/r/issues/490/comments": + _ = enc.Encode(marker("860")) + case "/repos/o/r/issues/491/comments": + _ = enc.Encode(marker("861")) + case "/repos/o/r/issues/492/comments": + _ = enc.Encode(marker("862")) + case "/repos/o/r/issues/493/comments": + _ = enc.Encode([]map[string]any{{"user": map[string]string{"login": "maintainer"}, "body": "done"}}) + case "/repos/o/r/issues/496/comments": + _ = enc.Encode(marker("863")) + case "/repos/o/r/issues/497/comments": + // A person quoting the hive's close note is not the hive closing it. + _ = enc.Encode([]map[string]any{{"user": map[string]string{"login": "someone"}, "body": CloseOnMergeMarker + "\nFixed by #864 (merged 5bd573a1565f)."}}) + case "/repos/o/r/pulls/864": + _ = enc.Encode(pr(864, "Refs #497")) + case "/repos/o/r/pulls/863": + _ = enc.Encode(pr(863, "Closes #496")) + case "/repos/o/r/pulls/860": + _ = enc.Encode(pr(860, "Refs #490 — ten outputs remain unpublished.")) + case "/repos/o/r/pulls/861": + _ = enc.Encode(pr(861, "Publishes the rest.")) + case "/repos/o/r/pulls/862": + _ = enc.Encode(pr(862, "Closes #492")) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + c.SetAppBotLogin("hive[bot]") + rows, err := c.CloseOnMergeWrongCloses(context.Background(), time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC)) + if err != nil { + t.Fatalf("report: %v", err) + } + if len(rows) != 2 || rows[0].Issue != 490 || rows[0].PR != 860 || rows[0].Reason != "pr_leaves_issue_open" || rows[0].Repo != "o/r" || + rows[1].Issue != 491 || rows[1].Reason != "issue_marked_open" { + t.Fatalf("rows = %+v, want #490 (pr_leaves_issue_open) and #491 (issue_marked_open)", rows) + } +} diff --git a/src/pkg/github/close_on_merge_refs_490_test.go b/src/pkg/github/close_on_merge_refs_490_test.go new file mode 100644 index 0000000000..e5637c7061 --- /dev/null +++ b/src/pkg/github/close_on_merge_refs_490_test.go @@ -0,0 +1,137 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "sync" + "testing" +) + +// projectbluefin/utah-packages#490: PR #860 (branch hive/490-…) said "Refs +// #490" and the hive's coverage comment said it would not close the issue, +// yet the backfill sweep closed #490 when #860 merged. The backfill passes +// the configured repo name ("utah-packages"); the Refs check compared it +// with the parsed owner/repo and found no reference, so the guard let the +// close through. The client here is configured the same way: org "o", +// repo "r". +func runBackfill490(t *testing.T, prNum int, body string, labels []string) (CloseOnMergeResult, bool) { + t.Helper() + var mu sync.Mutex + closed := false + issueLabels := make([]map[string]string, 0, len(labels)) + for _, l := range labels { + issueLabels = append(issueLabels, map[string]string{"name": l}) + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + p := r.URL.Path + switch { + case r.Method == "GET" && p == "/repos/o/r/issues": + _ = json.NewEncoder(w).Encode([]map[string]any{{"number": 7, "state": "open", "labels": append([]map[string]string{{"name": CoveredByPRLabel}}, issueLabels...)}}) + case r.Method == "GET" && p == "/repos/o/r/issues/7/timeline": + _ = json.NewEncoder(w).Encode([]map[string]any{{ + "event": "cross-referenced", + "source": map[string]any{"issue": map[string]any{"number": prNum, "pull_request": map[string]any{"url": "u"}}}, + }}) + case r.Method == "GET" && p == "/repos/o/r/issues/7/comments": + _ = json.NewEncoder(w).Encode([]map[string]string{}) + case r.Method == "GET" && p == "/repos/o/r/pulls/"+strconv.Itoa(prNum): + _ = json.NewEncoder(w).Encode(map[string]any{ + "number": prNum, "title": "parity batch", "body": body, "merged": true, + "merged_at": "2026-11-01T10:25:46Z", "merge_commit_sha": "5bd573a1565f0000", + "user": map[string]string{"login": "hive[bot]"}, + "merged_by": map[string]string{"login": "hive[bot]"}, + "head": map[string]string{"ref": "hive/7-dev-tools-parity", "sha": "headsha"}, + "base": map[string]any{"ref": "main", "repo": map[string]string{"full_name": "o/r"}}, + }) + case r.Method == "GET" && p == "/repos/o/r/issues/7": + _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "open", "labels": issueLabels}) + case r.Method == "PATCH" && p == "/repos/o/r/issues/7": + mu.Lock() + closed = true + mu.Unlock() + _ = json.NewEncoder(w).Encode(map[string]any{"number": 7, "state": "closed"}) + case r.Method == "POST" && strings.HasSuffix(p, "/comments"): + _ = json.NewEncoder(w).Encode(map[string]any{"id": 1}) + case r.Method == "POST" && strings.HasSuffix(p, "/labels"): + _, _ = w.Write([]byte(`[{"name":"x"}]`)) + case r.Method == "GET" && strings.Contains(p, "/labels/"): + _, _ = w.Write([]byte(`{"name":"x"}`)) + case r.Method == "DELETE": + _, _ = w.Write([]byte(`[]`)) + default: + t.Errorf("unexpected request %s %s", r.Method, p) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + results := c.CloseOnMergeBackfill(context.Background(), CloseOnMergeOptions{Identity: HiveIdentity{AppLogin: "hive[bot]"}}) + if len(results) == 0 { + t.Fatalf("no result") + } + mu.Lock() + defer mu.Unlock() + return results[len(results)-1], closed +} + +func TestCloseOnMergeRefsNeverCloses490(t *testing.T) { + const ( + closes = "Closes #7: the last outputs are published." + refs = "Refs #7 (the accepted parity batch) — ten outputs remain unpublished." + partOf = "Part of #7: publishes five of the outputs." + leaveOpn = "Publishes five outputs; #7 stays open for the rest." + branch = "Publishes the remaining outputs." // claimed only by the branch name + ) + // The PR-detail cache is process-wide, so every case gets its own PR. + for i, tc := range []struct { + name string + body string + labels []string + close bool + reason string + }{ + {name: "Closes", body: closes, close: true, reason: "closed_completed"}, + {name: "Refs (#490)", body: refs, reason: "partially_addressed"}, + {name: "Part of", body: partOf, reason: "partially_addressed"}, + {name: "leave-open wording", body: leaveOpn, reason: "partially_addressed"}, + {name: "branch claim only", body: branch, close: true, reason: "closed_completed"}, + {name: "Closes + verified-open", body: closes, labels: []string{VerifiedOpenLabel}, close: true, reason: "closed_completed"}, + {name: "Refs + verified-open", body: refs, labels: []string{VerifiedOpenLabel}, reason: "partially_addressed"}, + {name: "branch claim + verified-open", body: branch, labels: []string{VerifiedOpenLabel}, reason: "issue_marked_open"}, + {name: "branch claim + partially-addressed", body: branch, labels: []string{PartiallyAddressedLabel}, reason: "issue_marked_open"}, + } { + t.Run(tc.name, func(t *testing.T) { + res, closed := runBackfill490(t, 4900+i, tc.body, tc.labels) + if closed != tc.close || res.Reason != tc.reason { + t.Fatalf("closed=%v reason=%q (action %q), want closed=%v reason=%q", closed, res.Reason, res.Action, tc.close, tc.reason) + } + }) + } +} + +// The close path and the coverage comment read one decision. +func TestPRLeavesIssueOpen(t *testing.T) { + for _, tc := range []struct { + text, prRepo, issueRepo string + want bool + }{ + {"Refs #490", "o/r", "o/r", true}, + {"Part of #490", "o/r", "o/r", true}, + {"Refs o/r#490", "o/other", "o/r", true}, + {"This leaves #490 open.", "o/r", "o/r", true}, + {"This does not close #490.", "o/r", "o/r", false}, // GitHub reads "close #490" as a keyword + {"Closes #490", "o/r", "o/r", false}, + {"Closes #490. Refs #491", "o/r", "o/r", false}, + {"Refs #491", "o/r", "o/r", false}, + {"Refs #490", "o/r", "o/x", false}, + } { + if got := prLeavesIssueOpen(tc.text, tc.prRepo, tc.issueRepo, 490); got != tc.want { + t.Errorf("prLeavesIssueOpen(%q, %s → %s) = %v, want %v", tc.text, tc.prRepo, tc.issueRepo, got, tc.want) + } + } +} diff --git a/src/pkg/github/close_on_merge_test.go b/src/pkg/github/close_on_merge_test.go index dfaf0276f5..49d55bc3fa 100644 --- a/src/pkg/github/close_on_merge_test.go +++ b/src/pkg/github/close_on_merge_test.go @@ -218,7 +218,7 @@ func TestCloseOnMergeBackfillClosesLinkedMergedPR(t *testing.T) { _ = json.NewEncoder(w).Encode([]map[string]string{}) case r.Method == "GET" && r.URL.Path == "/repos/o/r/pulls/42": _ = json.NewEncoder(w).Encode(map[string]any{ - "number": 42, "title": "fix", "body": "Refs #7", "merged": true, + "number": 42, "title": "fix", "body": "Fixes #7", "merged": true, "merged_at": "2026-10-09T12:00:00Z", "merge_commit_sha": "abcdef0123456789", "user": map[string]string{"login": "contributor"}, "merged_by": map[string]string{"login": "hive[bot]"}, From 5ce720edde16a02c5cd0d7f9f530ccf3347087a6 Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 18:26:05 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix(close-on-merge):=20owner?= =?UTF-8?q?-only,=20bounded=20wrong-closes=20report;=20fix=20API=20citatio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Security review on #16835: the report fanned out unbounded GitHub reads on the installation token for any read-role session. - requireOwnerRole; 60 s request deadline - at most 500 closed issues inspected per scan (truncated + note past it) - 10-minute cache per days value; the lock is held for the scan so concurrent requests read the cache; failed scans are not cached - document that GitHub's since filters on updated_at - re-anchor the api-reference citations the new route line shifted Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- dashboard/openapi.json | 24 +++- src/docs/api-reference.md | 2 +- src/docs/api-reference/dashboard-misc.md | 4 +- src/docs/api-reference/packs-and-acmm.md | 2 +- src/docs/hive-merge.md | 5 +- src/pkg/dashboard/api_close_on_merge_audit.go | 63 +++++++++- .../api_close_on_merge_audit_test.go | 111 +++++++++++++++--- src/pkg/github/close_on_merge_audit.go | 76 ++++++++---- src/pkg/github/close_on_merge_audit_test.go | 39 +++++- 9 files changed, 274 insertions(+), 52 deletions(-) diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 58262ba1c3..73081aa0b3 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -36946,7 +36946,7 @@ "Governor" ], "summary": "List issues close-on-merge closed on a PR that left them open", - "description": "Read-only report for maintainers (projectbluefin/utah-packages#490): issues the hive closed in the last ?days=N (default 30, 1-90) with \"Fixed by #N\" where PR #N only references the issue (Refs / Part of) or says it stays open, or where the issue carried hive/verified-open or hive/partially-addressed and the PR had no closing keyword for it. The hive reopens nothing; maintainers decide.", + "description": "Owner only. Read-only report for maintainers (projectbluefin/utah-packages#490): issues the hive closed in the last ?days=N (default 30, 1-90) with \"Fixed by #N\" where PR #N only references the issue (Refs / Part of) or says it stays open, or where the issue carried hive/verified-open or hive/partially-addressed and the PR had no closing keyword for it. The scan reads GitHub on the installation token: it runs under a 60 s deadline, inspects at most 500 closed issues (truncated with a note past that) and caches each window's answer for 10 minutes (cached_at). GitHub's issue-list since filters on updated_at; the close time is checked locally. The hive reopens nothing; maintainers decide.", "parameters": [ { "name": "days", @@ -36976,7 +36976,7 @@ }, "complete": { "type": "boolean", - "description": "false when a repository listing failed; error says which" + "description": "false when a repository listing failed (error says which) or the scan was truncated" }, "error": { "type": "string" @@ -37015,6 +37015,23 @@ } } } + }, + "inspected": { + "type": "integer", + "description": "Closed issues inspected in the window" + }, + "truncated": { + "type": "boolean", + "description": "The 500-issue cap stopped the scan; items covers only what was inspected" + }, + "note": { + "type": "string", + "description": "Present when truncated" + }, + "cached_at": { + "type": "string", + "format": "date-time", + "description": "Present when the answer came from the 10-minute cache" } } } @@ -37026,6 +37043,9 @@ }, "503": { "description": "GitHub client not configured" + }, + "403": { + "description": "Not a verified owner" } } } diff --git a/src/docs/api-reference.md b/src/docs/api-reference.md index 14da3a2ccd..d873ee5479 100644 --- a/src/docs/api-reference.md +++ b/src/docs/api-reference.md @@ -318,7 +318,7 @@ Read the result from `GET /api/kick/{agent}/status`, which returns `status` of ` | `POST` | `/api/repos/{owner}/{repo}/items/{number}/hold` | Owner or repo write/maintain/admin | Add or remove a dashboard hold chip for an issue or PR. Body `{"held": true}` adds the hive's exact-match canonical `hive-pause/` label; `{"held": false}` removes exactly the labels causing the hold (`hive-pause/` and/or generic hold labels) and never removes `hive/` provenance. Every toggle writes an audit-log entry; the #8851 upgrade migration writes `/data/hive-hold-migration-.json` with audit-backed, provenance-only, and ambiguous legacy `hive/` classifications. | `pkg/dashboard/api.go:364`, `pkg/dashboard/api_repo_hold.go` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/decision` | Owner or repo write/maintain/admin, signed-in GitHub user | Record an operator decision from the activity monitor's row menu / selection bar. Body `{"decision": "approve"|"direction"|"close-superseded"|"close-wontfix", "comment": "…", "type": "issue"|"pr", "superseded_by": "#458"}`. On an issue parked at 045, `approve`/`direction` act exactly as a maintainer's `/hive approve` / `/hive decision ` comment would (labels, `approved-direction`, stage-engine Unparked fact, reply comment naming the operator). On a PR parked at 085 (`type: "pr"`), `approve` posts the `/lgtm` and `/approve` commands as two comments attributed to the operator, records the approval against the head as the human answer to a `requires_human` / decision-requested verdict (the lanes and the stage engine honour it like a trusted merger's Approve review), lifts `needs-human`, clears the sentinel label and acknowledges the bundle's sentinel findings for that head, re-derives the stage and drops the tracker cache — the response adds `released` (`approval-recorded`, `needs-human`, `sentinel-label`, `sentinel-acknowledged`) and `stage`; the merge gate decides from there (no review as the operator, no merge) — and `direction` records the text as a human change request against the head — the `/hive decision` fix lane — on a hive-authored PR only (409 otherwise). The close variants post a comment naming the operator (and the superseding PR when `superseded_by` is given) and close the item as not planned. Never merges. Every decision writes an audit-log entry (`repo_item_decision_*`). | `pkg/dashboard/api.go:365`, `pkg/dashboard/api_repo_item_decision.go` | | `POST` / `GET` | `/api/repos/{owner}/{repo}/items/{number}/decide-delegated` | Same as `…/hold` (owner or repo write), signed-in GitHub user; `decisions.delegate.enabled` (403 when off) | *Let hive decide*: enqueue a delegated-decision job in which the hive decides the parked item itself as the owner's delegate under `pkg/delegatedecision.PolicyText` and records it as a human decision (`/hive decision` comment with the `` marker, park labels off, `triage/accepted`, queue approval on a PR head). Body `{"type": "issue"|"pr", "reason", "caption", "sentinel_reason"}`. `POST` answers 202 with the job (`{id, status: "running"|"done"|"failed", decision: {verdict, rule, text, rationale, actions, keeps_park}, notes, error}`); 409 with the running job while one is in flight; `GET` reads it. Rails: never merges, refuses items wearing `hold`, never grants secrets/permissions (`needs-owner` + `hold` instead), recommends only on hive policy/governance questions (keeps 085). Audited as `repo_item_decision_delegated[_failed]`. See [dashboard-sections.md](dashboard-sections.md#let-hive-decide). | `pkg/dashboard/api.go:329`, `pkg/dashboard/api_repo_item_decide_delegated.go` | -| `POST` | `/api/repos/{owner}/{repo}/items/{number}/accept-triage` | Owner or repo write/maintain/admin | Accept triage on an issue from the activity monitor's row menu — what a maintainer accepting triage on GitHub does by hand: adds the reporter-trust triage label (`triage/accepted`, or the first of `issue_filter.reporter_trust.untrusted_require_labels`; the label is created in the repo if missing) and removes the awaiting label (`needs-triage` / `reporter_trust.awaiting_label`) and a Hive-written `stage/015-triage-needed`. Empty body. Returns `{ok, repo, number, acceptLabel, added, removed, alreadyAccepted, minStatusSeq}`; an issue already carrying the accept label with nothing left to remove is a no-op (`alreadyAccepted: true`, no write, no audit). 404 for a pull request or an unknown item. The status snapshot's labels are patched so the next reconcile reads 010 Actionable before the engine's own 015 → 010 derivation. Every write is audited as `repo_item_accept_triage` under the dashboard user. | `pkg/dashboard/api.go:576`, `pkg/dashboard/api_repo_item_accept_triage.go` | +| `POST` | `/api/repos/{owner}/{repo}/items/{number}/accept-triage` | Owner or repo write/maintain/admin | Accept triage on an issue from the activity monitor's row menu — what a maintainer accepting triage on GitHub does by hand: adds the reporter-trust triage label (`triage/accepted`, or the first of `issue_filter.reporter_trust.untrusted_require_labels`; the label is created in the repo if missing) and removes the awaiting label (`needs-triage` / `reporter_trust.awaiting_label`) and a Hive-written `stage/015-triage-needed`. Empty body. Returns `{ok, repo, number, acceptLabel, added, removed, alreadyAccepted, minStatusSeq}`; an issue already carrying the accept label with nothing left to remove is a no-op (`alreadyAccepted: true`, no write, no audit). 404 for a pull request or an unknown item. The status snapshot's labels are patched so the next reconcile reads 010 Actionable before the engine's own 015 → 010 derivation. Every write is audited as `repo_item_accept_triage` under the dashboard user. | `pkg/dashboard/api.go:579`, `pkg/dashboard/api_repo_item_accept_triage.go` | | `GET` | `/api/acmm/evaluation` | Dashboard auth/session | ACMMEvaluation — combined codebase + operational result, cached server-side for 1 hour (`acmmEvalTTL`). `?refresh=1` (the dashboard's "🔄 Re-evaluate" button, #5877) bypasses the hourly TTL but is debounced server-side: requests within 1 minute of the last evaluation (`acmmRefreshDebounce`) still serve the cache, since a full refresh costs up to ~29 GitHub GetContents calls per repo. The response's `last_evaluated_at` timestamp reports when the cached evaluation was computed | `pkg/dashboard/api.go:373`, `pkg/dashboard/api_acmm_eval.go` | | `POST` | `/api/acmm/issue` | Owner only | ACMMCreate Issue — files on GitHub or, with `governor.acmm.issue_tracker: work_source` / body `tracker: "work_source"` on a Linear-sourced hive, on Linear; response `tracker` says which. See [ACMM policy matrix](acmm-policy-matrix.md#where-acmm-gap-issues-are-filed) | `pkg/dashboard/api.go:374` | | `GET` | `/api/acmm-recommendation` | Dashboard auth/session | Advisory level-up recommendation (`acmmadvisor.Recommendation`, JSON): never changes the applied level — see [ACMM advisor](acmm-advisor.md) | `pkg/dashboard/api.go:375` | diff --git a/src/docs/api-reference/dashboard-misc.md b/src/docs/api-reference/dashboard-misc.md index 8f1ef7ea77..68a3e8c003 100644 --- a/src/docs/api-reference/dashboard-misc.md +++ b/src/docs/api-reference/dashboard-misc.md @@ -50,8 +50,8 @@ The monitor renders CI and Review concurrently on one shared PR row; see | `GET` | `/api/upstream-watch` | Owner only | Upstream-watch divergence: per watched fork the upstream followed, the watermark and last-run time, surfaced/ported/dismissed/skipped counts, and the recent upstream refs with their upstream URL, diff URL, fork issue number and state. Read-only — it reads the config and the watch state file, never GitHub. Also carries `interval`, `project_repos` and each repo's configured entry (`config`) for the editor. | `pkg/dashboard/api.go:321` | | `PUT` | `/api/config/upstream-watch` | Owner only | Upstream Watch Config Update: edit `upstream_watch` from the Features tab. Body `{enabled?, interval?, repos?: {: {upstream?, sources?, pr_labels?, max_issues_per_run?, label?} \| null}}` — absent keys are unchanged, `null` removes a repo, keys are stored as the bare repo name. Validated with the loader's rules before the config changes (400 on failure), then persisted and audited as `config_upstream_watch`. | `pkg/dashboard/api.go:322` | -| `GET` | `/api/hotspots` | Dashboard auth/session | Merge-hotspot advisor report (`?repo=owner/name` optional): files repeatedly touched by concurrent PRs over the last `governor.hotspot_window_days`, each with `prs` (distinct PRs), `pr_numbers`, `overlaps` (PR pairs open at the same time), `conflicts` (rebase conflicts blamed on the file), `lines`, `kind` (`code`/`doc`/`lock`/`generated`/`data`), `score`, `symbol_groups`, `recommendation` (`split-code`/`split-doc`/`split-source`/`report-only`/`none`), `reason`, `generated_from`, `source_files` (files under `generated_from`, when known), `issue_number`/`issue_url` of the modularize issue filed, `last_updated`; top-level `count`/`actionable`/`filed`. Derived from the merge train's own PR file lists and the rebase follow-through's conflict records — never lists PRs or files from GitHub. See [operator-reference.md § Merge-hotspot advisor](../operator-reference.md#merge-hotspot-advisor-governorhotspot_). | `pkg/dashboard/api.go:567` | -| `GET` | `/api/close-on-merge/wrong-closes` | Dashboard auth/session | Read-only report of issues close-on-merge closed on a PR that did not close them (projectbluefin/utah-packages#490): for each watched repo, issues closed in the last `?days=N` (default 30, 1–90) whose hive "Fixed by #N" comment names a merged PR that only references the issue (`Refs`/`Part of`) or says it stays open (`reason: pr_leaves_issue_open`), or that carries `hive/verified-open` / `hive/partially-addressed` and was closed by a PR with no closing keyword for it (`reason: issue_marked_open`). Returns `{since, days, items[], complete}`, each item `{repo, issue, title, url, pr, closed_at, reason}`; `complete:false` with `error` when a listing failed. Maintainers reopen what they agree with; the hive reopens nothing | `pkg/dashboard/api_close_on_merge_audit.go:24` | +| `GET` | `/api/hotspots` | Dashboard auth/session | Merge-hotspot advisor report (`?repo=owner/name` optional): files repeatedly touched by concurrent PRs over the last `governor.hotspot_window_days`, each with `prs` (distinct PRs), `pr_numbers`, `overlaps` (PR pairs open at the same time), `conflicts` (rebase conflicts blamed on the file), `lines`, `kind` (`code`/`doc`/`lock`/`generated`/`data`), `score`, `symbol_groups`, `recommendation` (`split-code`/`split-doc`/`split-source`/`report-only`/`none`), `reason`, `generated_from`, `source_files` (files under `generated_from`, when known), `issue_number`/`issue_url` of the modularize issue filed, `last_updated`; top-level `count`/`actionable`/`filed`. Derived from the merge train's own PR file lists and the rebase follow-through's conflict records — never lists PRs or files from GitHub. See [operator-reference.md § Merge-hotspot advisor](../operator-reference.md#merge-hotspot-advisor-governorhotspot_). | `pkg/dashboard/api.go:570` | +| `GET` | `/api/close-on-merge/wrong-closes` | Owner (verified owner role; 403 otherwise) | Read-only report of issues close-on-merge closed on a PR that did not close them (projectbluefin/utah-packages#490): for each watched repo, issues closed in the last `?days=N` (default 30, 1–90) whose hive "Fixed by #N" comment names a merged PR that only references the issue (`Refs`/`Part of`) or says it stays open (`reason: pr_leaves_issue_open`), or that carries `hive/verified-open` / `hive/partially-addressed` and was closed by a PR with no closing keyword for it (`reason: issue_marked_open`). The scan reads GitHub on the installation token, so it runs under a 60 s deadline, inspects at most 500 closed issues (`truncated: true` with a `note` past that) and caches each window's answer for 10 minutes (`cached_at` on a cached answer; a failed scan is not cached). GitHub's issue-list `since` filters on `updated_at`, so the close time is checked locally. Returns `{since, days, items[], inspected, truncated, complete, note?, error?, cached_at?}`, each item `{repo, issue, title, url, pr, closed_at, reason}`. Maintainers reopen what they agree with; the hive reopens nothing | `pkg/dashboard/api.go:567` | | `GET` | `/api/widget` | Dashboard auth/session | Widget | `pkg/dashboard/api.go:130` | | `GET` | `/api/pane/{agent}` | Dashboard auth/session | Pane | `pkg/dashboard/api.go:135` | | `GET` | `/api/role` | Dashboard auth/session | Role | `pkg/dashboard/api.go:149` | diff --git a/src/docs/api-reference/packs-and-acmm.md b/src/docs/api-reference/packs-and-acmm.md index c7aace3eea..958b4bbe4f 100644 --- a/src/docs/api-reference/packs-and-acmm.md +++ b/src/docs/api-reference/packs-and-acmm.md @@ -22,7 +22,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/repos/{owner}/{repo}/items/{number}/decide-delegated/plan` | Same as `…/decide-delegated` | Preview for *Let hive decide*: runs the same owner-delegate policy with **no side effects** (nothing written, no job) and answers `{would, rule, acting, title, summary, why, consequences[], ladder, mergeable_as_is, alternatives[], writes: false, prior, notes}` — `summary` is one verb-first sentence with no "Hive would:" prefix (*Send #15332 back to its author with one instruction: make CI green, nothing else.*), `why` one short line (*CI is red.*), `consequences` at most two items, `ladder` the rung that decided with its `ruled_out` list, and `mergeable_as_is` whether an as-is merge is honest (the queue-merge and merge-now alternatives are dropped when it is not). The dialog shows **Cancel** / **Let hive do that**, plus **Approve and let hive merge** (or **Merge now as maintainer** when the hive would approve anyway) only when `mergeable_as_is`; only then does the SPA post `…/decide-delegated` with `chosen` naming the button. See [dashboard-sections.md](../dashboard-sections/activity-monitor.md#let-hive-decide). | `pkg/dashboard/api.go:369`, `pkg/dashboard/api_repo_item_decide_delegated_plan.go` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/blocker/recheck` | Same as `…/hold` (owner or repo write), signed-in GitHub user | *Re-check permissions* on an issue parked on a capability blocker (#15099): probes the GitHub App installation's permissions; when the missing permission is granted, clears every ledger entry blocked on it and un-parks the item (`{result: {outcome: "released", …}}`), otherwise `{result: {outcome: "no-effect", reason: "blocker", options: [...]}}`; 404 when the item has no blocker, 409 when the blocker is not a capability gap | `pkg/dashboard/api.go:370` | | `POST` | `/api/repos/{owner}/{repo}/items/{number}/blocker/close-partial` | Same as `…/hold` (owner or repo write), signed-in GitHub user | Option (c) on a capability blocker (#15099): labels the issue `hive/partially-addressed`, posts the operator's `{"comment"}` naming what remains and closes it; the ledger entry is cleared | `pkg/dashboard/api.go:371` | -| `POST` | `/api/repos/{owner}/{repo}/items/{number}/accept-triage` | Owner or repo write/maintain/admin | Accept triage on an issue from the activity monitor's row menu — what a maintainer accepting triage on GitHub does by hand: adds the reporter-trust triage label (`triage/accepted`, or the first of `issue_filter.reporter_trust.untrusted_require_labels`; the label is created in the repo if missing) and removes the awaiting label (`needs-triage` / `reporter_trust.awaiting_label`) and a Hive-written `stage/015-triage-needed`. Empty body. Returns `{ok, repo, number, acceptLabel, added, removed, alreadyAccepted, minStatusSeq}`; an issue already carrying the accept label with nothing left to remove is a no-op (`alreadyAccepted: true`, no write, no audit). 404 for a pull request or an unknown item. The status snapshot's labels are patched so the next reconcile reads 010 Actionable before the engine's own 015 → 010 derivation. Every write is audited as `repo_item_accept_triage` under the dashboard user. | `pkg/dashboard/api.go:576`, `pkg/dashboard/api_repo_item_accept_triage.go` | +| `POST` | `/api/repos/{owner}/{repo}/items/{number}/accept-triage` | Owner or repo write/maintain/admin | Accept triage on an issue from the activity monitor's row menu — what a maintainer accepting triage on GitHub does by hand: adds the reporter-trust triage label (`triage/accepted`, or the first of `issue_filter.reporter_trust.untrusted_require_labels`; the label is created in the repo if missing) and removes the awaiting label (`needs-triage` / `reporter_trust.awaiting_label`) and a Hive-written `stage/015-triage-needed`. Empty body. Returns `{ok, repo, number, acceptLabel, added, removed, alreadyAccepted, minStatusSeq}`; an issue already carrying the accept label with nothing left to remove is a no-op (`alreadyAccepted: true`, no write, no audit). 404 for a pull request or an unknown item. The status snapshot's labels are patched so the next reconcile reads 010 Actionable before the engine's own 015 → 010 derivation. Every write is audited as `repo_item_accept_triage` under the dashboard user. | `pkg/dashboard/api.go:579`, `pkg/dashboard/api_repo_item_accept_triage.go` | | `GET` | `/api/acmm/evaluation` | Dashboard auth/session | ACMMEvaluation — combined codebase + operational result, cached server-side for 1 hour (`acmmEvalTTL`). `?refresh=1` (the dashboard's "🔄 Re-evaluate" button, #5877) bypasses the hourly TTL but is debounced server-side: requests within 1 minute of the last evaluation (`acmmRefreshDebounce`) still serve the cache, since a full refresh costs up to ~29 GitHub GetContents calls per repo. The response's `last_evaluated_at` timestamp reports when the cached evaluation was computed | `pkg/dashboard/api.go:373`, `pkg/dashboard/api_acmm_eval.go` | | `POST` | `/api/acmm/issue` | Owner only | ACMMCreate Issue — files on GitHub or, with `governor.acmm.issue_tracker: work_source` / body `tracker: "work_source"` on a Linear-sourced hive, on Linear; response `tracker` says which. See [ACMM policy matrix](../acmm-policy-matrix.md#where-acmm-gap-issues-are-filed) | `pkg/dashboard/api.go:374` | | `GET` | `/api/acmm-recommendation` | Dashboard auth/session | Advisory level-up recommendation (`acmmadvisor.Recommendation`, JSON): never changes the applied level — see [ACMM advisor](../acmm-advisor.md) | `pkg/dashboard/api.go:375` | diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 9982b89a2f..ca8c905190 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -1165,8 +1165,9 @@ whose own text closes it, never by a branch name, claim or comment link alone. Every sweep compares references as `owner/repo`, whatever name the repository is configured under: the backfill sweep passed the short name and closed projectbluefin/utah-packages#490 on a `Refs #490` PR. -`GET /api/close-on-merge/wrong-closes?days=N` lists the issues closed that way -for maintainers to reopen; the hive reopens nothing itself. +`GET /api/close-on-merge/wrong-closes?days=N` (owner only; at most 500 closed +issues per scan, cached for 10 minutes) lists the issues closed that way for +maintainers to reopen; the hive reopens nothing itself. The tracker shows the consequence honestly. A follow-up or duplicate PR whose body names an issue an *earlier* PR already delivered — `Refs #15422` on diff --git a/src/pkg/dashboard/api_close_on_merge_audit.go b/src/pkg/dashboard/api_close_on_merge_audit.go index 1de44409fe..4112ec5050 100644 --- a/src/pkg/dashboard/api_close_on_merge_audit.go +++ b/src/pkg/dashboard/api_close_on_merge_audit.go @@ -5,23 +5,50 @@ import ( "net/http" "strconv" "strings" + "sync" "time" ghpkg "github.com/hivecommons/hive/pkg/github" ) +// The report reads GitHub on the installation token: one issue listing per +// repo, then a comments read (and a PR read) per closed issue. It is owner +// only, bounded in time and size, and cached per window so repeated GETs do +// not re-scan. +const ( + closeOnMergeAuditDeadline = 60 * time.Second + closeOnMergeAuditMaxIssues = 500 + closeOnMergeAuditCacheTTL = 10 * time.Minute +) + // closeOnMergeWrongClosesFunc reads the report from GitHub; tests swap it. -var closeOnMergeWrongClosesFunc = func(ctx context.Context, c *ghpkg.Client, since time.Time) ([]ghpkg.CloseOnMergeWrongClose, error) { - return c.CloseOnMergeWrongCloses(ctx, since) +var closeOnMergeWrongClosesFunc = func(ctx context.Context, c *ghpkg.Client, opts ghpkg.CloseOnMergeAuditOptions) (ghpkg.CloseOnMergeAudit, error) { + return c.CloseOnMergeWrongCloses(ctx, opts) +} + +type closeOnMergeAuditEntry struct { + at time.Time + resp map[string]interface{} } +// closeOnMergeAuditCache holds one answer per `days` value. Its lock is held +// for the whole scan, so concurrent requests wait for the first and are then +// served from the cache instead of scanning again. +var closeOnMergeAuditCache = struct { + sync.Mutex + entries map[int]closeOnMergeAuditEntry +}{entries: map[int]closeOnMergeAuditEntry{}} + // handleCloseOnMergeWrongCloses serves GET /api/close-on-merge/wrong-closes?days=N // (default 30, at most 90): the issues the hive closed with "Fixed by #N" // where PR #N only referenced them or said they stay open, or where the issue // was marked hive/verified-open or hive/partially-addressed and the PR did not -// close it (projectbluefin/utah-packages#490). Read-only: maintainers reopen -// what they agree with; the hive reopens nothing. +// close it (projectbluefin/utah-packages#490). Owner only and read-only: +// maintainers reopen what they agree with; the hive reopens nothing. func (s *Server) handleCloseOnMergeWrongCloses(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } days := 30 if raw := strings.TrimSpace(r.URL.Query().Get("days")); raw != "" { n, err := strconv.Atoi(raw) @@ -35,14 +62,38 @@ func (s *Server) handleCloseOnMergeWrongCloses(w http.ResponseWriter, r *http.Re jsonError(w, "GitHub client not configured", http.StatusServiceUnavailable) return } + cache := &closeOnMergeAuditCache + cache.Lock() + defer cache.Unlock() + if e, ok := cache.entries[days]; ok && time.Since(e.at) < closeOnMergeAuditCacheTTL { + resp := map[string]interface{}{} + for k, v := range e.resp { + resp[k] = v + } + resp["cached_at"] = e.at.UTC().Format(time.RFC3339) + jsonResponse(w, resp) + return + } + ctx, cancel := context.WithTimeout(r.Context(), closeOnMergeAuditDeadline) + defer cancel() since := time.Now().Add(-time.Duration(days) * 24 * time.Hour) - items, err := closeOnMergeWrongClosesFunc(r.Context(), s.deps.GHClient, since) + audit, err := closeOnMergeWrongClosesFunc(ctx, s.deps.GHClient, ghpkg.CloseOnMergeAuditOptions{Since: since, MaxIssues: closeOnMergeAuditMaxIssues}) + items := audit.Items if items == nil { items = []ghpkg.CloseOnMergeWrongClose{} } - resp := map[string]interface{}{"since": since.UTC().Format(time.RFC3339), "days": days, "items": items, "complete": err == nil} + resp := map[string]interface{}{ + "since": since.UTC().Format(time.RFC3339), "days": days, "items": items, + "inspected": audit.Inspected, "truncated": audit.Truncated, "complete": err == nil && !audit.Truncated, + } + if audit.Truncated { + resp["note"] = "stopped after " + strconv.Itoa(closeOnMergeAuditMaxIssues) + " closed issues; ask for fewer days to cover the rest" + } if err != nil { resp["error"] = err.Error() + } else { + // Only a scan that finished is reused; a failed one is retried. + cache.entries[days] = closeOnMergeAuditEntry{at: time.Now(), resp: resp} } jsonResponse(w, resp) } diff --git a/src/pkg/dashboard/api_close_on_merge_audit_test.go b/src/pkg/dashboard/api_close_on_merge_audit_test.go index 9de2f84f02..905ef24ba4 100644 --- a/src/pkg/dashboard/api_close_on_merge_audit_test.go +++ b/src/pkg/dashboard/api_close_on_merge_audit_test.go @@ -4,43 +4,126 @@ import ( "context" "encoding/json" "net/http" + "net/http/httptest" "testing" "time" ghpkg "github.com/hivecommons/hive/pkg/github" ) -func TestCloseOnMergeWrongClosesEndpoint(t *testing.T) { +func closeOnMergeAuditGet(s *Server, path, role string, verified bool) *httptest.ResponseRecorder { + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, path, nil) + if role != "" { + req.Header.Set("X-Hive-Role", role) + } + if verified { + req.Header.Set(ownerRoleVerifiedHeader, "true") + } + s.mux.ServeHTTP(rec, req) + return rec +} + +func stubCloseOnMergeAudit(t *testing.T, audit ghpkg.CloseOnMergeAudit) (*int, *ghpkg.CloseOnMergeAuditOptions) { + t.Helper() prev := closeOnMergeWrongClosesFunc - t.Cleanup(func() { closeOnMergeWrongClosesFunc = prev }) - var gotSince time.Time - closeOnMergeWrongClosesFunc = func(_ context.Context, _ *ghpkg.Client, since time.Time) ([]ghpkg.CloseOnMergeWrongClose, error) { - gotSince = since - return []ghpkg.CloseOnMergeWrongClose{{Repo: "projectbluefin/utah-packages", Issue: 490, PR: 860, Reason: "pr_leaves_issue_open"}}, nil + closeOnMergeAuditCache.Lock() + closeOnMergeAuditCache.entries = map[int]closeOnMergeAuditEntry{} + closeOnMergeAuditCache.Unlock() + t.Cleanup(func() { + closeOnMergeWrongClosesFunc = prev + closeOnMergeAuditCache.Lock() + closeOnMergeAuditCache.entries = map[int]closeOnMergeAuditEntry{} + closeOnMergeAuditCache.Unlock() + }) + calls := 0 + got := &ghpkg.CloseOnMergeAuditOptions{} + closeOnMergeWrongClosesFunc = func(ctx context.Context, _ *ghpkg.Client, opts ghpkg.CloseOnMergeAuditOptions) (ghpkg.CloseOnMergeAudit, error) { + calls++ + *got = opts + if dl, ok := ctx.Deadline(); !ok || time.Until(dl) > closeOnMergeAuditDeadline { + t.Errorf("scan context deadline = %v (set %v), want within %v", dl, ok, closeOnMergeAuditDeadline) + } + return audit, nil } + return &calls, got +} + +func TestCloseOnMergeWrongClosesEndpoint(t *testing.T) { + calls, opts := stubCloseOnMergeAudit(t, ghpkg.CloseOnMergeAudit{Inspected: 3, Items: []ghpkg.CloseOnMergeWrongClose{{Repo: "projectbluefin/utah-packages", Issue: 490, PR: 860, Reason: "pr_leaves_issue_open"}}}) s, deps := apiServer(t) deps.GHClient = &ghpkg.Client{} - rec := doGet(s, "/api/close-on-merge/wrong-closes?days=7") + + // The scan spends the installation's API budget: owner only. + for _, tc := range []struct { + role string + verified bool + }{{"read", false}, {"read-write", false}, {"owner", false}} { + if rec := closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes", tc.role, tc.verified); rec.Code != http.StatusForbidden { + t.Fatalf("role %q verified=%v: code = %d, want 403", tc.role, tc.verified, rec.Code) + } + } + if *calls != 0 { + t.Fatalf("a refused request scanned GitHub %d times", *calls) + } + + rec := closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes?days=7", "owner", true) if rec.Code != http.StatusOK { t.Fatalf("code = %d body = %s", rec.Code, rec.Body.String()) } var resp struct { - Days int `json:"days"` - Complete bool `json:"complete"` - Items []ghpkg.CloseOnMergeWrongClose `json:"items"` + Days int `json:"days"` + Complete bool `json:"complete"` + Inspected int `json:"inspected"` + Items []ghpkg.CloseOnMergeWrongClose `json:"items"` + CachedAt string `json:"cached_at"` } if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatal(err) } - if resp.Days != 7 || !resp.Complete || len(resp.Items) != 1 || resp.Items[0].Issue != 490 { + if resp.Days != 7 || !resp.Complete || resp.Inspected != 3 || len(resp.Items) != 1 || resp.Items[0].Issue != 490 || resp.CachedAt != "" { t.Fatalf("resp = %+v", resp) } - if d := time.Since(gotSince); d < 7*24*time.Hour-time.Minute || d > 7*24*time.Hour+time.Minute { - t.Fatalf("since = %v, want 7 days ago", gotSince) + if opts.MaxIssues != closeOnMergeAuditMaxIssues { + t.Fatalf("MaxIssues = %d, want %d", opts.MaxIssues, closeOnMergeAuditMaxIssues) + } + if d := time.Since(opts.Since); d < 7*24*time.Hour-time.Minute || d > 7*24*time.Hour+time.Minute { + t.Fatalf("since = %v, want 7 days ago", opts.Since) + } + + // A repeat within the TTL is served from the cache: no GitHub calls. + rec = closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes?days=7", "owner", true) + resp.CachedAt = "" + _ = json.Unmarshal(rec.Body.Bytes(), &resp) + if *calls != 1 || resp.CachedAt == "" || len(resp.Items) != 1 { + t.Fatalf("repeat: scans = %d cached_at = %q items = %d, want 1 scan and a cached answer", *calls, resp.CachedAt, len(resp.Items)) } + // Another window is its own entry. + closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes?days=8", "owner", true) + if *calls != 2 { + t.Fatalf("days=8 scans = %d, want 2", *calls) + } + for _, bad := range []string{"0", "91", "x"} { - if rec := doGet(s, "/api/close-on-merge/wrong-closes?days="+bad); rec.Code != http.StatusBadRequest { + if rec := closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes?days="+bad, "owner", true); rec.Code != http.StatusBadRequest { t.Errorf("days=%s: code = %d, want 400", bad, rec.Code) } } } + +// A capped scan says so and is not reported complete. +func TestCloseOnMergeWrongClosesEndpointTruncated(t *testing.T) { + stubCloseOnMergeAudit(t, ghpkg.CloseOnMergeAudit{Inspected: closeOnMergeAuditMaxIssues, Truncated: true}) + s, deps := apiServer(t) + deps.GHClient = &ghpkg.Client{} + rec := closeOnMergeAuditGet(s, "/api/close-on-merge/wrong-closes", "owner", true) + var resp struct { + Complete bool `json:"complete"` + Truncated bool `json:"truncated"` + Note string `json:"note"` + } + _ = json.Unmarshal(rec.Body.Bytes(), &resp) + if rec.Code != http.StatusOK || resp.Complete || !resp.Truncated || resp.Note == "" { + t.Fatalf("code=%d resp=%+v, want truncated with a note and complete=false", rec.Code, resp) + } +} diff --git a/src/pkg/github/close_on_merge_audit.go b/src/pkg/github/close_on_merge_audit.go index a7d4d9eec2..285144cece 100644 --- a/src/pkg/github/close_on_merge_audit.go +++ b/src/pkg/github/close_on_merge_audit.go @@ -31,23 +31,46 @@ type CloseOnMergeWrongClose struct { var closeOnMergeFixedByRE = regexp.MustCompile(`Fixed by #(\d+) \(merged`) -// CloseOnMergeWrongCloses lists the issues the hive closed since `since` with -// its close-on-merge comment ("Fixed by #N (merged …)") where PR #N did not -// close them (projectbluefin/utah-packages#490: `Refs #490` on #860). It only -// reads: closed issues per watched repo, the close comment, the PR. -func (c *Client) CloseOnMergeWrongCloses(ctx context.Context, since time.Time) ([]CloseOnMergeWrongClose, error) { +// CloseOnMergeAuditOptions bounds one report. +type CloseOnMergeAuditOptions struct { + // Since is the start of the window. GitHub's issue-list `since` filters on + // updated_at, so the listing also returns issues closed earlier and + // updated since; the close time is checked locally against Since. + Since time.Time + // MaxIssues caps how many closed issues are inspected (each costs a + // comments read, and a PR read when the hive's close note names one). + // 0 means no cap. + MaxIssues int +} + +// CloseOnMergeAudit is one report: the wrong closes found and how much of +// the window was inspected. +type CloseOnMergeAudit struct { + Items []CloseOnMergeWrongClose `json:"items"` + Inspected int `json:"inspected"` + // Truncated is true when MaxIssues stopped the scan before the window + // was covered; Items then lists only what was inspected. + Truncated bool `json:"truncated"` +} + +// CloseOnMergeWrongCloses lists the issues the hive closed since opts.Since +// with its close-on-merge comment ("Fixed by #N (merged …)") where PR #N did +// not close them (projectbluefin/utah-packages#490: `Refs #490` on #860). It +// only reads: closed issues per watched repo, the close comment, the PR. +func (c *Client) CloseOnMergeWrongCloses(ctx context.Context, opts CloseOnMergeAuditOptions) (CloseOnMergeAudit, error) { + var out CloseOnMergeAudit if c == nil || c.client == nil { - return nil, nil + return out, nil } ctx = WithRESTCaller(ctx, "hive:close_on_merge_audit") - var out []CloseOnMergeWrongClose var firstErr error +repos: for _, repo := range c.activeRepos() { owner, name := c.splitRepo(repo) full := owner + "/" + name - opts := &gh.IssueListByRepoOptions{State: "closed", Since: since, ListOptions: gh.ListOptions{PerPage: 100}} + list := &gh.IssueListByRepoOptions{State: "closed", Since: opts.Since, ListOptions: gh.ListOptions{PerPage: 100}} for page := 0; page < claimSearchMaxPages; page++ { - issues, resp, err := c.client.Issues.ListByRepo(ctx, owner, name, opts) + issues, resp, err := c.client.Issues.ListByRepo(ctx, owner, name, list) if err != nil { if firstErr == nil { firstErr = err @@ -55,33 +78,40 @@ func (c *Client) CloseOnMergeWrongCloses(ctx context.Context, since time.Time) ( break } for _, issue := range issues { - if row, ok := c.closeOnMergeWrongClose(ctx, owner, name, full, since, issue); ok { - out = append(out, row) + if !closeOnMergeAuditCandidate(issue, opts.Since) { + continue + } + if opts.MaxIssues > 0 && out.Inspected >= opts.MaxIssues { + out.Truncated = true + break repos + } + out.Inspected++ + if row, ok := c.closeOnMergeWrongClose(ctx, owner, name, full, issue); ok { + out.Items = append(out.Items, row) } } if resp == nil || resp.NextPage == 0 { break } - opts.ListOptions.Page = resp.NextPage + list.ListOptions.Page = resp.NextPage } } - sort.Slice(out, func(i, j int) bool { - if out[i].Repo != out[j].Repo { - return out[i].Repo < out[j].Repo + sort.Slice(out.Items, func(i, j int) bool { + if out.Items[i].Repo != out.Items[j].Repo { + return out.Items[i].Repo < out.Items[j].Repo } - return out[i].Issue < out[j].Issue + return out.Items[i].Issue < out.Items[j].Issue }) return out, firstErr } -func (c *Client) closeOnMergeWrongClose(ctx context.Context, owner, name, full string, since time.Time, issue *gh.Issue) (CloseOnMergeWrongClose, bool) { - if issue == nil || issue.IsPullRequest() || !strings.EqualFold(issue.GetState(), "closed") { - return CloseOnMergeWrongClose{}, false - } +// closeOnMergeAuditCandidate keeps issues (not PRs) closed at or after since. +func closeOnMergeAuditCandidate(issue *gh.Issue, since time.Time) bool { + return issue != nil && !issue.IsPullRequest() && strings.EqualFold(issue.GetState(), "closed") && !issue.GetClosedAt().Time.Before(since) +} + +func (c *Client) closeOnMergeWrongClose(ctx context.Context, owner, name, full string, issue *gh.Issue) (CloseOnMergeWrongClose, bool) { closedAt := issue.GetClosedAt().Time - if closedAt.Before(since) { - return CloseOnMergeWrongClose{}, false - } pr := c.closeOnMergeCommentPR(ctx, owner, name, issue.GetNumber(), closedAt) if pr <= 0 { return CloseOnMergeWrongClose{}, false diff --git a/src/pkg/github/close_on_merge_audit_test.go b/src/pkg/github/close_on_merge_audit_test.go index 85c03e3658..5abeaf4daf 100644 --- a/src/pkg/github/close_on_merge_audit_test.go +++ b/src/pkg/github/close_on_merge_audit_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" "time" ) @@ -71,12 +72,48 @@ func TestCloseOnMergeWrongClosesReport(t *testing.T) { defer srv.Close() c := newTestClient(t, srv, "o", []string{"r"}) c.SetAppBotLogin("hive[bot]") - rows, err := c.CloseOnMergeWrongCloses(context.Background(), time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC)) + audit, err := c.CloseOnMergeWrongCloses(context.Background(), CloseOnMergeAuditOptions{Since: time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC)}) if err != nil { t.Fatalf("report: %v", err) } + rows := audit.Items + if audit.Truncated || audit.Inspected != 6 { + t.Fatalf("inspected=%d truncated=%v, want 6 closed issues in the window and no truncation", audit.Inspected, audit.Truncated) + } if len(rows) != 2 || rows[0].Issue != 490 || rows[0].PR != 860 || rows[0].Reason != "pr_leaves_issue_open" || rows[0].Repo != "o/r" || rows[1].Issue != 491 || rows[1].Reason != "issue_marked_open" { t.Fatalf("rows = %+v, want #490 (pr_leaves_issue_open) and #491 (issue_marked_open)", rows) } } + +// MaxIssues stops the scan: only that many closed issues cost a comments +// read, and the result says it was truncated. +func TestCloseOnMergeWrongClosesCap(t *testing.T) { + commentReads := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == "/repos/o/r/issues": + var issues []map[string]any + for n := 1; n <= 5; n++ { + issues = append(issues, map[string]any{"number": n, "state": "closed", "closed_at": "2026-11-01T12:00:00Z"}) + } + _ = json.NewEncoder(w).Encode(issues) + case strings.HasSuffix(r.URL.Path, "/comments"): + commentReads++ + _, _ = w.Write([]byte(`[]`)) + default: + t.Errorf("unexpected request %s", r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := newTestClient(t, srv, "o", []string{"r"}) + audit, err := c.CloseOnMergeWrongCloses(context.Background(), CloseOnMergeAuditOptions{Since: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC), MaxIssues: 2}) + if err != nil { + t.Fatal(err) + } + if !audit.Truncated || audit.Inspected != 2 || commentReads != 2 { + t.Fatalf("truncated=%v inspected=%d comment reads=%d, want true/2/2", audit.Truncated, audit.Inspected, commentReads) + } +}