gunk-dev/the-valley

review_check.go

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