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-19096-review-thread-replies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Let the agent that opened a PR reply inside its review threads (`pulls/comments/{id}/replies`, `pulls/{n}/comments`) and retract a hive comment within 15 minutes on a PR that agent opened; every other PR, agent and comment stays refused, the review-feedback brief forbids probe comments, and a refused reply is posted in the sweep's needs-human note for a person (#19096).
11 changes: 11 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4303,6 +4303,17 @@ func (b *boot) bootProxyWith(deps bootProxyDeps) {
// unconditionally: with HIVE_PROXY_INJECT_GH_AUTH unset (the default)
// the proxy never consults the source and the registry stays empty.
b.githubProxy.SetAgentTokenSource(github.AgentProxyToken)
// Review-thread replies on the hive's own PRs (#19096): the proxy lets
// the agent that opened a PR (audit trail agent_pr_created) reply in
// its review threads, lets the hive retract its own comment for a
// short window, and keeps any refused reply for the review-feedback
// sweep's needs-human note. The closures read b.cfg and the live client per
// call because both are replaced on reload and key rotation.
b.githubProxy.SetReviewThreadGuard(newReviewThreadGuard(
func() string { return b.cfg.Project.Org },
func() string { return b.currentGitHubClient().AppBotLogin() },
b.logger,
))
// project.reference_repos (#18015): study-only repos outside the
// project, with a rare per-entry issues/prs opt-in served by a
// credential verified here (boot refuses otherwise).
Expand Down
132 changes: 115 additions & 17 deletions src/cmd/hive/review_feedback_wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/hivecommons/hive/crux"
"github.com/hivecommons/hive/crux/proxy"
"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/reviewfeedback"
Expand Down Expand Up @@ -36,10 +38,97 @@ const (
var (
reviewFeedbackLastRun time.Time
reviewFeedbackCursor int
// reviewFeedbackStore is shared by the sweep and the proxy's refused-reply
// hook (#19096), which run on different goroutines; open it through
// openReviewFeedbackStore.
reviewFeedbackStore *reviewfeedback.Store
reviewRuleStore *reviewrules.Store
reviewFeedbackStoreMu sync.Mutex
)

// openReviewFeedbackStore opens the sweep's state once and returns it.
func openReviewFeedbackStore(logger *slog.Logger) *reviewfeedback.Store {
reviewFeedbackStoreMu.Lock()
defer reviewFeedbackStoreMu.Unlock()
if reviewFeedbackStore == nil {
st, err := reviewfeedback.Open(reviewfeedback.Dir())
if err != nil {
logger.Warn("review feedback: state unreadable or not writable; handled ids kept in memory only", "dir", reviewfeedback.Dir(), "error", err)
}
reviewFeedbackStore = st
}
return reviewFeedbackStore
}

// reviewThreadOwnersTTL bounds how long the audit-trail PR ownership map the
// proxy's review-thread gate reads is reused before it is read again.
const reviewThreadOwnersTTL = time.Minute

// reviewThreadOwners caches auditPRAgents for the proxy gate, which is asked
// per request: reading the audit log on every reply would be wasteful, and a
// PR opened a minute ago is not yet being reviewed.
type reviewThreadOwners struct {
mu sync.Mutex
at time.Time
owners map[string]string
org func() string
load func(org string, since time.Time) map[string]string
now func() time.Time
}

// owner returns the agent that opened repo#number, from the same audit-trail
// source the sweep uses (agent_pr_created within auditPRAttributionWindow).
func (o *reviewThreadOwners) owner(repo string, number int) (string, bool) {
o.mu.Lock()
defer o.mu.Unlock()
now := o.now()
if o.owners == nil || now.Sub(o.at) >= reviewThreadOwnersTTL {
o.owners = o.load(o.org(), now.Add(-auditPRAttributionWindow))
o.at = now
}
key := reviewfeedback.Key(repo, number)
if agent, ok := o.owners[key]; ok && agent != "" {
return agent, true
}
for k, agent := range o.owners {
if agent != "" && strings.EqualFold(k, key) {
return agent, true
}
}
return "", false
}

// newReviewThreadGuard builds the proxy's review-thread gate seams (#19096):
// PR ownership from the audit trail, the hive's App bot login, and the hook
// that keeps a refused reply for the sweep's needs-human note.
func newReviewThreadGuard(org func() string, botLogin func() string, logger *slog.Logger) proxy.ReviewThreadGuard {
owners := &reviewThreadOwners{
org: org,
load: func(org string, since time.Time) map[string]string {
return auditPRAgents(org, since, "")
},
now: time.Now,
}
return proxy.ReviewThreadGuard{
OwnPR: owners.owner,
HiveLogins: func() []string {
if l := strings.TrimSpace(botLogin()); l != "" {
return []string{l}
}
return nil
},
OnRefused: func(r proxy.ReviewWriteRefusal) {
key := reviewfeedback.Key(r.Repo, r.Number)
err := openReviewFeedbackStore(logger).RecordBlockedReply(key, reviewfeedback.ReplyDraft{InReplyTo: r.InReplyTo, Body: r.Body, Agent: r.Agent, At: r.At})
if err != nil {
logger.Warn("review feedback: refused reply not kept for the needs-human note", "repo", r.Repo, "pr", r.Number, "agent", r.Agent, "error", err)
return
}
logger.Info("review feedback: reply refused by the proxy, kept for a person", "repo", r.Repo, "pr", r.Number, "agent", r.Agent, "in_reply_to", r.InReplyTo)
},
}
}

// reviewFeedbackSource is the GitHub surface the sweep needs.
type reviewFeedbackSource interface {
FetchPRFeedback(ctx context.Context, repo string, number int) (github.PRFeedback, error)
Expand Down Expand Up @@ -120,13 +209,21 @@ func (s reviewFeedbackSweep) run(ctx context.Context, parse func(key string) (re
s.Logger.Warn("review feedback: fetch failed, PR skipped this pass", "repo", repo, "pr", number, "error", err)
continue
}
if s.Rules != nil && fb.Open() {
if !fb.Open() {
continue
}
if s.Rules != nil {
s.applyRuleReplies(repo, number, fb.HiveReplies)
s.postRuleNotices(ctx, repo, number)
s.checkRuleReply(ctx, key, repo, number, fb.AgentReplies, &res)
s.captureRules(ctx, repo, number, redPRFixOwner(s.Owners[key]), fb.RuleCandidates, &res)
}
if !fb.Open() || len(fb.Items) == 0 {
// Replies the agent drafted but the proxy refused (#19096): hand them
// to a person instead of letting them vanish.
if drafts := s.Store.BlockedDraftsDue(key, s.Now, s.Limits.Window); len(drafts) > 0 {
s.needsHuman(ctx, key, repo, number, "the hive's proxy refused the reply, so it was not posted", nil, drafts, &res)
}
if len(fb.Items) == 0 {
continue
}
d := s.Store.Plan(key, fb.Items, s.Now, s.Limits)
Expand All @@ -136,11 +233,11 @@ func (s reviewFeedbackSweep) run(ctx context.Context, parse func(key string) (re
s.Logger.Debug("review feedback: burst still arriving, waiting to coalesce", "repo", repo, "pr", number, "comments", len(d.Items))
case reviewfeedback.VerdictCapReached:
res.CapReached++
s.needsHuman(ctx, key, repo, number, "it has been routed to its agent "+strconv.Itoa(s.Limits.WindowCap)+" times in "+s.Limits.Window.String()+"; further feedback is waiting, and a new comment from a person resets this", d.Items, &res)
s.needsHuman(ctx, key, repo, number, "it has been routed to its agent "+strconv.Itoa(s.Limits.WindowCap)+" times in "+s.Limits.Window.String()+"; further feedback is waiting, and a new comment from a person resets this", d.Items, nil, &res)
case reviewfeedback.VerdictKick:
recorded := s.recordedRules(repo, number, d.Items)
if route.Agent == "" {
s.needsHuman(ctx, key, repo, number, route.Reason, d.Items, &res)
s.needsHuman(ctx, key, repo, number, route.Reason, d.Items, nil, &res)
continue
}
brief := reviewfeedback.BuildBriefWithRules(repo, number, fb.HeadRef, s.WritingGuide, d.Items, recorded)
Expand Down Expand Up @@ -174,16 +271,23 @@ func (s reviewFeedbackSweep) run(ctx context.Context, parse func(key string) (re
return res
}

func (s reviewFeedbackSweep) needsHuman(ctx context.Context, key, repo string, number int, reason string, items []github.FeedbackItem, res *reviewFeedbackResult) {
if s.Store.NeedsHumanNoted(key) {
// needsHuman posts the needs-human note: once per PR when feedback cannot be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Advisory (style): needsHuman now has a drafts parameter that switches the dedupe path (NeedsHumanNoted versus MarkBlockedNoted), and two existing callers pass a bare nil. Splitting this into two small methods would read more clearly than a nil positional argument that changes behaviour. Not blocking.

// routed, or with drafts set, the replies the agent drafted but could not
// post (#19096), at most once per Limits.Window (BlockedDraftsDue).
func (s reviewFeedbackSweep) needsHuman(ctx context.Context, key, repo string, number int, reason string, items []github.FeedbackItem, drafts []reviewfeedback.ReplyDraft, res *reviewFeedbackResult) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needsHuman now has two modes selected by len(drafts), and three call sites pass nil. A separate method for the blocked-draft hand-off would be easier to read.

if len(drafts) == 0 && s.Store.NeedsHumanNoted(key) {
return
}
s.Logger.Warn("review feedback: cannot route to an agent, asking a person", "repo", repo, "pr", number, "reason", reason, "comments", len(items))
if err := s.Source.CreateIssueComment(ctx, repo, number, reviewfeedback.NeedsHumanNote(reason, items)); err != nil { // writeback:solicited a person's review feedback on this hive-authored PR
s.Logger.Warn("review feedback: cannot route to an agent, asking a person", "repo", repo, "pr", number, "reason", reason, "comments", len(items), "drafted_replies", len(drafts))
if err := s.Source.CreateIssueComment(ctx, repo, number, reviewfeedback.NeedsHumanNote(reason, items, drafts)); err != nil { // writeback:solicited a person's review feedback on this hive-authored PR
s.Logger.Warn("review feedback: needs-human note not posted", "repo", repo, "pr", number, "error", err)
return
}
if _, err := s.Store.MarkNeedsHuman(key, s.Now); err != nil {
if len(drafts) > 0 {
if err := s.Store.MarkBlockedNoted(key, drafts, s.Now); err != nil {
s.Logger.Warn("review feedback: could not persist drafted-replies hand-off", "repo", repo, "pr", number, "error", err)
}
} else if _, err := s.Store.MarkNeedsHuman(key, s.Now); err != nil {
s.Logger.Warn("review feedback: could not persist needs-human flag", "repo", repo, "pr", number, "error", err)
}
res.NeedsHuman++
Expand Down Expand Up @@ -420,13 +524,7 @@ func sweepReviewFeedback(ctx context.Context, cfg *config.Config, client *github
return
}
reviewFeedbackLastRun = now
if reviewFeedbackStore == nil {
st, err := reviewfeedback.Open(reviewfeedback.Dir())
if err != nil {
logger.Warn("review feedback: state unreadable or not writable; handled ids kept in memory only", "dir", reviewfeedback.Dir(), "error", err)
}
reviewFeedbackStore = st
}
store := openReviewFeedbackStore(logger)
rulesCfg, rulesOn := reviewRulesPolicy(cfg)
if reviewRuleStore == nil && rulesOn {
rs, err := reviewrules.Open(reviewrules.Dir(), reviewrules.VaultDir(), rulesCfg)
Expand All @@ -445,7 +543,7 @@ func sweepReviewFeedback(ctx context.Context, cfg *config.Config, client *github
available := agentAvailability(cfg, agentMgr)
sweep := reviewFeedbackSweep{
Source: client,
Store: reviewFeedbackStore,
Store: store,
Owners: auditPRAgents(cfg.Project.Org, now.Add(-auditPRAttributionWindow), ""),
Kicker: agentKicker{mgr: agentMgr},
WritingGuide: cfg.Project.WritingGuideSection(),
Expand Down
92 changes: 92 additions & 0 deletions src/cmd/hive/review_feedback_wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/hivecommons/hive/crux/proxy"
"github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/reviewfeedback"
)
Expand Down Expand Up @@ -223,3 +224,94 @@ func TestParseReviewFeedbackKey(t *testing.T) {
}
}
}

// A reply the proxy refused (#19096) is handed to a person once with the
// drafted text, even when no new feedback is waiting, and a second pass inside
// the window posts nothing more.
func TestReviewFeedbackSweep_RefusedReplyPostsDraftsOnce(t *testing.T) {
fb := openDraftFeedback()
fb.Items = nil
src := &fakeFeedbackSource{byKey: map[string]github.PRFeedback{feedbackTestKey: fb}}
now := time.Now()
s := feedbackSweepFor(t, src, &recordingKicker{}, routeTo("quality"), now)
if err := s.Store.RecordBlockedReply(feedbackTestKey, reviewfeedback.ReplyDraft{InReplyTo: "4616203214", Body: "Verified the scorecard; fixed in abc123.", Agent: "quality", At: now}); err != nil {
t.Fatal(err)
}
res := s.run(context.Background(), parseReviewFeedbackKey)
if res.NeedsHuman != 1 || len(src.comments) != 1 {
t.Fatalf("needs_human=%d comments=%d, want one note", res.NeedsHuman, len(src.comments))
}
for _, want := range []string{github.FeedbackNeedsHumanMarker, "could not post its replies", "4616203214", "fixed in abc123"} {
if !strings.Contains(src.comments[0], want) {
t.Errorf("note missing %q:\n%s", want, src.comments[0])
}
}
s.Now = now.Add(10 * time.Minute)
if res := s.run(context.Background(), parseReviewFeedbackKey); res.NeedsHuman != 0 || len(src.comments) != 1 {
t.Fatalf("second pass must not post again: needs_human=%d comments=%d", res.NeedsHuman, len(src.comments))
}
}

func TestReviewThreadOwners_AuditOwnershipCachedAndCaseInsensitive(t *testing.T) {
loads := 0
now := time.Now()
o := &reviewThreadOwners{
org: func() string { return "cncf" },
load: func(org string, _ time.Time) map[string]string {
loads++
return map[string]string{"cncf/techdocs#424": "quality"}
},
now: func() time.Time { return now },
}
cases := []struct {
name string
repo string
number int
wantAgent string
wantOK bool
}{
{"hive-authored PR", "cncf/techdocs", 424, "quality", true},
{"case-insensitive repo", "CNCF/TechDocs", 424, "quality", true},
{"other PR", "cncf/techdocs", 425, "", false},
{"other repo", "cncf/toc", 424, "", false},
}
for _, tc := range cases {
agent, ok := o.owner(tc.repo, tc.number)
if agent != tc.wantAgent || ok != tc.wantOK {
t.Errorf("%s: owner=%q ok=%v, want %q %v", tc.name, agent, ok, tc.wantAgent, tc.wantOK)
}
}
if loads != 1 {
t.Fatalf("audit read %d times inside the TTL, want 1", loads)
}
now = now.Add(reviewThreadOwnersTTL)
o.owner("cncf/techdocs", 424)
if loads != 2 {
t.Fatalf("audit must be re-read after the TTL, loads=%d", loads)
}
}

func TestNewReviewThreadGuard_LoginsAndRefusedReplyKept(t *testing.T) {
t.Setenv(reviewfeedback.DirEnvVar, t.TempDir())
reviewFeedbackStoreMu.Lock()
prev := reviewFeedbackStore
reviewFeedbackStore = nil
reviewFeedbackStoreMu.Unlock()
t.Cleanup(func() {
reviewFeedbackStoreMu.Lock()
reviewFeedbackStore = prev
reviewFeedbackStoreMu.Unlock()
})
g := newReviewThreadGuard(func() string { return "cncf" }, func() string { return " hive-app[bot] " }, discardLogger())
if got := g.HiveLogins(); len(got) != 1 || got[0] != "hive-app[bot]" {
t.Fatalf("logins = %v", got)
}
if got := newReviewThreadGuard(func() string { return "" }, func() string { return "" }, discardLogger()).HiveLogins(); got != nil {
t.Fatalf("no App login must yield no logins, got %v", got)
}
now := time.Now()
g.OnRefused(proxy.ReviewWriteRefusal{Agent: "quality", Repo: "cncf/techdocs", Number: 424, InReplyTo: "1", Body: "drafted", At: now})
if due := openReviewFeedbackStore(discardLogger()).BlockedDraftsDue(feedbackTestKey, now, time.Hour); len(due) != 1 || due[0].Body != "drafted" {
t.Fatalf("refused reply not kept: %+v", due)
}
}
3 changes: 3 additions & 0 deletions src/crux/proxy/github_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ type GitHubProxy struct {
referenceRepos map[string]ReferenceRepoPolicy
referenceDefaults map[string]referenceDefaultBranch
referenceLookup referenceLookupFunc
// reviewGuard holds the ownership seams for review-thread replies and
// own-comment deletes on the hive's own PRs (#19096, review_thread.go).
reviewGuard reviewGuardState

// repoPaused reports whether a repo ("owner/repo", as ExtractRepo yields it)
// is under an operator pause (#6203). Guarded because the dashboard can
Expand Down
10 changes: 10 additions & 0 deletions src/crux/proxy/github_proxy_mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str
blockReason = reason
blockCode = DenyReasonRepoAutoMergeOff
blockJSON = true
} else if reason, refused := p.reviewThreadRefusal(req, agentName); !isLinear && refused {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check !isLinear first so the guard (which may do a GitHub GET) is never called for Linear traffic, as the sibling branches do. E.g. fold into else if !isLinear { if reason, refused := ...; refused {...} } or reorder the condition via a small helper.

// Review-thread replies, PR review comments and comment deletes
// (#19096): allowed only on the hive's own PR for the agent that
// opened it, or for the hive's own recent comment.
blocked = true
blockReason = reason
blockCode = DenyReasonReviewThread
} else if !isLinear {
if reason, kind, refused := p.writeSurfaceEnforceRefusal(agentName, req.Method, req.URL.Path, req.URL.RawQuery); refused {
blocked = true
Expand Down Expand Up @@ -279,6 +286,9 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str
detail = blockReason
}
p.recordViolation(agentName, req.Method, detail)
if agentName != internalCallerName && !isLinear {
p.noteRefusedReviewReply(req, agentName, detail)
}
if writeSurfaceKind != "" {
p.auditWriteRefused(agentName, writeSurfaceKind, req.Method, repoScopePath(req))
}
Expand Down
9 changes: 8 additions & 1 deletion src/crux/proxy/reference_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,15 @@ func ReferenceRepoDecision(pol ReferenceRepoPolicy, method, path, rawQuery strin
m := refItemCommentsPath.FindStringSubmatch(path)
n, _ := strconv.Atoi(m[1])
return referenceOwnItem, n, ""
case method == "POST" && reviewReplyPathRe.MatchString(path),
method == "DELETE" && ownCommentDeletePathRe.MatchString(path):
// A reply inside a review thread, or retracting the hive's own recent
// comment (#19096). The ownership checks (the hive's own PR and its
// opening agent, or the hive's own comment inside the retract window)
// are the review-thread gate's, which runs right after this one.
return referenceAllow, 0, ""
}
return referenceDeny, 0, referenceRepoRefusal(pol, "only opening an issue or PR, forking it, pushing to the owner's fork and commenting on the hive's own items are ever allowed there — never merge, push to the default branch, label, assign, close or review")
return referenceDeny, 0, referenceRepoRefusal(pol, "only opening an issue or PR, forking it, pushing to the owner's fork, commenting on the hive's own items, replying in review threads on the hive's own PRs and retracting the hive's own recent comment are ever allowed there — never merge, push to the default branch, label, assign, close or review")
}

func referenceRepoRefusal(pol ReferenceRepoPolicy, why string) string {
Expand Down
Loading
Loading