diff --git a/changelog.d/fixed-issue-title-agent-prefix.md b/changelog.d/fixed-issue-title-agent-prefix.md new file mode 100644 index 0000000000..20bf4341f7 --- /dev/null +++ b/changelog.d/fixed-issue-title-agent-prefix.md @@ -0,0 +1 @@ +- Every issue the hive files now carries the filing agent in its title (`[reviewer] Review backlog (security): …`, `[governor] refactor: … is a merge hotspot …`): one shared `issueshape.TitleFor` helper tags titles idempotently at `CreateIssue`, in the review-backlog, hotspot and upstream-watch title builders, and in the egress proxy for an agent's raw `gh issue create`; open-issue title dedupe ignores the tag so items filed before it still match instead of being refiled. diff --git a/src/crux/issueshape/issueshape.go b/src/crux/issueshape/issueshape.go index 0063d357f1..02624f3e9e 100644 --- a/src/crux/issueshape/issueshape.go +++ b/src/crux/issueshape/issueshape.go @@ -98,3 +98,54 @@ func UnchosenOptionList(text string) (string, bool) { m := unchosenOptionListRE.FindString(text) return m, m != "" } + +// agentTitlePrefixRE matches the leading `[]` tag every hive-filed +// issue title carries (`[scanner] …`, `[reviewer] …`, `[selfreport] …`). A +// filer-chosen tag with spaces (`[ACMM L0] …`) counts too, so it is kept +// rather than stacked under a second tag. +var agentTitlePrefixRE = regexp.MustCompile(`^\s*\[([^\]\n]+)\]\s*`) + +// TitleFor returns title carrying a leading `[]` tag, the convention +// every issue the hive files follows so a reader can tell which agent or +// subsystem filed it. The tag is the lowercased agent (lane) name as the +// dashboard shows it. It is idempotent: a title that already starts with a +// bracketed tag — the same agent, or a tag the filer chose itself such as +// `[Tests]` or `[DCO]` — is returned unchanged, so a title is never +// double-prefixed. An empty agent leaves the title as it is. +func TitleFor(agent, title string) string { + title = strings.TrimSpace(title) + agent = strings.ToLower(strings.TrimSpace(agent)) + if agent == "" || title == "" || HasAgentPrefix(title) { + return title + } + return "[" + agent + "] " + title +} + +// HasAgentPrefix reports whether title starts with a bracketed `[tag]`. +func HasAgentPrefix(title string) bool { + return agentTitlePrefixRE.MatchString(title) +} + +// AgentPrefix returns the tag inside a title's leading `[tag]`, lowercased, +// or "" when the title carries none. +func AgentPrefix(title string) string { + m := agentTitlePrefixRE.FindStringSubmatch(title) + if m == nil { + return "" + } + return strings.ToLower(m[1]) +} + +// StripAgentPrefix returns title without its leading `[tag]`, trimmed. It is +// the key dedupe compares on so an issue filed before titles carried the +// filing agent still matches its prefixed re-filing. +func StripAgentPrefix(title string) string { + return strings.TrimSpace(agentTitlePrefixRE.ReplaceAllString(strings.TrimSpace(title), "")) +} + +// SameTitle reports whether two issue titles are the same title once the +// leading `[]` tag is ignored: `Review backlog (security): x` and +// `[reviewer] Review backlog (security): x` are one finding, not two. +func SameTitle(a, b string) bool { + return StripAgentPrefix(a) == StripAgentPrefix(b) +} diff --git a/src/crux/issueshape/issueshape_test.go b/src/crux/issueshape/issueshape_test.go index a531369445..683b3d3be5 100644 --- a/src/crux/issueshape/issueshape_test.go +++ b/src/crux/issueshape/issueshape_test.go @@ -102,3 +102,50 @@ func TestUnchosenOptionList(t *testing.T) { }) } } + +// Every issue the hive files names its filer in the title; TitleFor is the +// one helper that adds the tag, and it never adds a second one. +func TestTitleForTagsOnceAndKeepsExistingTag(t *testing.T) { + cases := []struct{ agent, title, want string }{ + {"reviewer", "Review backlog (security): token logged", "[reviewer] Review backlog (security): token logged"}, + {"Reviewer", " Review backlog (style): x ", "[reviewer] Review backlog (style): x"}, + {"reviewer", "[reviewer] Review backlog (security): token logged", "[reviewer] Review backlog (security): token logged"}, + {"tester", "[Tests] v5 test failure on 4536f90", "[Tests] v5 test failure on 4536f90"}, + {"dashboard", "[ACMM L0] Add CI/CD pipeline", "[ACMM L0] Add CI/CD pipeline"}, + {"selfreport", "[selfreport] unclassified_error_repeated: x", "[selfreport] unclassified_error_repeated: x"}, + {"governor", "refactor: src/a.go is a merge hotspot — split into 4 units", "[governor] refactor: src/a.go is a merge hotspot — split into 4 units"}, + {"", "untagged stays untagged", "untagged stays untagged"}, + {"scanner", "", ""}, + } + for _, tc := range cases { + if got := TitleFor(tc.agent, tc.title); got != tc.want { + t.Errorf("TitleFor(%q, %q) = %q, want %q", tc.agent, tc.title, got, tc.want) + } + if again := TitleFor(tc.agent, TitleFor(tc.agent, tc.title)); again != tc.want { + t.Errorf("TitleFor is not idempotent for (%q, %q): %q", tc.agent, tc.title, again) + } + } +} + +// Dedupe compares titles with the tag ignored, so an issue filed before +// titles carried the filer still matches its tagged re-filing. +func TestStripAgentPrefixAndSameTitle(t *testing.T) { + if got := StripAgentPrefix("[reviewer] Review backlog (security): x"); got != "Review backlog (security): x" { + t.Fatalf("StripAgentPrefix = %q", got) + } + if got := StripAgentPrefix(" no tag here "); got != "no tag here" { + t.Fatalf("StripAgentPrefix(untagged) = %q", got) + } + if got := AgentPrefix("[Scanner] split x.go"); got != "scanner" { + t.Fatalf("AgentPrefix = %q", got) + } + if AgentPrefix("plain") != "" || HasAgentPrefix("plain") { + t.Fatal("untagged title reported a tag") + } + if !SameTitle("Review backlog (security): x", "[reviewer] Review backlog (security): x") { + t.Fatal("legacy untagged title must match its tagged successor") + } + if SameTitle("[reviewer] Review backlog (security): x", "[reviewer] Review backlog (security): y") { + t.Fatal("different subjects must not match") + } +} diff --git a/src/crux/proxy/github_proxy_violations.go b/src/crux/proxy/github_proxy_violations.go index a76249d3ba..d8088c48ef 100644 --- a/src/crux/proxy/github_proxy_violations.go +++ b/src/crux/proxy/github_proxy_violations.go @@ -156,12 +156,13 @@ func isIssueCreateRESTWrite(method, path string) bool { // injectOriginLabel ensures an agent's issue-create JSON body carries the // canonical agent/ label and no redundant origin/ or bare -// label, so an issue filed with `gh issue create` through the proxy records -// the same provenance as one filed through the issue-request watcher. The -// body is returned unchanged (ok=false) for other routes, an unidentified -// caller, a body that is not a JSON object, or a labels field in an unexpected -// shape (GitHub accepts label objects too; those are forwarded as written -// rather than guessed at). +// label, and that its title starts with the `[]` tag every hive-filed +// issue carries, so an issue filed with `gh issue create` through the proxy +// records the same provenance as one filed through the issue-request +// watcher. The body is returned unchanged (ok=false) for other routes, an +// unidentified caller, a body that is not a JSON object, or a labels field in +// an unexpected shape (GitHub accepts label objects too; those are forwarded +// as written rather than guessed at). func injectOriginLabel(method, path string, body []byte, agentName string) ([]byte, bool) { lane := config.OriginLaneFromAgent(agentName) if lane == "" || agentName == internalCallerName || !isIssueCreateRESTWrite(method, path) { @@ -171,22 +172,37 @@ func injectOriginLabel(method, path string, body []byte, agentName string) ([]by if err := json.Unmarshal(body, &payload); err != nil || payload == nil { return body, false } + changed := false var labels []string if raw, present := payload["labels"]; present && len(bytes.TrimSpace(raw)) > 0 && string(bytes.TrimSpace(raw)) != "null" { if err := json.Unmarshal(raw, &labels); err != nil { return body, false } } - filtered := config.WithIssueOriginLabel(labels, lane) - if sameLabels(labels, filtered) { - return body, false + if filtered := config.WithIssueOriginLabel(labels, lane); !sameLabels(labels, filtered) { + encoded, err := json.Marshal(filtered) + if err != nil { + return body, false + } + payload["labels"] = encoded + changed = true } - labels = filtered - encoded, err := json.Marshal(labels) - if err != nil { + if raw, present := payload["title"]; present { + var title string + if err := json.Unmarshal(raw, &title); err == nil && title != "" { + if tagged := issueshape.TitleFor(lane, title); tagged != title { + encoded, err := json.Marshal(tagged) + if err != nil { + return body, false + } + payload["title"] = encoded + changed = true + } + } + } + if !changed { return body, false } - payload["labels"] = encoded rewritten, err := json.Marshal(payload) if err != nil { return body, false diff --git a/src/crux/proxy/origin_label_inject_test.go b/src/crux/proxy/origin_label_inject_test.go index 8f3392c84f..f08d4dcf00 100644 --- a/src/crux/proxy/origin_label_inject_test.go +++ b/src/crux/proxy/origin_label_inject_test.go @@ -21,8 +21,11 @@ func TestInjectOriginLabelAddsLaneOnceAndKeepsAgentChoice(t *testing.T) { agent string wantOK bool wantLabels []string + wantTitle string }{ - {name: "no labels field", method: http.MethodPost, path: path, body: `{"title":"t","body":"b"}`, agent: "scanner", wantOK: true, wantLabels: []string{"agent/scanner"}}, + {name: "no labels field", method: http.MethodPost, path: path, body: `{"title":"t","body":"b"}`, agent: "scanner", wantOK: true, wantLabels: []string{"agent/scanner"}, wantTitle: "[scanner] t"}, + {name: "already tagged title kept, replica suffix stripped", method: http.MethodPost, path: path, body: `{"title":"[scanner] t","body":"b","labels":["agent/scanner"]}`, agent: "scanner-2", wantOK: false}, + {name: "foreign tag not double-prefixed", method: http.MethodPost, path: path, body: `{"title":"[Tests] t","body":"b"}`, agent: "tester", wantOK: true, wantLabels: []string{"agent/tester"}, wantTitle: "[Tests] t"}, {name: "bare lane label dropped, replica suffix stripped", method: http.MethodPost, path: path, body: `{"title":"t","body":"b","labels":["agent/scanner","scanner","kind/bug"]}`, agent: "scanner-2", wantOK: true, wantLabels: []string{"agent/scanner", "kind/bug"}}, {name: "agent-supplied origin kept while bare filing lane drops", method: http.MethodPost, path: path, body: `{"title":"t","body":"b","labels":["origin/dashboard","agent/scanner","scanner"]}`, agent: "scanner", wantOK: true, wantLabels: []string{"origin/dashboard", "agent/scanner"}}, {name: "agent-supplied matching origin drops redundant origin and bare label", method: http.MethodPost, path: path, body: `{"title":"t","body":"b","labels":["origin/scanner","agent/scanner","scanner","kind/bug"]}`, agent: "scanner", wantOK: true, wantLabels: []string{"agent/scanner", "kind/bug"}}, @@ -53,8 +56,12 @@ func TestInjectOriginLabelAddsLaneOnceAndKeepsAgentChoice(t *testing.T) { if err := json.Unmarshal(got, &payload); err != nil { t.Fatalf("rewritten body is not JSON: %v: %s", err, got) } - if payload.Title != "t" || payload.Body != "b" { - t.Fatalf("title/body must survive the rewrite: %s", got) + wantTitle := tc.wantTitle + if wantTitle == "" { + wantTitle = "[" + strings.TrimSuffix(tc.agent, "-2") + "] t" + } + if payload.Title != wantTitle || payload.Body != "b" { + t.Fatalf("title = %q, want %q; body must survive the rewrite: %s", payload.Title, wantTitle, got) } if strings.Join(payload.Labels, ",") != strings.Join(tc.wantLabels, ",") { t.Fatalf("labels = %v, want %v", payload.Labels, tc.wantLabels) @@ -88,6 +95,9 @@ func TestEnforceIssueShapeForInjectsOriginAfterShapePasses(t *testing.T) { if strings.Contains(forwarded, `"origin/tester"`) || !strings.Contains(forwarded, `"agent/tester"`) || strings.Contains(forwarded, `"tester"`) || !strings.Contains(forwarded, `"kind/bug"`) { t.Fatalf("forwarded body = %s", forwarded) } + if !strings.Contains(forwarded, `"title":"[tester] Real title"`) { + t.Fatalf("forwarded title must carry the filing lane: %s", forwarded) + } if req.ContentLength != int64(len(forwarded)) { t.Fatalf("ContentLength %d != forwarded %d", req.ContentLength, len(forwarded)) } diff --git a/src/docs/hive-open-issue.md b/src/docs/hive-open-issue.md index 7e7861db7f..98176a69f9 100644 --- a/src/docs/hive-open-issue.md +++ b/src/docs/hive-open-issue.md @@ -52,7 +52,7 @@ hive-open-issue request-review --repo [--reviewer a,b] | Flag | Aliases | Applies to | Notes | | --- | --- | --- | --- | | `--repo` | `-R` | all | required | -| `--title` | `-t` | issue | required for `issue` | +| `--title` | `-t` | issue | required for `issue`; must start with `[]` (`[scanner] …`) — the watcher and the proxy's `gh issue create` path both add the tag when it is missing and never double it | | `--body` | `-b` | issue, comment | required for `issue` and `comment`; not used by `claim` | | `--body-file` | `-F` | issue, comment | reads body from a file; `-` reads stdin | | `--label` | `-l`, `--add-label` | issue, label | repeatable; comma-separated values are split | diff --git a/src/docs/review-swarm.md b/src/docs/review-swarm.md index ec121ff84c..df2e2b4968 100644 --- a/src/docs/review-swarm.md +++ b/src/docs/review-swarm.md @@ -102,7 +102,7 @@ The default human threshold is `high`. Review-triggered fix cycles use the same ### Out-of-scope backlog filing -When the relay accepts a review verdict, cited out-of-scope findings (`review_scope: "out-of-scope"` with `file` and positive `line`) are filed as follow-up issues labeled `agent/reviewer` (the reviewer lane provenance/routing label — see [labels-and-control-signals.md](labels-and-control-signals.md#label-axes); the retired `from-review`, `origin/review`, and `origin/reviewer` names are accepted in config and migrated on open items). Each issue body links back to the reviewed PR and records the perspective, severity, evidence location, and finding summary. The relay deduplicates against open issues through the normal issue-create path and also persists a finding key in `/data/review-backlog-issues.json`, so re-reviewing a new commit does not file the same backlog issue or summary comment again. +When the relay accepts a review verdict, cited out-of-scope findings (`review_scope: "out-of-scope"` with `file` and positive `line`) are filed as follow-up issues labeled `agent/reviewer` (the reviewer lane provenance/routing label — see [labels-and-control-signals.md](labels-and-control-signals.md#label-axes); the retired `from-review`, `origin/review`, and `origin/reviewer` names are accepted in config and migrated on open items). Each issue is titled `[reviewer] Review backlog (): ` — like every issue the hive files, the title starts with the filing agent in square brackets (`issueshape.TitleFor`, applied once more in `CreateIssue` so no filer can forget it, and ignored by title dedupe so items filed before the tag still match). Each issue body links back to the reviewed PR and records the perspective, severity, evidence location, and finding summary. The relay deduplicates against open issues through the normal issue-create path and also persists a finding key in `/data/review-backlog-issues.json`, so re-reviewing a new commit does not file the same backlog issue or summary comment again. The PR receives one summary comment listing the backlog issues filed from that review. Filing is enabled by default and bounded by `review.max_out_of_scope_backlog_issues` (default `3`) per PR; set `review.out_of_scope_backlog_disabled: true` to opt out. diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 93953076aa..ef716e2317 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -1216,6 +1216,36 @@ func (r CreateIssueRequest) OriginLabel() string { return config.OriginLabelForAgent(r.Origin) } +// TitleFiler returns the agent or subsystem name the issue title is tagged +// with (`[] …`): the Origin's agent lane (`reviewer-2` → `reviewer`), +// else a non-lane origin/* label the caller chose (the convergence publisher +// filing through the forge seam), else the Origin source as written +// (`governor`, `selfreport`, `dashboard`), else the source of the resolved +// provenance label. +func (r CreateIssueRequest) TitleFiler() string { + lane := config.OriginLaneFromAgent(r.Origin) + if lane != "" && config.IsKnownOriginLane(lane) { + return lane + } + for _, l := range r.Labels { + if source := config.OriginSourceOf(l); source != "" && !config.IsKnownOriginLane(source) { + return source + } + } + if lane != "" { + return lane + } + if source := config.OriginSourceOf(r.OriginLabel()); source != "" { + return source + } + for _, l := range r.Labels { + if source := config.OriginSourceOf(l); source != "" { + return source + } + } + return "" +} + // ErrOriginRequired is returned when a create carries no provenance label and // names no Origin source. var ErrOriginRequired = errors.New("CreateIssue: provenance is required (set Origin or pass an origin/* label)") @@ -1255,6 +1285,11 @@ func (c *Client) createIssue(ctx context.Context, req CreateIssueRequest) (Creat if title == "" { return CreateIssueResult{}, fmt.Errorf("CreateIssue: title is required") } + // Every issue the hive files names its filer in the title (`[reviewer] + // Review backlog (security): …`). Hive-internal filers and the watcher + // both land here, so a caller cannot forget it; TitleFor is idempotent + // for a title an agent already tagged. + title = issueshape.TitleFor(req.TitleFiler(), title) // Same posture and same placement as validateRepoRef above: a request that // is still carrying its template cannot become a useful issue, and unlike a // bad repo ref it would SUCCEED, leaving a human to triage an empty report @@ -1302,7 +1337,7 @@ func (c *Client) createIssue(ctx context.Context, req CreateIssueRequest) (Creat // twin (#13906). An exact-title match is the same finding and stays // unguarded. if err == nil && scan.subject != nil && req.FoldMatch != nil && - strings.TrimSpace(scan.subject.GetTitle()) != title && + !issueshape.SameTitle(scan.subject.GetTitle(), title) && !req.FoldMatch(scan.subject.GetTitle(), scan.subject.GetBody()) { scan.subject = nil } @@ -1778,7 +1813,10 @@ func (c *Client) scanOpenIssues(ctx context.Context, owner, repo, title, body st continue } candidate := strings.TrimSpace(is.GetTitle()) - if candidate == title || reviewBacklogTitlesEqual(candidate, title) { + // Exact match ignores the leading `[]` tag so an issue + // filed before titles carried the filer still dedupes its + // prefixed re-filing instead of gaining a twin. + if candidate == title || issueshape.SameTitle(candidate, title) || reviewBacklogTitlesEqual(candidate, title) { return openIssueMatches{subject: is}, nil } // Pages arrive newest-first, so overwriting leaves the oldest diff --git a/src/pkg/github/issue_title_agent_prefix_test.go b/src/pkg/github/issue_title_agent_prefix_test.go new file mode 100644 index 0000000000..3a04ecf927 --- /dev/null +++ b/src/pkg/github/issue_title_agent_prefix_test.go @@ -0,0 +1,123 @@ +package github + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/issueshape" + "github.com/hivecommons/hive/pkg/outputschema" + "github.com/hivecommons/hive/pkg/review" +) + +// Every title-producing helper in this package tags the filing agent, and +// the one create path tags whatever a caller hands it, so a hive-filed issue +// always says who filed it (the owner's rule: "agents that need to open +// issues need to use [agent name]"). +func TestHiveFiledIssueTitlesCarryTheFilingAgent(t *testing.T) { + finding := outputschema.Finding{Title: "token logged", File: "pkg/a.go", Line: 3} + helpers := map[string]string{ + "review backlog": reviewBacklogIssueTitle(review.PerspectiveSecurity, finding), + "review backlog empty": reviewBacklogIssueTitle(review.PerspectiveStyle, outputschema.Finding{}), + } + for name, title := range helpers { + if issueshape.AgentPrefix(title) != config.OriginSourceReviewer { + t.Errorf("%s title %q does not start with [reviewer]", name, title) + } + } + if got := reviewBacklogIssueTitle(review.PerspectiveSecurity, finding); got != "[reviewer] Review backlog (security): token logged" { + t.Fatalf("review backlog title = %q", got) + } + if got := reviewBacklogItemCategory("[reviewer] Review backlog (security): token logged", ""); got != "security" { + t.Fatalf("tagged title category = %q, want security", got) + } + if got := reviewBacklogItemCategory("Review backlog (style): x", ""); got != "style" { + t.Fatalf("legacy untagged title category = %q, want style", got) + } + + filers := []struct { + name string + req CreateIssueRequest + want string + }{ + {"agent replica", CreateIssueRequest{Origin: "reviewer-2"}, "reviewer"}, + {"custom agent", CreateIssueRequest{Origin: "adjudicator"}, "adjudicator"}, + {"governor hotspot routed to a lane", CreateIssueRequest{Origin: config.OriginSourceGovernor, Labels: []string{"agent/ci-maintainer", "kind/cleanup"}}, "governor"}, + {"self report", CreateIssueRequest{Origin: config.OriginSourceSelfReport}, "selfreport"}, + {"convergence through the forge seam", CreateIssueRequest{Origin: config.OriginSourceForge, Labels: []string{"origin/convergence"}}, "convergence"}, + {"label only", CreateIssueRequest{Labels: []string{"agent/scanner"}}, "scanner"}, + } + for _, tc := range filers { + if got := tc.req.TitleFiler(); got != tc.want { + t.Errorf("%s: TitleFiler = %q, want %q", tc.name, got, tc.want) + } + } +} + +// createIssue tags the title for every caller and dedupes against an open +// issue that was filed before titles carried the tag, so the roll that +// introduces the convention does not refile every open finding. +func TestCreateIssueTagsTitleAndDedupesAgainstUntaggedTwin(t *testing.T) { + var createdTitles []string + var listed string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == "GET" && strings.HasSuffix(r.URL.Path, "/issues"): + if listed == "" { + _, _ = io.WriteString(w, `[]`) + return + } + b, _ := json.Marshal([]map[string]any{{"number": 7, "title": listed, "html_url": "https://github.example/o/r/issues/7"}}) + _, _ = w.Write(b) + case r.Method == "GET" && strings.Contains(r.URL.Path, "/labels/"): + _, _ = io.WriteString(w, `{"name":"x"}`) + case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/labels"): + _, _ = io.WriteString(w, `{"name":"x"}`) + case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/issues"): + var body struct { + Title string `json:"title"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + createdTitles = append(createdTitles, body.Title) + _, _ = io.WriteString(w, `{"number":99,"html_url":"https://github.example/o/r/issues/99"}`) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := issueTestClient(t, srv.URL) + ctx := context.Background() + + res, err := c.CreateIssue(ctx, CreateIssueRequest{Repo: "o/r", Title: "Review backlog (security): token logged", Body: "body", Origin: "reviewer-2"}) + if err != nil || res.AlreadyExisted { + t.Fatalf("create: res=%+v err=%v", res, err) + } + res, err = c.CreateIssue(ctx, CreateIssueRequest{Repo: "o/r", Title: "[selfreport] already tagged", Body: "body", Origin: config.OriginSourceSelfReport}) + if err != nil || res.AlreadyExisted { + t.Fatalf("create tagged: res=%+v err=%v", res, err) + } + want := []string{"[reviewer] Review backlog (security): token logged", "[selfreport] already tagged"} + if strings.Join(createdTitles, "|") != strings.Join(want, "|") { + t.Fatalf("created titles = %q, want %q", createdTitles, want) + } + + listed = "Review backlog (security): token logged" + res, err = c.CreateIssue(ctx, CreateIssueRequest{Repo: "o/r", Title: "Review backlog (security): token logged", Body: "body", Origin: "reviewer"}) + if err != nil || !res.AlreadyExisted || res.Number != 7 { + t.Fatalf("untagged open twin must dedupe the tagged re-filing: res=%+v err=%v", res, err) + } + listed = "[reviewer] Review backlog (security): token logged" + res, err = c.CreateIssue(ctx, CreateIssueRequest{Repo: "o/r", Title: "Review backlog (security): token logged", Body: "body", Origin: "reviewer"}) + if err != nil || !res.AlreadyExisted || res.Number != 7 { + t.Fatalf("tagged open twin must dedupe: res=%+v err=%v", res, err) + } + if len(createdTitles) != 2 { + t.Fatalf("dedupe must not create: %q", createdTitles) + } +} diff --git a/src/pkg/github/review_backlog.go b/src/pkg/github/review_backlog.go index e6c7ef8f90..b1e6ecf904 100644 --- a/src/pkg/github/review_backlog.go +++ b/src/pkg/github/review_backlog.go @@ -18,6 +18,7 @@ import ( gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/issueshape" "github.com/hivecommons/hive/pkg/outputschema" "github.com/hivecommons/hive/pkg/review" ) @@ -587,12 +588,15 @@ func reviewBacklogKey(repo string, pr int, perspective review.Perspective, f out return hex.EncodeToString(h[:]) } +// reviewBacklogIssueTitle is the title of a review backlog item: +// `[reviewer] Review backlog (): `. The leading tag +// names the filing agent, as every hive-filed issue title does. func reviewBacklogIssueTitle(p review.Perspective, f outputschema.Finding) string { title := strings.TrimSpace(f.Title) if title == "" { title = "out-of-scope review finding" } - return fmt.Sprintf("Review backlog (%s): %s", p, title) + return issueshape.TitleFor(config.OriginSourceReviewer, fmt.Sprintf("Review backlog (%s): %s", p, title)) } func reviewBacklogSummaryComment(issues []reviewBacklogFiled, cap int) string { diff --git a/src/pkg/github/review_backlog_dedupe_test.go b/src/pkg/github/review_backlog_dedupe_test.go index c36958123c..3cfebe4b8d 100644 --- a/src/pkg/github/review_backlog_dedupe_test.go +++ b/src/pkg/github/review_backlog_dedupe_test.go @@ -129,7 +129,7 @@ func TestReviewBacklogOneItemPerFindingAcrossPerspectivesAndHeads(t *testing.T) file5(review.PerspectiveStyle, "abc", oos("New test re-covers prWarnOnce semantics rather than the dispatcher wiring", file, 27, "the test exercises prWarnOnce")) file5(review.PerspectiveSecurity, "abc", oos("Previous style finding still present (non-blocking)", file, 27, "unchanged")) file5(review.PerspectiveIntentAlignment, "abc", oos("Previous style note still present (non-blocking): TestReviewPromotedWarnOncePerPR exercises prWarnOnce, not planReviewDispatchWith wiring", file, 30, "unchanged")) - if len(sink.created) != 1 || sink.created[0].Title != "Review backlog (style): New test re-covers prWarnOnce semantics rather than the dispatcher wiring" { + if len(sink.created) != 1 || sink.created[0].Title != "[reviewer] Review backlog (style): New test re-covers prWarnOnce semantics rather than the dispatcher wiring" { t.Fatalf("created = %+v, want the one style item", sink.created) } if len(sink.appends) != 0 { @@ -158,7 +158,7 @@ func TestReviewBacklogOneItemPerFindingAcrossPerspectivesAndHeads(t *testing.T) } // A genuinely new finding elsewhere still files. file5(review.PerspectiveCorrectness, "bbb", oos("Dispatcher wiring untested", "pkg/other.go", 9, "nothing covers planReviewDispatchWith")) - if len(sink.created) != 2 || sink.created[1].Title != "Review backlog (correctness): Dispatcher wiring untested" { + if len(sink.created) != 2 || sink.created[1].Title != "[reviewer] Review backlog (correctness): Dispatcher wiring untested" { t.Fatalf("created = %+v, want the new correctness finding filed second", sink.created) } diff --git a/src/pkg/github/review_backlog_heads_test.go b/src/pkg/github/review_backlog_heads_test.go index 082c799b88..6cd6fa2dfa 100644 --- a/src/pkg/github/review_backlog_heads_test.go +++ b/src/pkg/github/review_backlog_heads_test.go @@ -119,7 +119,7 @@ func TestReviewBacklogHeadsModeFilesDecidedReportsOnly(t *testing.T) { t.Fatalf("review 77 edits = %d, want 1", len(edits)) } if !strings.HasPrefix(edits[0], "Decided review body.") || !strings.Contains(edits[0], reviewFollowUpsMarker) || - !strings.Contains(edits[0], "[ENG-1](https://tracker.test/ENG-1) — Review backlog (correctness): stale docstring (filed)") { + !strings.Contains(edits[0], "[ENG-1](https://tracker.test/ENG-1) — [reviewer] Review backlog (correctness): stale docstring (filed)") { t.Fatalf("follow-ups edit = %q", edits[0]) } diff --git a/src/pkg/github/review_backlog_routing.go b/src/pkg/github/review_backlog_routing.go index 5cbffca83a..2dc933b00b 100644 --- a/src/pkg/github/review_backlog_routing.go +++ b/src/pkg/github/review_backlog_routing.go @@ -158,6 +158,8 @@ func (s githubIssueBacklogSink) CreateItem(ctx context.Context, item ReviewBackl } var ( + // The optional leading `[]` tag keeps items filed before titles + // carried the filer in the same category as their prefixed successors. reviewBacklogTitleCategoryRE = regexp.MustCompile(`^\s*(?:\[[^\]\s]+\]\s*)?Review backlog \(([^)]+)\):`) reviewBacklogPerspectiveRE = regexp.MustCompile(`(?m)^\s*Perspective:\s*(\S+)`) reviewBacklogFileLineRE = regexp.MustCompile(`([A-Za-z0-9_./-]+):(\d+)\b`) diff --git a/src/pkg/github/review_backlog_routing_test.go b/src/pkg/github/review_backlog_routing_test.go index fcf112fce4..3f58c1a8d9 100644 --- a/src/pkg/github/review_backlog_routing_test.go +++ b/src/pkg/github/review_backlog_routing_test.go @@ -265,7 +265,7 @@ func TestReviewBacklogRoutesBelowLineFindingsWithDedupAndDailyCap(t *testing.T) t.Fatalf("created = %d, want 2 (daily cap) below-line items", len(sink.created)) } first := sink.created[0] - if first.Repo != "o/r" || first.Title != "Review backlog (correctness): unused variable" || strings.Join(first.Labels, ",") != "nit,blocked" { + if first.Repo != "o/r" || first.Title != "[reviewer] Review backlog (correctness): unused variable" || strings.Join(first.Labels, ",") != "nit,blocked" { t.Fatalf("first item = %+v", first) } for _, want := range []string{ @@ -735,7 +735,7 @@ func TestReviewBacklogFoldedTwinKeepsFullFinding(t *testing.T) { "**Additional finding (folded in — same finding area and category).**", "it is a correctness finding at the same symbol or lines of `pkg/a.go`", "PR #5 (https://github.com/o/r/pull/5)", - "### Review backlog (correctness): unused variable", + "### [reviewer] Review backlog (correctness): unused variable", "- [ ] unused variable (`pkg/a.go:7`)", "Perspective: correctness", "Severity: medium (P2)", @@ -752,7 +752,7 @@ func TestReviewBacklogFoldedTwinKeepsFullFinding(t *testing.T) { if strings.Contains(got.comment, "Linked again") { t.Errorf("folded comment still uses the bare link line:\n%s", got.comment) } - if len(comments[5]) != 1 || !strings.Contains(comments[5][0], "- ENG-1 (folded into an existing issue) — Review backlog (correctness): unused variable (https://tracker.test/ENG-1)") { + if len(comments[5]) != 1 || !strings.Contains(comments[5][0], "- ENG-1 (folded into an existing issue) — [reviewer] Review backlog (correctness): unused variable (https://tracker.test/ENG-1)") { t.Fatalf("PR 5 summary comments = %q, want one line saying ENG-1 was folded", comments[5]) } diff --git a/src/pkg/github/review_backlog_split_test.go b/src/pkg/github/review_backlog_split_test.go index 47a52b2d5f..d213fdf226 100644 --- a/src/pkg/github/review_backlog_split_test.go +++ b/src/pkg/github/review_backlog_split_test.go @@ -58,7 +58,7 @@ func TestParseReviewBacklogFoldedComment(t *testing.T) { if item.Key != key || item.Fingerprint != fp || item.PRNumber != 5 { t.Errorf("key/fp/pr = %q/%q/%d, want %q/%q/5", item.Key, item.Fingerprint, item.PRNumber, key, fp) } - if item.Title != "Review backlog (correctness): unused variable (x)" || item.Perspective != review.PerspectiveCorrectness { + if item.Title != "[reviewer] Review backlog (correctness): unused variable (x)" || item.Perspective != review.PerspectiveCorrectness { t.Errorf("title/perspective = %q/%q", item.Title, item.Perspective) } if item.Finding.Title != f.Title || item.Finding.File != f.File || item.Finding.Line != f.Line { @@ -162,7 +162,7 @@ func TestSplitReviewBacklogHostIsIdempotentByLinkKey(t *testing.T) { t.Fatalf("created=%d appends=%d, want 1 and %d", len(sink.created), len(sink.appends), tc.wantAppends) } item := sink.created[0] - if item.Title != "Review backlog (correctness): unused variable" || item.Finding.File != "pkg/a.go" || item.Perspective != review.PerspectiveCorrectness { + if item.Title != "[reviewer] Review backlog (correctness): unused variable" || item.Finding.File != "pkg/a.go" || item.Perspective != review.PerspectiveCorrectness { t.Errorf("created item = %+v", item) } for _, want := range []string{reviewBacklogSplitMarker + openKey + " -->", "Split from #42 (host closed)", "PR: #5 (https://github.com/o/r/pull/5)", "x is never read", "Fingerprint: `" + openFP + "`"} { diff --git a/src/pkg/hotspot/hotspot_test.go b/src/pkg/hotspot/hotspot_test.go index 18192e5b7c..4084c6594a 100644 --- a/src/pkg/hotspot/hotspot_test.go +++ b/src/pkg/hotspot/hotspot_test.go @@ -283,7 +283,7 @@ func TestFilerDedupeAndRefileWindow(t *testing.T) { if !strings.HasPrefix(cr.body, Marker(h.File)) { t.Errorf("body must start with dedupe marker, got %q", cr.body[:60]) } - if cr.title != "refactor: pkg/dashboard/api.go is a merge hotspot — split into 4 units" { + if cr.title != "[governor] refactor: pkg/dashboard/api.go is a merge hotspot — split into 4 units" { t.Errorf("title=%q", cr.title) } if strings.Join(cr.labels, ",") != "agent/ci-maintainer,kind/cleanup" { @@ -360,7 +360,7 @@ func TestBodyGolden(t *testing.T) { if !strings.Contains(gb, "**generated**") || strings.Contains(gb, "## Split proposal") { t.Errorf("generated body wrong:\n%s", gb) } - if got := Title(gen, 4); got != "refactor: pkg/dashboard/static/index.html is a merge hotspot — split its source pkg/dashboard/static-src into 4 units" { + if got := Title(gen, 4); got != "[governor] refactor: pkg/dashboard/static/index.html is a merge hotspot — split its source pkg/dashboard/static-src into 4 units" { t.Errorf("generated title=%q", got) } } diff --git a/src/pkg/hotspot/issue.go b/src/pkg/hotspot/issue.go index 9b0525c4b1..779972c1f8 100644 --- a/src/pkg/hotspot/issue.go +++ b/src/pkg/hotspot/issue.go @@ -7,21 +7,29 @@ import ( "sort" "strings" "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/issueshape" ) // Labels applied to every modularize issue. Both exist on hivecommons/hive; // the writer's label-ensure step creates them elsewhere. var Labels = []string{"agent/ci-maintainer", "kind/cleanup"} -// Title is the issue title for a hotspot. +// Title is the issue title for a hotspot. The governor's hotspot advisor +// files it, so it carries the `[governor]` tag every hive-filed issue title +// starts with; the agent/ci-maintainer label says who works it. func Title(h Hotspot, units int) string { + var title string switch h.Recommendation { case RecommendSplitSource: - return fmt.Sprintf("refactor: %s is a merge hotspot — split its source %s into %d units", h.File, strings.TrimSuffix(h.GeneratedFrom, "/"), units) + title = fmt.Sprintf("refactor: %s is a merge hotspot — split its source %s into %d units", h.File, strings.TrimSuffix(h.GeneratedFrom, "/"), units) case RecommendSplitDoc: - return fmt.Sprintf("refactor: %s is a merge hotspot — split into %d sections", h.File, units) + title = fmt.Sprintf("refactor: %s is a merge hotspot — split into %d sections", h.File, units) + default: + title = fmt.Sprintf("refactor: %s is a merge hotspot — split into %d units", h.File, units) } - return fmt.Sprintf("refactor: %s is a merge hotspot — split into %d units", h.File, units) + return issueshape.TitleFor(config.OriginSourceGovernor, title) } // Body renders the issue body: plain-language summary (≤ 300 words), diff --git a/src/pkg/issueshape/issueshape.go b/src/pkg/issueshape/issueshape.go index 8a9cdb70a3..58100f1208 100644 --- a/src/pkg/issueshape/issueshape.go +++ b/src/pkg/issueshape/issueshape.go @@ -13,4 +13,9 @@ var ( UnsubstitutedTemplatePlaceholder = cruxissueshape.UnsubstitutedTemplatePlaceholder BodyHasMisEscapedNewlines = cruxissueshape.BodyHasMisEscapedNewlines UnchosenOptionList = cruxissueshape.UnchosenOptionList + TitleFor = cruxissueshape.TitleFor + HasAgentPrefix = cruxissueshape.HasAgentPrefix + AgentPrefix = cruxissueshape.AgentPrefix + StripAgentPrefix = cruxissueshape.StripAgentPrefix + SameTitle = cruxissueshape.SameTitle ) diff --git a/src/pkg/policies/defaults/dedupe-issues.md b/src/pkg/policies/defaults/dedupe-issues.md index c6755715e8..0e360ac937 100644 --- a/src/pkg/policies/defaults/dedupe-issues.md +++ b/src/pkg/policies/defaults/dedupe-issues.md @@ -241,7 +241,7 @@ to decide what to close and what to merge, so it is parked: ```bash gh issue create --repo --needs-decision \ - --title "triage: duplicate and overlapping open issues/PRs to consolidate" \ + --title "[dedupe] triage: duplicate and overlapping open issues/PRs to consolidate" \ --body-file /tmp/.md ``` @@ -254,7 +254,7 @@ so it is **not** parked: no `--needs-decision`, no "What to reply" notice, no ```bash gh issue create --repo --label triage/advisory \ - --title "triage: duplicate and overlapping open issues/PRs to consolidate" \ + --title "[dedupe] triage: duplicate and overlapping open issues/PRs to consolidate" \ --body-file /tmp/.md ``` diff --git a/src/pkg/upstreamwatch/render.go b/src/pkg/upstreamwatch/render.go index b9b4d3d4a8..1ae4fd725d 100644 --- a/src/pkg/upstreamwatch/render.go +++ b/src/pkg/upstreamwatch/render.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/issueshape" ) const ( @@ -94,7 +95,7 @@ func RenderIssue(upstream string, item Item, j Judgement, label string) Issue { // The watch files on the governor's behalf; origin/governor is its // provenance label (the forge auto-creates unknown labels on create). labels = config.WithOriginLabel(labels, config.OriginSourceGovernor) - return Issue{Title: titlePrefix + title, Body: b.String(), Labels: labels} + return Issue{Title: issueshape.TitleFor(config.OriginSourceGovernor, titlePrefix+title), Body: b.String(), Labels: labels} } // summarize trims the upstream body to a bounded summary. diff --git a/src/pkg/upstreamwatch/render_test.go b/src/pkg/upstreamwatch/render_test.go index b51f2602fa..9fe6ac3b1a 100644 --- a/src/pkg/upstreamwatch/render_test.go +++ b/src/pkg/upstreamwatch/render_test.go @@ -47,7 +47,7 @@ func TestRenderIssue(t *testing.T) { item: pr, j: Judgement{Class: ClassBugfix, Difficulty: DifficultyEasy, Applicable: true, Present: []string{"api.go"}}, label: "upstream/port", - title: "upstream: fix(api): handle nil config", + title: "[governor] upstream: fix(api): handle nil config", contains: []string{ "up/widgets#42 (https://github.com/up/widgets/pull/42)", "**Class:** bugfix", @@ -65,7 +65,7 @@ func TestRenderIssue(t *testing.T) { item: Item{Kind: KindRelease, Ref: RefRelease("v2.0.0"), Title: "v2.0.0", HTMLURL: "https://github.com/up/widgets/releases/tag/v2.0.0"}, j: Judgement{Class: ClassChore, Difficulty: DifficultyModerate, Applicable: true}, label: "custom", - title: "upstream: v2.0.0", + title: "[governor] upstream: v2.0.0", contains: []string{"upstream release", "**Port difficulty:** moderate\n", ""}, absent: []string{"### Affected files", "### Summary"}, labels: []string{"custom", "origin/governor"}, @@ -74,7 +74,7 @@ func TestRenderIssue(t *testing.T) { name: "empty title falls back to ref and long file lists are bounded", item: Item{Kind: KindPR, Ref: RefPR(7), Files: many}, j: Judgement{Class: ClassChore, Difficulty: DifficultyHard, Applicable: true, Present: many}, - title: "upstream: upstream#7", + title: "[governor] upstream: upstream#7", contains: []string{"- ... and 5 more"}, labels: []string{"origin/governor"}, }, diff --git a/src/policies/dedupe-issues.md b/src/policies/dedupe-issues.md index c6755715e8..0e360ac937 100644 --- a/src/policies/dedupe-issues.md +++ b/src/policies/dedupe-issues.md @@ -241,7 +241,7 @@ to decide what to close and what to merge, so it is parked: ```bash gh issue create --repo --needs-decision \ - --title "triage: duplicate and overlapping open issues/PRs to consolidate" \ + --title "[dedupe] triage: duplicate and overlapping open issues/PRs to consolidate" \ --body-file /tmp/.md ``` @@ -254,7 +254,7 @@ so it is **not** parked: no `--needs-decision`, no "What to reply" notice, no ```bash gh issue create --repo --label triage/advisory \ - --title "triage: duplicate and overlapping open issues/PRs to consolidate" \ + --title "[dedupe] triage: duplicate and overlapping open issues/PRs to consolidate" \ --body-file /tmp/.md ```