gunk-dev/the-valley

review_check.go

source6a6791929c36main's published tip history
5.4 KiB
1package cmd
2
3import (
4 "strings"
5 "time"
6
7 "the-valley/flow/internal/github"
8 "the-valley/flow/internal/pipeline"
9)
10
11// isAgentReply reports whether a comment body was posted by a fix agent.
12// Agents run gh as the operator, who is often a trusted reviewer, so the
13// author login can't tell their replies apart; the prompt has them tag each
14// reply with an agent-reply marker instead. Counting those replies would
15// make every agent reply look like fresh trusted feedback and redispatch.
16func isAgentReply(body string) bool {
17 return pipeline.IsAgentReply(body)
18}
19
20// isOptedIn reports whether a conversation comment explicitly asks for a fix:
21// its first non-blank line is a fix directive (/klaus fix or /flow fix,
22// optionally followed by text), or some line is exactly an actionable marker
23// (<!-- klaus-actionable --> or <!-- flow-actionable -->). Both must stand on
24// their own line, so a reply that quotes an opted-in comment ("> /klaus fix")
25// doesn't inherit the opt-in.
26func isOptedIn(body string) bool {
27 first := true
28 for _, line := range strings.Split(body, "\n") {
29 line = strings.TrimSpace(line)
30 if line == "" {
31 continue
32 }
33 if pipeline.IsActionableMarker(line) {
34 return true
35 }
36 if first {
37 first = false
38 if pipeline.HasFixCommand(line) {
39 return true
40 }
41 }
42 }
43 return false
44}
45
46// hasUnaddressedTrustedComments reports whether a PR has comments from
47// trusted reviewers that no push has addressed since. Both inline review
48// comments and PR conversation comments count. Conversation comments by the
49// PR author count only when opted in (see isOptedIn), because the operator
50// and fix agents share that account; inline review comments don't treat the
51// author specially.
52func hasUnaddressedTrustedComments(a *github.PRActivity, trustedReviewers []string) bool {
53 _, unaddressed := trustedFeedback(a, trustedReviewers)
54 return unaddressed
55}
56
57// trustedFeedback returns when trusted reviewers last left actionable
58// feedback on the PR (zero when never), and whether no push has addressed it
59// since.
60func trustedFeedback(a *github.PRActivity, trustedReviewers []string) (time.Time, bool) {
61 if a == nil || len(trustedReviewers) == 0 {
62 return time.Time{}, false
63 }
64 trustedSet := make(map[string]bool, len(trustedReviewers))
65 for _, r := range trustedReviewers {
66 trustedSet[r] = true
67 }
68
69 latest := latestTrustedReviewTime(a, trustedSet)
70 if t := latestTrustedConversationCommentTime(a, trustedSet); t.After(latest) {
71 latest = t
72 }
73 if latest.IsZero() {
74 return latest, false
75 }
76 if a.LastCommitAt.IsZero() {
77 // Can't determine commit time; assume comments are unaddressed.
78 return latest, true
79 }
80 // The head's committer date, not its author date: author dates survive
81 // rebases unchanged and can be arbitrarily old, which would falsely mark
82 // a fresh push as predating the reviews it addresses.
83 return latest, latest.After(a.LastCommitAt)
84}
85
86// latestTrustedReviewTime returns the submission time of the most recent
87// actionable review from a trusted reviewer, or the zero time if there is none.
88//
89// A `klaus review --post` review is trusted when the operator's gh account
90// posted it; the marker alone is not, or anyone could trigger fix agents.
91//
92// Only reviews with at least one inline comment not written by a fix agent
93// are actionable. A body-only review (e.g. gemini-code-assist's summary
94// COMMENTED review) gives a fix agent nothing to address: the agent correctly
95// pushes no commit, so the commit-time watermark never advances and the review
96// would read as "unaddressed" forever, redispatching fix agents in a loop.
97// Body-only CHANGES_REQUESTED reviews still flip GitHub's reviewDecision and
98// are handled by the changes-requested path instead. A reply posted through
99// the replies endpoint creates its own review, so an agent's reply must not
100// make its review count.
101func latestTrustedReviewTime(a *github.PRActivity, trustedSet map[string]bool) time.Time {
102 var latest time.Time
103 for _, r := range a.Reviews {
104 if !trustedSet[r.Author] {
105 if !pipeline.HasCrossReviewMarker(r.Body) || a.Operator == "" || r.Author != a.Operator {
106 continue
107 }
108 }
109 state := strings.ToUpper(r.State)
110 if state != "COMMENTED" && state != "CHANGES_REQUESTED" {
111 continue
112 }
113 inline := false
114 for _, body := range r.Comments {
115 if !isAgentReply(body) {
116 inline = true
117 break
118 }
119 }
120 if inline && r.SubmittedAt.After(latest) {
121 latest = r.SubmittedAt
122 }
123 }
124 return latest
125}
126
127// latestTrustedConversationCommentTime returns the last-updated time of the
128// most recent actionable PR conversation comment from a trusted reviewer, or
129// the zero time if there is none.
130//
131// A comment by the PR author is actionable only when opted in: the operator
132// and fix agents post through the same account, so an agent reply that omits
133// or mangles pipeline.AgentReplyMarker must not read as fresh feedback. The
134// marker check stays as a second guard. If the PR author isn't known, every
135// comment is held to the opt-in rule, failing toward "no dispatch".
136//
137// The last edit is the watermark, so a reviewer editing an older comment
138// after the latest push to add feedback is picked up.
139func latestTrustedConversationCommentTime(a *github.PRActivity, trustedSet map[string]bool) time.Time {
140 var latest time.Time
141 for _, c := range a.Comments {
142 if !trustedSet[c.Author] || isAgentReply(c.Body) {
143 continue
144 }
145 if (a.Author == "" || c.Author == a.Author) && !isOptedIn(c.Body) {
146 continue
147 }
148 if c.UpdatedAt.After(latest) {
149 latest = c.UpdatedAt
150 }
151 }
152 return latest
153}