feat(daemon,forge): the review-reaction producers

Sequencing steps 6 and 7 of pr-review-remediation.md: the consumer
landed in 33e915c5, and these are the two sources that feed it.

Webhook (decision 2, low latency): the GitHub receiver decodes
pull_request_review and pull_request_review_comment events behind the
existing HMAC verification and publishes the same typed reaction the
poller produces, keyed source-independently so PublishUnique collapses
the two sources. Only submitted/created carry a verdict to remediate;
edited and dismissed are acknowledged. Reaction deliveries are
rate-limited per source (decision 6's 'add when the receiver gains a
second event family'), and the reaction path carries no dispatch
predicate -- eligibility is the consumer's owned-PR lookup.

Poll (both forges, the no-webhook backstop): one shared scan over all
pr_open tasks, each with the forge client its own identity owns
(reconcilePRs's shape), listing reviews and comments since per-task
persisted high-water marks and skipping the owning identity's bot user
(decision 5's own-comment exclusion, which the poll is positioned to
make identity-correct).

The cursors are two, not one: review IDs and comment IDs are independent
forge sequences, and a combined high-water mark would silently skip
reviews once a larger comment ID landed. watch_comment_id -- dead since
the comment-watch design it was created for never shipped -- carries the
comment cursor; review_cursor is a new presence-gated migrator column,
and SetReviewCursors is its guarded RPC (pr_open only, so cursors never
move under a run that owns the task). forge.ReviewComment gains ReviewID
so a comment is collectable into its review's unit (decision 5) on both
forges, and forge.Review gains Body so poll-delivered reviews carry
their summary.

Note: the store/proto/facade half of this work landed early in aac31b69
under a release session's message -- a shared-checkout collision, kept
rather than rewritten; that commit carries the review_cursor migration
and the SetReviewCursors contract, this one carries the producers.
This commit is contained in:
sam committed 2026-09-23 12:41:53 +10:00
1 parent 51bc6d2df0
commit 6046aaf4c6
15 files changed
+699 -41

No files matched your search

+12 -1
View File
@@ -1367,7 +1367,18 @@ func (b *boot) setupForgeWebhook() {
"engine", cfg.Forge.WebhookSecret.Engine, "key", cfg.Forge.WebhookSecret.Key, "err", err)
return
}
receiver := forgewebhook.New(secretValue, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, log)
// The reaction publisher is the consumer's supply side: the same typed
// reaction the poll produces, keyed source-independently, so webhook and
// poll deliveries of one review dedup (pr-review-remediation.md
// decision 2).
publishReaction := func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
payload, err := reaction.Encode()
if err != nil {
return err
}
return b.d.Tasks.PublishUnique(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
}
receiver := forgewebhook.New(secretValue, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, publishReaction, log)
host, port := parseListenAddr(cfg.Forge.WebhookAddr, "0.0.0.0", 8645)
addr := fmt.Sprintf("%s:%d", host, port)
srv := &http.Server{Addr: addr, Handler: receiver}
+5
View File
@@ -137,6 +137,9 @@ type Daemon struct {
// reactionConsumer is built lazily by drainReactions and kept for its
// lifetime drop counter.
reactionConsumer *reactionConsumer
// reactionPublisher is built lazily by scanPRReviews; an indirection
// only so tests can swap the delivery without the bus.
reactionPublisher ReactionPublisher
// Reactions pulls the review-reaction fan-out stream
// (docs/prds/pr-review-remediation.md). Each cycle it is drained into
// queued remediate runs before the task drain, so a reaction can be
@@ -497,6 +500,7 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) {
func (d *Daemon) maintainAndDrain(ctx context.Context) {
d.cleanupExpiredStorage(ctx)
d.reconcilePRs(ctx)
d.scanPRReviews(ctx)
d.dispatchBindings(ctx)
d.drainReactions(ctx)
if d.Tasks != nil {
@@ -572,6 +576,7 @@ func (d *Daemon) Cycle(ctx context.Context) {
d.cleanupExpiredStorage(ctx)
d.poll(ctx)
d.reconcilePRs(ctx)
d.scanPRReviews(ctx)
d.dispatchBindings(ctx)
d.drainReactions(ctx)
if d.Tasks != nil {
+4
View File
@@ -91,6 +91,10 @@ func (f *fakeRemediations) UpdateReviewPayload(ctx context.Context, taskID int64
return nil
}
func (f *fakeRemediations) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
return nil
}
type fakeReviewLookup struct {
task *workflowtask.Task
err error
+141
View File
@@ -0,0 +1,141 @@
package daemon
import (
"context"
"fmt"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/forge"
)
// ReactionPublisher delivers one reaction to the reaction stream. The
// composition root wires it to the task bus's PublishUnique against the
// reaction subject, so webhook and poll deliveries of the same record dedup
// on the envelope's source-independent key.
type ReactionPublisher func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error
// scanPRReviewReactions is the poll backstop's core for one PR (decision 2):
// list review activity since the task's persisted cursors, publish every
// non-own record as a reaction, and return the advanced cursors. The cursors
// advance past every record seen -- skipped own comments included -- so a
// record is listed once and never re-listed on a later scan. On a publish
// failure the error carries: the caller keeps the old cursors, and the next
// scan re-fetches the records it did not get to (PublishUnique's dedup
// absorbs any re-delivery).
func scanPRReviewReactions(
ctx context.Context,
reader forge.PullRequestReviewReader,
task *workflowtask.Task,
botUser string,
reviewCursor, commentCursor int64,
publish ReactionPublisher,
) (int64, int64, error) {
newReviewCursor, newCommentCursor := reviewCursor, commentCursor
reviews, err := reader.ListReviews(ctx, task.Owner, task.Repo, task.PRNumber, reviewCursor)
if err != nil {
return reviewCursor, commentCursor, err
}
for _, r := range reviews {
if r.ID > newReviewCursor {
newReviewCursor = r.ID
}
if botUser != "" && r.Author == botUser {
continue
}
reaction := workintake.ReviewCommentEnvelope{
Owner: task.Owner, Repo: task.Repo, PRNumber: task.PRNumber,
Kind: workintake.ReviewReactionReview,
ReviewID: r.ID, Author: r.Author, State: r.State, Body: r.Body,
}
if err := publish(ctx, reaction); err != nil {
return reviewCursor, commentCursor, fmt.Errorf("publish review %d: %w", r.ID, err)
}
}
comments, err := reader.ListReviewComments(ctx, task.Owner, task.Repo, task.PRNumber, commentCursor)
if err != nil {
return reviewCursor, commentCursor, err
}
for _, cm := range comments {
if cm.ID > newCommentCursor {
newCommentCursor = cm.ID
}
if botUser != "" && cm.Author == botUser {
continue
}
reaction := workintake.ReviewCommentEnvelope{
Owner: task.Owner, Repo: task.Repo, PRNumber: task.PRNumber,
Kind: workintake.ReviewReactionComment,
ReviewID: cm.ReviewID,
CommentID: cm.ID,
Author: cm.Author, Body: cm.Body, Path: cm.Path, Line: cm.Line,
}
if err := publish(ctx, reaction); err != nil {
return reviewCursor, commentCursor, fmt.Errorf("publish comment %d: %w", cm.ID, err)
}
}
return newReviewCursor, newCommentCursor, nil
}
// scanPRReviews runs the shared per-PR review scan over every open PR task
// (reconcilePRs's shape): one pass over all pr_open tasks, each scanned with
// the forge client its own identity owns. A forge without the review-reader
// capability is skipped, not an error. Wired after reconcilePRs so a PR
// that just left pr_open is not scanned for reactions in the same pass.
func (d *Daemon) scanPRReviews(ctx context.Context) {
if d.Store == nil || d.Tasks == nil {
return
}
tasks, err := d.Store.OpenPRs(ctx)
if err != nil {
d.Log.Error("review scan query failed", "err", err)
return
}
if d.reactionPublisher == nil {
d.reactionPublisher = d.publishReaction
}
for i := range tasks {
t := &tasks[i]
reader, ok := d.forgeFor(t).(forge.PullRequestReviewReader)
if !ok {
continue // the noop forge and any forge without the capability
}
d.scanOnePR(ctx, reader, t)
}
}
func (d *Daemon) scanOnePR(ctx context.Context, reader forge.PullRequestReviewReader, t *workflowtask.Task) {
// A forge read failure skips the task for this scan with its cursors
// unchanged: the next scan re-fetches, and PublishUnique's dedup covers
// anything the previous scan already published.
reviewCursor, commentCursor, err := scanPRReviewReactions(
ctx, reader, t, d.botUserForTask(t),
t.ReviewCursor, t.WatchCommentID, d.reactionPublisher)
if err != nil {
d.Log.Warn("review scan failed", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber, "err", err)
return
}
if reviewCursor == t.ReviewCursor && commentCursor == t.WatchCommentID {
return
}
if err := d.Store.SetReviewCursors(ctx, t.ID, reviewCursor, commentCursor); err != nil {
// A stale guard here means the task was claimed between the scan and
// the write; the next scan re-reads and re-lists from the old cursor,
// and PublishUnique plus the consumer's dedup absorb the repeats.
d.Log.Warn("review cursor persist failed", "task", t.ID, "err", err)
}
}
// publishReaction encodes one reaction and publishes it on the reaction
// subject with its source-independent idempotency key, so a webhook delivery
// and a poll record of the same review collapse into one.
func (d *Daemon) publishReaction(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
payload, err := reaction.Encode()
if err != nil {
return err
}
return d.Tasks.PublishUnique(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
}
+138
View File
@@ -0,0 +1,138 @@
package daemon
import (
"context"
"errors"
"testing"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/forge"
)
type fakeReviewReader struct {
reviews []forge.Review
comments []forge.ReviewComment
listErr error
}
func (f *fakeReviewReader) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]forge.Review, error) {
// The real implementations filter on the forge; the fake models the
// contract so "records at or below the cursor never come back" is
// actually exercised.
var out []forge.Review
for _, r := range f.reviews {
if r.ID > sinceID {
out = append(out, r)
}
}
return out, f.listErr
}
func (f *fakeReviewReader) ListReviewComments(ctx context.Context, owner, repo string, number int, sinceID int64) ([]forge.ReviewComment, error) {
var out []forge.ReviewComment
for _, c := range f.comments {
if c.ID > sinceID {
out = append(out, c)
}
}
return out, f.listErr
}
func (f *fakeReviewReader) ReplyToReview(ctx context.Context, owner, repo string, number int, inReplyTo int64, body string) error {
return nil
}
type recordingPublisher struct {
reactions []workintake.ReviewCommentEnvelope
err error
}
func (p *recordingPublisher) publish(ctx context.Context, e workintake.ReviewCommentEnvelope) error {
if p.err != nil {
return p.err
}
p.reactions = append(p.reactions, e)
return nil
}
func openTask(prNumber int, reviewCursor, commentCursor int64) *workflowtask.Task {
return &workflowtask.Task{ID: 7, Owner: "acme", Repo: "widgets", PRNumber: prNumber, Status: "pr_open", ReviewCursor: reviewCursor, WatchCommentID: commentCursor}
}
func TestScanPRReviewReactionsPublishesNewRecordsAndSkipsOwn(t *testing.T) {
reader := &fakeReviewReader{
reviews: []forge.Review{
{ID: 5, Author: "archie-bot", State: "requested_changes", Body: "mine"},
{ID: 7, Author: "alice", State: "requested_changes", Body: "please fix"},
},
comments: []forge.ReviewComment{
{ID: 9, ReviewID: 7, Author: "archie-bot", Body: "my reply"},
{ID: 11, ReviewID: 7, Author: "bob", Body: "also this", Path: "x.go", Line: 3},
},
}
publisher := &recordingPublisher{}
task := openTask(42, 0, 0)
reviewCursor, commentCursor, err := scanPRReviewReactions(
context.Background(), reader, task, "archie-bot", 0, 0, publisher.publish)
if err != nil {
t.Fatal(err)
}
// The own review and own reply are excluded; alice's review and bob's
// comment are the two actionable records.
if len(publisher.reactions) != 2 {
t.Fatalf("published = %d, want 2", len(publisher.reactions))
}
if publisher.reactions[0].Kind != workintake.ReviewReactionReview || publisher.reactions[0].ReviewID != 7 {
t.Errorf("first reaction = %+v, want alice's review 7", publisher.reactions[0])
}
if publisher.reactions[1].Kind != workintake.ReviewReactionComment || publisher.reactions[1].CommentID != 11 {
t.Errorf("second reaction = %+v, want bob's comment 11", publisher.reactions[1])
}
if publisher.reactions[1].ReviewID != 7 {
t.Errorf("comment reaction ReviewID = %d, want 7 (collected under its review)", publisher.reactions[1].ReviewID)
}
if publisher.reactions[1].Body != "also this" || publisher.reactions[1].Path != "x.go" || publisher.reactions[1].Line != 3 {
t.Errorf("comment payload = %+v, want the comment's own content", publisher.reactions[1])
}
// Cursors advance past every record seen, own or not: a cursor that
// stalls on a skipped record would re-list it every scan forever.
if reviewCursor != 7 || commentCursor != 11 {
t.Errorf("cursors = (%d, %d), want (7, 11)", reviewCursor, commentCursor)
}
}
func TestScanPRReviewReactionsSkipRecordsAtOrBelowTheCursors(t *testing.T) {
reader := &fakeReviewReader{
reviews: []forge.Review{{ID: 5, Author: "alice", State: "requested_changes", Body: "old"}},
comments: []forge.ReviewComment{{ID: 6, ReviewID: 5, Author: "bob", Body: "old comment"}},
}
publisher := &recordingPublisher{}
reviewCursor, commentCursor, err := scanPRReviewReactions(
context.Background(), reader, openTask(42, 5, 6), "archie-bot", 5, 6, publisher.publish)
if err != nil {
t.Fatal(err)
}
if len(publisher.reactions) != 0 {
t.Errorf("published %d reactions for records at or below the cursors", len(publisher.reactions))
}
if reviewCursor != 5 || commentCursor != 6 {
t.Errorf("cursors = (%d, %d), want unchanged (5, 6)", reviewCursor, commentCursor)
}
}
func TestScanPRReviewReactionsKeepsCursorsOnPublishFailure(t *testing.T) {
reader := &fakeReviewReader{
reviews: []forge.Review{{ID: 7, Author: "alice", State: "requested_changes", Body: "fix"}},
}
publisher := &recordingPublisher{err: errors.New("bus unavailable")}
_, _, err := scanPRReviewReactions(
context.Background(), reader, openTask(42, 0, 0), "archie-bot", 0, 0, publisher.publish)
if err == nil {
t.Fatal("publish error was swallowed; the cursors must not advance past unpublished records")
}
}
@@ -80,7 +80,7 @@ type RemediationStarter interface {
// comment high-water marks (pr-review-remediation.md decision 2). The
// pr_open guard is part of the contract: cursors only move while no
// remediation run owns the task.
SetReviewCursors(ctx context.Context, taskID int64, reviewCursor, commentCursor int64) error
SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error
}
// TaskQueries groups read-only task accessors.
+9 -1
View File
@@ -123,6 +123,7 @@ type Review struct {
ID int64
Author string
State string // one of the ReviewState* constants
Body string // the review's own top-level summary; empty for a bare verdict
SubmittedAt time.Time
}
@@ -131,7 +132,14 @@ type Review struct {
// the whole file. InReplyTo is the parent comment ID on GitHub; Gitea's
// inline comments are flat under a review, so it is 0 there.
type ReviewComment struct {
ID int64
ID int64
// ReviewID is the review this comment belongs to. It is what lets a
// comment be collected into its review's remediation unit instead of
// starting one of its own (pr-review-remediation.md decision 5); a
// standalone comment carries 0. GitHub carries it on the comment;
// Gitea's flat comment list is walked per review, so the mapping comes
// from the walk.
ReviewID int64
Author string
Body string
Path string
+2
View File
@@ -188,6 +188,7 @@ func (c *GiteaClient) ListReviews(ctx context.Context, owner, repo string, numbe
ID: r.ID,
Author: giteaReviewAuthor(r.Reviewer),
State: normalizeGiteaReviewState(r.State),
Body: r.Body,
SubmittedAt: r.Submitted,
})
}
@@ -219,6 +220,7 @@ func (c *GiteaClient) ListReviewComments(ctx context.Context, owner, repo string
}
out = append(out, ReviewComment{
ID: cm.ID,
ReviewID: rv.ID,
Author: giteaReviewAuthor(cm.Reviewer),
Body: cm.Body,
Path: cm.Path,
+2
View File
@@ -207,6 +207,7 @@ func (c *GitHubClient) ListReviews(ctx context.Context, owner, repo string, numb
ID: r.GetID(),
Author: r.GetUser().GetLogin(),
State: normalizeReviewState(r.GetState()),
Body: r.GetBody(),
SubmittedAt: r.GetSubmittedAt().Time,
})
}
@@ -237,6 +238,7 @@ func (c *GitHubClient) ListReviewComments(ctx context.Context, owner, repo strin
}
out = append(out, ReviewComment{
ID: cm.GetID(),
ReviewID: cm.GetPullRequestReviewID(),
Author: cm.GetUser().GetLogin(),
Body: cm.GetBody(),
Path: cm.GetPath(),
+201
View File
@@ -0,0 +1,201 @@
package webhook
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-github/v78/github"
"github.com/samcharles93/archie-core/internal/domain/workintake"
)
// reviewEvent builds a signed pull_request_review delivery, GitHub's
// low-latency path for "a human reacted to archie's PR" (decision 2).
func reviewEvent(t *testing.T, action string, mutate func(*github.PullRequestReviewEvent)) *http.Request {
t.Helper()
ev := &github.PullRequestReviewEvent{
Action: new(action),
Review: &github.PullRequestReview{
ID: new(int64(7)),
State: new("requested_changes"),
Body: new("please fix the loop"),
User: &github.User{Login: new("alice")},
},
PullRequest: &github.PullRequest{Number: new(42)},
Repo: &github.Repository{
Name: new("widgets"),
Owner: &github.User{Login: new("acme")},
},
}
if mutate != nil {
mutate(ev)
}
payload, err := json.Marshal(ev)
if err != nil {
t.Fatal(err)
}
return signedRequest(t, testSecret, "pull_request_review", payload)
}
func reviewCommentEvent(t *testing.T, action string, mutate func(*github.PullRequestReviewCommentEvent)) *http.Request {
t.Helper()
ev := &github.PullRequestReviewCommentEvent{
Action: new(action),
Comment: &github.PullRequestComment{
ID: new(int64(11)),
PullRequestReviewID: new(int64(7)),
Body: new("also this"),
Path: new("x.go"),
Line: new(3),
User: &github.User{Login: new("bob")},
},
PullRequest: &github.PullRequest{Number: new(42)},
Repo: &github.Repository{
Name: new("widgets"),
Owner: &github.User{Login: new("acme")},
},
}
if mutate != nil {
mutate(ev)
}
payload, err := json.Marshal(ev)
if err != nil {
t.Fatal(err)
}
return signedRequest(t, testSecret, "pull_request_review_comment", payload)
}
func serveReaction(t *testing.T, r *Receiver, req *http.Request) (*httptest.ResponseRecorder, []workintake.ReviewCommentEnvelope) {
t.Helper()
var published []workintake.ReviewCommentEnvelope
r.reactionPublish = func(_ context.Context, e workintake.ReviewCommentEnvelope) error {
published = append(published, e)
return nil
}
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
return rec, published
}
func newReactionReceiver() *Receiver {
return New(testSecret, "label", testLabel, testBot, nil, nil, nil)
}
func TestReceiverPublishesASubmittedReviewAsAReaction(t *testing.T) {
r := newReactionReceiver()
rec, published := serveReaction(t, r, reviewEvent(t, "submitted", nil))
if rec.Code != http.StatusAccepted {
t.Fatalf("status = %d, want 202", rec.Code)
}
if len(published) != 1 {
t.Fatalf("published = %d, want 1", len(published))
}
e := published[0]
if e.Kind != workintake.ReviewReactionReview || e.ReviewID != 7 || e.Owner != "acme" || e.Repo != "widgets" || e.PRNumber != 42 {
t.Errorf("reaction = %+v, want review 7 on acme/widgets#42", e)
}
if e.State != "requested_changes" || e.Body != "please fix the loop" || e.Author != "alice" {
t.Errorf("reaction content = %+v, want the review's own state/body/author", e)
}
// The idempotency key is kinded (decision 3): a review ID and a comment
// ID come from independent forge sequences and must never collide.
if e.IdempotencyKey() == "" {
t.Error("reaction carried no idempotency key")
}
}
func TestReceiverPublishesACreatedCommentAsAReaction(t *testing.T) {
r := newReactionReceiver()
rec, published := serveReaction(t, r, reviewCommentEvent(t, "created", nil))
if rec.Code != http.StatusAccepted {
t.Fatalf("status = %d, want 202", rec.Code)
}
if len(published) != 1 {
t.Fatalf("published = %d, want 1", len(published))
}
e := published[0]
if e.Kind != workintake.ReviewReactionComment || e.CommentID != 11 || e.ReviewID != 7 {
t.Errorf("reaction = %+v, want comment 11 collected under review 7", e)
}
}
func TestReceiverIgnoresNonActionReviewActivity(t *testing.T) {
tests := []struct {
name string
kind string
action string
}{
{"dismissed review", "review", "dismissed"},
{"edited review", "review", "edited"},
{"deleted review", "review", "deleted"},
{"edited comment", "comment", "edited"},
{"deleted comment", "comment", "deleted"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := newReactionReceiver()
var req *http.Request
if tt.kind == "review" {
req = reviewEvent(t, tt.action, nil)
} else {
req = reviewCommentEvent(t, tt.action, nil)
}
rec, published := serveReaction(t, r, req)
if rec.Code != http.StatusAccepted {
t.Fatalf("status = %d, want 202", rec.Code)
}
if len(published) != 0 {
t.Errorf("published %d reactions for action %q", len(published), tt.action)
}
})
}
}
func TestReceiverReactionWithoutTheCapabilityIsUntouched(t *testing.T) {
// A receiver composed without the reaction publisher (nil) must not
// panic or publish when a review event arrives: the reaction path is an
// optional capability, the issue path is the receiver's core contract.
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, reviewEvent(t, "submitted", nil))
if rec.Code != http.StatusAccepted {
t.Fatalf("status = %d, want 202", rec.Code)
}
}
func TestReceiverRateLimitsReactionDeliveries(t *testing.T) {
r := newReactionReceiver()
// Drive the limiter to its per-window ceiling with one remote, then
// assert the next reaction from that remote is refused with 429 while a
// different remote still passes.
remote := "10.0.0.1:1234"
for i := range reactionRateLimit {
req := reviewEvent(t, "submitted", nil)
req.RemoteAddr = remote
rec, _ := serveReaction(t, r, req)
if rec.Code != http.StatusAccepted {
t.Fatalf("delivery %d within the window: status = %d, want 202", i+1, rec.Code)
}
}
req := reviewEvent(t, "submitted", nil)
req.RemoteAddr = remote
rec, published := serveReaction(t, r, req)
if rec.Code != http.StatusTooManyRequests {
t.Fatalf("over-limit status = %d, want 429", rec.Code)
}
if len(published) != 0 {
t.Errorf("over-limit delivery published %d reactions", len(published))
}
otherReq := reviewEvent(t, "submitted", nil)
otherReq.RemoteAddr = "10.0.0.2:1234"
rec, published = serveReaction(t, r, otherReq)
if rec.Code != http.StatusAccepted || len(published) != 1 {
t.Errorf("different remote blocked: status = %d, published = %d", rec.Code, len(published))
}
}
+176 -31
View File
@@ -55,37 +55,67 @@ const maxBodyBytes = 1 << 20 // 1 MiB
// (*daemon.Daemon).PublishTask, the same enqueue path the poller uses.
type PublishFunc func(ctx context.Context, task workintake.TaskEnvelope) error
// ReactionPublishFunc delivers one decoded review reaction to the reaction
// stream (pr-review-remediation.md decision 2's webhook producer). The
// composition root wires it to PublishUnique against the envelope's reaction
// subject, so a webhook delivery and a poll record of the same review dedup
// on the envelope's source-independent key. The reaction path carries no
// dispatch predicate: eligibility is the consumer's owned-PR lookup.
type ReactionPublishFunc func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error
// reactionRateLimit bounds reaction deliveries per remote source per window
// (decision 6's per-source rate limiting, due now that the receiver carries a
// second event family). It guards spend, not authorization: the owned-task
// guard at the consumer remains the authorization boundary.
const (
reactionRateLimit = 60
reactionRateWindow = time.Minute
reactionRateBucketTTL = 5 * time.Minute
)
// Receiver is the HTTP handler for forge webhooks. It verifies the signature,
// decodes the event, applies the shared dispatch predicate, and publishes
// matched issues.
type Receiver struct {
secret string
trigger string
label string
botUser string
publish PublishFunc
log *slog.Logger
secret string
trigger string
label string
botUser string
publish PublishFunc
reactionPublish ReactionPublishFunc
log *slog.Logger
mu sync.Mutex
startedAt time.Time
lastReceivedAt time.Time
deliveries uint64
publishes uint64
mu sync.Mutex
startedAt time.Time
lastReceivedAt time.Time
deliveries uint64
publishes uint64
reactionPublishes uint64
reactionLimiter map[string]*reactionWindow
}
// reactionWindow is one remote's fixed-window rate budget.
type reactionWindow struct {
start time.Time
count int
}
// New returns an unstarted Receiver.
func New(secret, trigger, label, botUser string, publish PublishFunc, log *slog.Logger) *Receiver {
func New(secret, trigger, label, botUser string, publish PublishFunc, reactionPublish ReactionPublishFunc, log *slog.Logger) *Receiver {
if log == nil {
log = slog.New(slog.DiscardHandler)
}
return &Receiver{
secret: secret,
trigger: trigger,
label: label,
botUser: botUser,
publish: publish,
log: log.With("component", "forge-webhook"),
startedAt: time.Now(),
secret: secret,
trigger: trigger,
label: label,
botUser: botUser,
publish: publish,
reactionPublish: reactionPublish,
reactionLimiter: map[string]*reactionWindow{},
log: log.With("component", "forge-webhook"),
startedAt: time.Now(),
}
}
@@ -96,10 +126,11 @@ func New(secret, trigger, label, botUser string, publish PublishFunc, log *slog.
// Publishes counts only what the dispatch predicate turned into work.
// LastReceivedAt is nil until the first authenticated delivery arrives.
type Status struct {
StartedAt time.Time `json:"started_at"`
LastReceivedAt *time.Time `json:"last_received_at"`
Deliveries uint64 `json:"deliveries"`
Publishes uint64 `json:"publishes"`
StartedAt time.Time `json:"started_at"`
LastReceivedAt *time.Time `json:"last_received_at"`
Deliveries uint64 `json:"deliveries"`
Publishes uint64 `json:"publishes"`
ReactionPublishes uint64 `json:"reaction_publishes"`
}
// Status returns a snapshot of the receiver's activity counters.
@@ -107,9 +138,10 @@ func (r *Receiver) Status() Status {
r.mu.Lock()
defer r.mu.Unlock()
s := Status{
StartedAt: r.startedAt,
Deliveries: r.deliveries,
Publishes: r.publishes,
StartedAt: r.startedAt,
Deliveries: r.deliveries,
Publishes: r.publishes,
ReactionPublishes: r.reactionPublishes,
}
if !r.lastReceivedAt.IsZero() {
last := r.lastReceivedAt
@@ -136,6 +168,13 @@ func (r *Receiver) recordPublish() {
r.publishes++
}
// recordReactionPublish marks one delivery as having produced a reaction.
func (r *Receiver) recordReactionPublish() {
r.mu.Lock()
defer r.mu.Unlock()
r.reactionPublishes++
}
// ServeHTTP handles one forge webhook delivery. GitHub only ever POSTs here,
// so any GET is answered with the receiver's Status instead -- an operator
// (or a monitor) can curl the same address the webhook is configured against
@@ -177,14 +216,23 @@ func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// independent of whether it turns out to be dispatch-eligible.
r.recordDelivery()
issueEvent, ok := event.(*github.IssuesEvent)
if !ok {
// Not an issue event (ping, pull_request, etc.). Acknowledge and
// ignore -- this receiver only turns issue activity into work.
switch e := event.(type) {
case *github.IssuesEvent:
r.serveIssueEvent(w, req, e)
case *github.PullRequestReviewEvent:
r.serveReviewEvent(w, req, e)
case *github.PullRequestReviewCommentEvent:
r.serveReviewCommentEvent(w, req, e)
default:
// Not an event this receiver decodes (ping, push, etc.).
// Acknowledge and ignore.
w.WriteHeader(http.StatusAccepted)
return
}
}
// serveIssueEvent is the receiver's original contract: an eligible issue
// event becomes a task envelope on the task path.
func (r *Receiver) serveIssueEvent(w http.ResponseWriter, req *http.Request, issueEvent *github.IssuesEvent) {
task, ok := r.taskFromEvent(issueEvent)
if !ok {
// Delivered but not eligible work (PR, closed, unrelated action,
@@ -203,6 +251,103 @@ func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusAccepted)
}
// serveReviewEvent turns a submitted review into a reaction, the webhook
// half of decision 2: the same typed reaction the poller produces, so
// PublishUnique dedups the two sources. Only "submitted" carries a verdict
// or summary to remediate; edited and dismissed restate or withdraw one.
func (r *Receiver) serveReviewEvent(w http.ResponseWriter, req *http.Request, e *github.PullRequestReviewEvent) {
if e.GetAction() != "submitted" || e.GetReview() == nil {
w.WriteHeader(http.StatusAccepted)
return
}
if !r.allowReactionDelivery(req.RemoteAddr) {
r.log.Warn("reaction rate limit exceeded", "remote", req.RemoteAddr)
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
if r.reactionPublish == nil {
w.WriteHeader(http.StatusAccepted)
return
}
review := e.GetReview()
reaction := workintake.ReviewCommentEnvelope{
Owner: e.GetRepo().GetOwner().GetLogin(),
Repo: e.GetRepo().GetName(),
PRNumber: e.GetPullRequest().GetNumber(),
Kind: workintake.ReviewReactionReview,
ReviewID: review.GetID(),
Author: review.GetUser().GetLogin(),
State: review.GetState(),
Body: review.GetBody(),
}
r.deliverReaction(w, req, reaction)
}
// serveReviewCommentEvent publishes an inline comment as a reaction. The
// comment carries its parent review's ID, so the consumer can collect it
// into that review's unit (decision 5) rather than starting a round of its
// own.
func (r *Receiver) serveReviewCommentEvent(w http.ResponseWriter, req *http.Request, e *github.PullRequestReviewCommentEvent) {
if e.GetAction() != "created" || e.GetComment() == nil {
w.WriteHeader(http.StatusAccepted)
return
}
if !r.allowReactionDelivery(req.RemoteAddr) {
r.log.Warn("reaction rate limit exceeded", "remote", req.RemoteAddr)
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
if r.reactionPublish == nil {
w.WriteHeader(http.StatusAccepted)
return
}
comment := e.GetComment()
reaction := workintake.ReviewCommentEnvelope{
Owner: e.GetRepo().GetOwner().GetLogin(),
Repo: e.GetRepo().GetName(),
PRNumber: e.GetPullRequest().GetNumber(),
Kind: workintake.ReviewReactionComment,
ReviewID: comment.GetPullRequestReviewID(),
CommentID: comment.GetID(),
Author: comment.GetUser().GetLogin(),
Body: comment.GetBody(),
Path: comment.GetPath(),
Line: comment.GetLine(),
}
r.deliverReaction(w, req, reaction)
}
func (r *Receiver) deliverReaction(w http.ResponseWriter, req *http.Request, reaction workintake.ReviewCommentEnvelope) {
if err := r.reactionPublish(req.Context(), reaction); err != nil {
r.log.Error("publish reaction", "key", reaction.IdempotencyKey(), "err", err)
http.Error(w, "publish failed", http.StatusInternalServerError)
return
}
r.recordReactionPublish()
w.WriteHeader(http.StatusAccepted)
}
// allowReactionDelivery consumes one reaction slot for the remote source.
// A fixed window keeps the bookkeeping trivial; the failure mode is a burst
// slightly over one window, which the consumer's dedup and round cap bound.
func (r *Receiver) allowReactionDelivery(remote string) bool {
now := time.Now()
r.mu.Lock()
defer r.mu.Unlock()
for k, v := range r.reactionLimiter {
if now.Sub(v.start) > reactionRateBucketTTL {
delete(r.reactionLimiter, k)
}
}
w, ok := r.reactionLimiter[remote]
if !ok || now.Sub(w.start) > reactionRateWindow {
r.reactionLimiter[remote] = &reactionWindow{start: now, count: 0}
w = r.reactionLimiter[remote]
}
w.count++
return w.count <= reactionRateLimit
}
// taskFromEvent decodes a GitHub issues event into a task envelope, or
// reports (zero, false) when the event is not eligible work.
func (r *Receiver) taskFromEvent(event *github.IssuesEvent) (workintake.TaskEnvelope, bool) {
+5 -5
View File
@@ -207,7 +207,7 @@ func TestReceiverPublishesMatchingIssue(t *testing.T) {
// The receiver always trusts testSecret; tc.secret is what the
// request is signed with, so a case can sign with the wrong (or no)
// secret to exercise rejection.
r := New(testSecret, tc.trigger, tc.cfgLabel, testBot, nil, nil)
r := New(testSecret, tc.trigger, tc.cfgLabel, testBot, nil, nil, nil)
rec, published := serve(t, r, signedRequest(t, tc.secret, tc.eventType, payload))
if rec.Code != tc.wantStatus {
@@ -228,7 +228,7 @@ func TestPublishedEnvelopeCarriesIssueIdentityAndKind(t *testing.T) {
if err != nil {
t.Fatalf("marshal event: %v", err)
}
r := New(testSecret, "label", testLabel, testBot, nil, nil)
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
_, published := serve(t, r, signedRequest(t, testSecret, "issues", payload))
if len(published) != 1 {
@@ -259,7 +259,7 @@ func TestPublishedEnvelopeCarriesIssueIdentityAndKind(t *testing.T) {
// the endpoint with a bad signature manufacture a false "receiver is alive
// and busy" signal.
func TestReceiverStatusTracksAuthenticatedDeliveries(t *testing.T) {
r := New(testSecret, "label", testLabel, testBot, nil, nil)
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
before := r.Status()
if before.Deliveries != 0 || before.Publishes != 0 {
@@ -327,7 +327,7 @@ func TestReceiverStatusTracksAuthenticatedDeliveries(t *testing.T) {
// payload really was dispatch-eligible) but must not count as a publish --
// otherwise Status would report work that never actually reached the queue.
func TestReceiverStatusDoesNotCountFailedPublish(t *testing.T) {
r := New(testSecret, "label", testLabel, testBot, nil, nil)
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
r.publish = func(context.Context, workintake.TaskEnvelope) error {
return errors.New("publish unavailable")
}
@@ -358,7 +358,7 @@ func TestReceiverStatusDoesNotCountFailedPublish(t *testing.T) {
// separate health-check subsystem: any GET is answered with the JSON status
// body, since GitHub only ever POSTs to this receiver.
func TestReceiverServesStatusOnGET(t *testing.T) {
r := New(testSecret, "label", testLabel, testBot, nil, nil)
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
ev := issueEvent(t, "labeled", func(ev *github.IssuesEvent) {
ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}}
})
+1 -1
View File
@@ -144,7 +144,7 @@ func (c *Client) UpdateReviewPayload(ctx context.Context, taskID int64, payload
return unmapError(err)
}
func (c *Client) SetReviewCursors(ctx context.Context, taskID int64, reviewCursor, commentCursor int64) error {
func (c *Client) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
_, err := c.client.SetReviewCursors(ctx, &pb.SetReviewCursorsRequest{TaskId: taskID, ReviewCursor: reviewCursor, CommentCursor: commentCursor})
return unmapError(err)
}
@@ -89,6 +89,7 @@ func taskValue(t *pb.Task) *task.Task { //nolint:dupl // see taskProto above
BindingID: t.BindingId, BindingVersion: int(t.BindingVersion),
CreatedAt: timeValue(t.CreatedAt), UpdatedAt: timeValue(t.UpdatedAt),
ReviewPayload: t.ReviewPayload,
ReviewCursor: t.ReviewCursor,
WorkflowDefinitionVersion: t.WorkflowDefinitionVersion,
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
WorkflowDefinitionYAML: t.WorkflowDefinitionYaml,
+1 -1
View File
@@ -591,7 +591,7 @@ func (s *Store) UpdateReviewPayload(ctx context.Context, taskID int64, payload s
// The pr_open guard keeps a cursor from moving while a remediation run owns
// the task: the run's next round re-reads the cursors the scan wrote, and a
// write under a running task would desync them from what was consumed.
func (s *Store) SetReviewCursors(ctx context.Context, taskID int64, reviewCursor, commentCursor int64) error {
func (s *Store) SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error {
res, err := s.db.ExecContext(ctx, `
UPDATE tasks SET review_cursor=?, watch_comment_id=?, updated_at=datetime('now')
WHERE id=? AND status=?`,