Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-attribution-trailer-footer-lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A PR whose attribution trailer (`-- hive: agent=… backend=… model=…`) is followed by footer lines is counted again in Governor "PRs by model", Operations "Most effective models" and the rework and cost figures. The trailer was read only when it was the last non-blank line before the identity footer, so a `Refs #N`, `Closes #N` or `Fixes #N` line, an HTML marker, or a `πŸ€– Generated with …` line below it dropped the PR: 112 of 707 trailer-carrying projectbluefin PRs (16%), among them [projectbluefin/utah-packages#949](https://github.com/projectbluefin/utah-packages/pull/949), [#1014](https://github.com/projectbluefin/utah-packages/pull/1014) and [projectbluefin/website#1040](https://github.com/projectbluefin/website/pull/1040). Those lines, and an identity footer with a `Fixes #N` appended below it, are now skipped; any other line after the trailer still means the PR has none, so a trailer quoted in prose is not taken for the PR's own. The issue-link repair that rewrites `Refs #N` to `Fixes #N` now puts its `<!-- hive:issue-link-fixed -->` marker above the trailer instead of below it, so the hive no longer breaks its own attribution (22 of the 112).
84 changes: 74 additions & 10 deletions src/pkg/github/attribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,49 @@ func HasAttributionTrailer(body string) bool {
return strings.Contains(body, AttributionTrailerPrefix) || strings.Contains(body, LegacyAttributionTrailerPrefix)
}

// identityFooterLinePattern is the `🐝 **Hive Agent**: …` line of
// bin/gh-wrapper.sh's _identity_footer, shared by the terminal-block regex
// and the per-line check ParseAttributionTrailer applies to a footer that is
// no longer last.
const identityFooterLinePattern = "🐝 \\*\\*Hive Agent\\*\\*:(?: `[^`\\n]+`)?(?: \\| \\*\\*Instance:\\*\\* `[^`\\n]+`)? \\| \\*\\*SHA:\\*\\* `[^`\\n]+`"

// attributionIdentityFooter matches only the terminal block emitted by
// bin/gh-wrapper.sh's _identity_footer, not arbitrary trailing Markdown. The
// footer may end with the origin marker (`<!-- hive:origin … -->`, #12539).
var attributionIdentityFooter = regexp.MustCompile("\\n[ \\t]*---[ \\t]*\\n🐝 \\*\\*Hive Agent\\*\\*:(?: `[^`\\n]+`)?(?: \\| \\*\\*Instance:\\*\\* `[^`\\n]+`)? \\| \\*\\*SHA:\\*\\* `[^`\\n]+`[ \\t]*(?:\\n[ \\t]*<!-- " + OriginMarkerName + "[^>]*-->)?[ \\t\\n]*$")
var attributionIdentityFooter = regexp.MustCompile("\\n[ \\t]*---[ \\t]*\\n" + identityFooterLinePattern + "[ \\t]*(?:\\n[ \\t]*<!-- " + OriginMarkerName + "[^>]*-->)?[ \\t\\n]*$")

// attributionIdentityFooterLine is identityFooterLinePattern as a whole line.
var attributionIdentityFooterLine = regexp.MustCompile("^" + identityFooterLinePattern + "[ \\t]*$")

// attributionFooterRefLine matches a closing or reference keyword line that
// tools and the hive itself write below the trailer: `Refs #489`, `Closes
// #1000`, `Fixes owner/repo#12, #13`, `Part of #7`, optionally followed by a
// note after a dash, colon or parenthesis (`Refs #503 - remaining: lockdev`).
// It is anchored to the whole line, so prose that merely starts with "Fixes"
// is not a footer line.
var attributionFooterRefLine = func() *regexp.Regexp {
keyword := `(?:` + strings.Join(closingKeywords, "|") + `|refs?|part\s+of)`
ref := `(?:(?:[\w.-]+/[\w.-]+)?#\d+|https://github\.com/[\w.-]+/[\w.-]+/(?:issues|pull)/\d+)`
gap := `(?:\s*:\s*|\s+)`
sep := `(?:\s*,\s*(?:and\s+)?|\s+and\s+|\s+)`
note := `(?:[.;]|\s*(?:-|\x{2013}|\x{2014}|:|\().*)?`
return regexp.MustCompile(`(?i)^` + keyword + gap + ref + `(?:` + sep + `(?:` + keyword + gap + `)?` + ref + `)*` + note + `$`)
}()

// isAttributionFooterLine reports whether line (already trimmed) is one of
// the lines that may follow the trailer without hiding it: a closing or
// reference keyword line, a single-line HTML comment (the hive's own
// `<!-- hive:issue-link-fixed -->` and similar markers), or a
// `πŸ€– Generated with …` line coding tools append.
func isAttributionFooterLine(line string) bool {
if strings.HasPrefix(line, "<!--") && strings.Index(line, "-->") == len(line)-len("-->") {
return true
}
if strings.HasPrefix(line, "πŸ€– Generated with ") {
return true
}
return attributionFooterRefLine.MatchString(line)
}

// OriginFooterNote reports whether body is an agent's free-text note whose
// only machine marker is the identity footer's `<!-- hive:origin … -->`
Expand Down Expand Up @@ -461,25 +500,50 @@ func OriginFooterNote(body string) (note string, ok bool) {
return note, true
}

// ParseAttributionTrailer extracts the launch metadata from the final visible
// `β€” hive:` line in a PR body, optionally followed by Hive's identity footer.
// Mentions of the template earlier in the body are deliberately ignored so
// docs and quoted examples do not count as agent PRs.
func ParseAttributionTrailer(body string) (InvocationMeta, bool) {
// attributionTrailerLine finds body's own trailer: the last `β€” hive:` line
// with nothing after it but blank lines, footer lines
// (isAttributionFooterLine) and one Hive identity footer. It returns the
// trimmed trailer and its index among body's "\n"-separated lines, or ("",
// -1) when any other line follows the last trailer or there is none. Line
// indices are those of the CRLF-normalized body, which has the same lines.
func attributionTrailerLine(body string) (string, int) {
body = strings.ReplaceAll(body, "\r\n", "\n")
body = attributionIdentityFooter.ReplaceAllString(body, "")
footerSeen := false
if loc := attributionIdentityFooter.FindStringIndex(body); loc != nil {
body = body[:loc[0]]
footerSeen = true
}
lines := strings.Split(body, "\n")
trailer := ""
for i := len(lines) - 1; i >= 0; i-- {
line := strings.TrimSpace(lines[i])
if line == "" {
if line == "" || isAttributionFooterLine(line) {
continue
}
// The identity footer is no longer last when a line was appended
// below it (`Fixes #N` added to a contributor PR). One footer, with
// its `---` rule directly above, is skipped like the terminal one.
if !footerSeen && i > 0 && strings.TrimSpace(lines[i-1]) == "---" && attributionIdentityFooterLine.MatchString(lines[i]) {
footerSeen = true
i--
continue
}
if _, ok := HasAttributionTrailerPrefix(line); ok {
trailer = line
return line, i
}
break
}
return "", -1
}

// ParseAttributionTrailer extracts the launch metadata from the final visible
// `β€” hive:` line in a PR body. Only footer lines may follow it: Hive's
// identity footer, closing/reference keyword lines, single-line HTML
// comments and a `πŸ€– Generated with …` line (attributionTrailerLine). Mentions
// of the template earlier in the body, or a trailer followed by any other
// line, are deliberately ignored so docs and quoted examples do not count as
// agent PRs.
func ParseAttributionTrailer(body string) (InvocationMeta, bool) {
trailer, _ := attributionTrailerLine(body)
if trailer == "" {
return InvocationMeta{}, false
}
Expand Down
110 changes: 110 additions & 0 deletions src/pkg/github/attribution_footer_lines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package github

import (
"strings"
"testing"
)

// Lines tools and the hive itself write below the attribution trailer
// (closing keywords, HTML markers, a coding tool's "Generated with" line)
// must not hide it from the PR-models count: 112 of 707 trailer-carrying
// projectbluefin PRs were dropped that way.

const footerLinesTrailer = "-- hive: agent=scanner backend=codex model=gpt-6-astra"

func assertFooterLinesTrailer(t *testing.T, body string) {
t.Helper()
meta, ok := ParseAttributionTrailer(body)
if !ok || meta.Agent != "scanner" || meta.Backend != "codex" || meta.Model != "gpt-6-astra" {
t.Errorf("ParseAttributionTrailer(%q) = %+v, %v; want scanner/codex/gpt-6-astra", body, meta, ok)
}
}

func TestParseAttributionTrailerSkipsFooterLines(t *testing.T) {
for _, tc := range []struct{ name, tail string }{
{"refs", "Refs #489"},
{"ref", "Ref #489"},
{"closes", "Closes #1000"},
{"close", "close #1000"},
{"closed", "Closed #1000"},
{"fixes", "Fixes #1031"},
{"fix", "FIX #1031"},
{"fixed", "Fixed #1031"},
{"resolves", "Resolves #12"},
{"resolve", "resolve #12"},
{"resolved", "Resolved #12"},
{"part-of", "Part of #7"},
{"colon", "Fixes: #7"},
{"cross-repo", "Refs owner/repo#7"},
{"url", "Fixes https://github.com/owner/repo/issues/7"},
{"list", "Fixes #7, #8 and owner/repo#9"},
{"repeated-keyword", "Fixes #7, fixes #8"},
{"period", "Closes #7."},
{"dash-note", "Refs #503 - remaining: lockdev"},
{"em-dash-note", "Refs #503 β€” remaining: lockdev"},
{"paren-note", "Refs #7 (closed by #9; this PR adds docs)"},
{"hive-marker", issueLinkFixedMarker},
{"html-comment", "<!-- some-tool: id=42 -->"},
{"generated-with", "πŸ€– Generated with [Claude Code](https://claude.com/claude-code)"},
{"several", "Refs #1\n\n<!-- marker -->\n\nπŸ€– Generated with [Claude Code](https://claude.com/claude-code)\nCloses #2"},
} {
t.Run(tc.name, func(t *testing.T) {
assertFooterLinesTrailer(t, "Done\n\n"+footerLinesTrailer+"\n\n"+tc.tail)
assertFooterLinesTrailer(t, "Done\n\n"+footerLinesTrailer+"\n"+tc.tail+"\n\n")
})
}
}

func TestParseAttributionTrailerRealFooterTails(t *testing.T) {
legacy := strings.Replace(footerLinesTrailer, AttributionTrailerPrefix, LegacyAttributionTrailerPrefix, 1)
for _, tc := range []struct{ name, body string }{
// Refs lines, the trailer, then the hive's own issue-link marker.
{"refs-then-marker", "## Summary\n\nPackages lockdev.\n\nRefs #489\nRefs #503 β€” remaining: lockdev, uucp\n\n" + legacy + " effort=high\n\n" + issueLinkFixedMarker},
// Trailer, Generated with, Closes, then the identity footer.
{"closes-then-identity-footer", "Summary\n\n" + footerLinesTrailer + "\n\nπŸ€– Generated with [Claude Code](https://claude.com/claude-code)\n\nCloses #1000\n\n---\n🐝 **Hive Agent**: `contributor` | **SHA:** `d6177bc3`"},
// Fixes above the trailer, Generated with and the identity footer below.
{"generated-then-identity-footer", "Fixes #1031\n\n" + footerLinesTrailer + "\n\nπŸ€– Generated with [Claude Code](https://claude.com/claude-code)\n\n---\n🐝 **Hive Agent**: `contributor` | **SHA:** `d6177bc3`"},
// A line appended below the identity footer (Fixes #N added to a
// contributor PR), with and without the origin marker.
{"fixes-after-identity-footer", contributorAttributionBodyFor(footerLinesTrailer) + "\n\nFixes #7"},
{"fixes-after-identity-footer-origin", contributorAttributionBodyFor(footerLinesTrailer) + "\n<!-- hive:origin hive=hive-bold-fox at=2026-10-16T20:00:00Z -->\n\nFixes #7"},
} {
t.Run(tc.name, func(t *testing.T) {
assertFooterLinesTrailer(t, tc.body)
assertFooterLinesTrailer(t, strings.ReplaceAll(tc.body, "\n", "\r\n"))
})
}
}

func contributorAttributionBodyFor(trailer string) string {
return "Done\n\n" + trailer + "\n\n---\n🐝 **Hive Agent**: `contributor` | **SHA:** `671737a3b`"
}

func TestParseAttributionTrailerFooterLinesStayStrict(t *testing.T) {
for _, tc := range []struct{ name, body string }{
{"prose-after", "Done\n\n" + footerLinesTrailer + "\n\nFixes #7\n\nThanks for reviewing."},
{"prose-starting-with-keyword", "Done\n\n" + footerLinesTrailer + "\n\nFixes the flaky test in #7 as well"},
{"keyword-without-ref", "Done\n\n" + footerLinesTrailer + "\n\nCloses the gap"},
{"code-after", "Done\n\n" + footerLinesTrailer + "\n\n```\nFixes #7\n```"},
{"quoted-in-prose", "The trailer looks like `" + footerLinesTrailer + "` in a body.\n\nSee the docs."},
{"quoted-line-then-prose", "Example:\n" + footerLinesTrailer + "\n\nThat line above is only an example.\n\nRefs #7"},
{"blockquoted", "Done\n\n> " + footerLinesTrailer + "\n\nRefs #7"},
{"multi-line-comment", "Done\n\n" + footerLinesTrailer + "\n\n<!--\nhidden\n-->"},
{"text-after-comment", "Done\n\n" + footerLinesTrailer + "\n\n<!-- marker --> visible text"},
{"identity-line-without-rule", "Done\n\n" + footerLinesTrailer + "\n\n🐝 **Hive Agent**: `contributor` | **SHA:** `671737a3b`\n\nFixes #7"},
{"two-identity-footers", contributorAttributionBodyFor(footerLinesTrailer) + "\n\nFixes #7\n\n---\n🐝 **Hive Agent**: `contributor` | **SHA:** `unknown`"},
{"footer-lines-only", "Refs #7\n\n" + issueLinkFixedMarker},
} {
t.Run(tc.name, func(t *testing.T) {
if meta, ok := ParseAttributionTrailer(tc.body); ok {
t.Errorf("ParseAttributionTrailer(%q) = %+v, want no trailer", tc.body, meta)
}
})
}
}

func TestParseAttributionTrailerFooterLinesLastTrailerWins(t *testing.T) {
body := "-- hive: agent=reviewer backend=claude model=claude-fable-5\n\nRefs #6\n\n" +
"β€” hive: agent=scanner backend=codex model=gpt-6-astra\n\nFixes #7\n" + issueLinkFixedMarker
assertFooterLinesTrailer(t, body)
}
5 changes: 3 additions & 2 deletions src/pkg/github/issue_park_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func parkWithdrawnBy(body string) string {
}

// trailerAgent reads the agent from the text's attribution trailer. Hidden
// markers after it (the owner-step marker a park ends with) are skipped:
// ParseAttributionTrailer reads only the final visible line.
// markers after it (the owner-step marker a park ends with) are skipped
// here as well as by ParseAttributionTrailer, which also skips the other
// footer lines a PR body ends with.
func trailerAgent(text string) string {
lines := strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n")
for len(lines) > 0 {
Expand Down
11 changes: 11 additions & 0 deletions src/pkg/github/pr_issue_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ func rewriteRefsIssueLinkToClosing(body, repo string, issue int) (string, bool)
return "", false
}
out := body[:loc[0]] + "Fixes" + body[loc[2]:]
// The marker goes above the attribution trailer when there is one, so
// the trailer stays the last content line the PR-models count reads.
if _, i := attributionTrailerLine(out); i >= 0 {
lines := strings.Split(out, "\n")
head := strings.TrimRight(strings.Join(lines[:i], "\n"), " \t\r\n")
tail := strings.Join(lines[i:], "\n")
if head == "" {
return issueLinkFixedMarker + "\n\n" + tail, true
}
return head + "\n\n" + issueLinkFixedMarker + "\n\n" + tail, true
}
out = strings.TrimRight(out, " \t\r\n") + "\n\n" + issueLinkFixedMarker
return out, true
}
Expand Down
39 changes: 39 additions & 0 deletions src/pkg/github/pr_issue_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,42 @@ func TestBodySaysIssueStaysOpenNumberBoundary(t *testing.T) {
t.Fatal("leaving #12 open should keep #12 open")
}
}

// The issue-link marker lands above the attribution trailer, never below it,
// so the rewrite does not drop the PR from the PR-models count. A body
// without a trailer gets the marker appended at the end, as before.
func TestRewriteRefsIssueLinkToClosingKeepsTrailerLast(t *testing.T) {
const trailer = "-- hive: agent=scanner backend=codex model=gpt-6-astra"
const footer = "\n\n---\n🐝 **Hive Agent**: `contributor` | **SHA:** `671737a3b`"
for _, tc := range []struct{ name, body, want string }{
{"refs-above-trailer", "Work.\n\nRefs #7\n\n" + trailer, "Work.\n\nFixes #7\n\n" + issueLinkFixedMarker + "\n\n" + trailer},
{"refs-above-trailer-and-footer", "Work.\n\nRefs #7\n\n" + trailer + footer, "Work.\n\nFixes #7\n\n" + issueLinkFixedMarker + "\n\n" + trailer + footer},
{"refs-below-trailer", "Work.\n\n" + trailer + "\nRefs #7", "Work.\n\n" + issueLinkFixedMarker + "\n\n" + trailer + "\nFixes #7"},
{"trailer-first", trailer + "\n\nRefs #7", issueLinkFixedMarker + "\n\n" + trailer + "\n\nFixes #7"},
} {
t.Run(tc.name, func(t *testing.T) {
got, ok := rewriteRefsIssueLinkToClosing(tc.body, "hivecommons/hive", 7)
if !ok || got != tc.want {
t.Fatalf("rewrite = %v %q, want %q", ok, got, tc.want)
}
meta, ok := ParseAttributionTrailer(got)
if !ok || meta.Agent != "scanner" || meta.Model != "gpt-6-astra" {
t.Fatalf("ParseAttributionTrailer(%q) = %+v, %v; want the trailer kept", got, meta, ok)
}
})
}
}

func TestRewriteRefsIssueLinkToClosingWithoutTrailerAppendsMarker(t *testing.T) {
for _, tc := range []struct{ name, body, want string }{
{"plain", "## Summary\n\nWork.\n\nRefs #7\n", "## Summary\n\nWork.\n\nFixes #7\n\n<!-- hive:issue-link-fixed -->"},
{"trailer-then-prose", "Work.\n\n-- hive: agent=scanner backend=codex\n\nRefs #7\n\nMore prose. \n", "Work.\n\n-- hive: agent=scanner backend=codex\n\nFixes #7\n\nMore prose.\n\n<!-- hive:issue-link-fixed -->"},
} {
t.Run(tc.name, func(t *testing.T) {
got, ok := rewriteRefsIssueLinkToClosing(tc.body, "hivecommons/hive", 7)
if !ok || got != tc.want {
t.Fatalf("rewrite = %v %q, want %q", ok, got, tc.want)
}
})
}
}
Loading