Repository navigation
🐛 Let the agent that opened a PR reply on its review threads #19102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dc791e0
aa6b063
437946c
fba7857
b9aa19c
e6bdfdf
946c459
e20206c
1acd85f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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) | ||
|
|
@@ -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) | ||
|
|
@@ -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) | ||
|
|
@@ -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 | ||
| // 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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++ | ||
|
|
@@ -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) | ||
|
|
@@ -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(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check |
||
| // 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 | ||
|
|
@@ -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)) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Advisory (style):
needsHumannow has adraftsparameter that switches the dedupe path (NeedsHumanNotedversusMarkBlockedNoted), and two existing callers pass a barenil. Splitting this into two small methods would read more clearly than a nil positional argument that changes behaviour. Not blocking.