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-14705-review-verdict-wake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Recording a review head's dispatched verdict now wakes the governor for an early cached-snapshot eval cycle, so the next head or the decision kick is dispatched right away instead of waiting for an unrelated webhook or `governor.eval_interval_webhook_s`; a burst of verdicts is coalesced to at most one wake per `governor.webhook_wake_min_gap_s` ([#14705](https://github.com/hivecommons/hive/issues/14705)).
4 changes: 4 additions & 0 deletions src/cmd/hive/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ type boot struct {
// carries its wakes to the governor loop (hivecommons/hive#11091).
reviewEvents *eventdispatch.Dispatcher
reviewWake chan struct{}
// verdictWaker turns dispatched review verdicts the relay records into
// early eval cycles, at most one per governor.webhook_wake_min_gap_s
// (hivecommons/hive#14705).
verdictWaker *webhookWaker

// webhookWaker coalesces cache-invalidating GitHub webhooks into early
// eval cycles and evalLoop is the pacing block /api/status reports.
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/hive/github_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ func (b *boot) applyGitHubClientDashboardHooks(client *github.Client) {
// claims ledger is on.
recordStarted(b.dashSrv, b.cfg.Project.Org, agentName, repo, issue, signal)
})
// hivecommons/hive#14705: a recorded dispatched verdict frees its head,
// so the governor re-plans now instead of at the next eval cycle.
client.SetReviewVerdictHook(b.wakeForReviewVerdict)
// PR follow-up session resume (#9583, default off): remember which live
// CLI session authored this PR, and the PR's handoff note, so its
// follow-ups can resume that conversation or brief a fresh one. The
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6369,6 +6369,13 @@ func (b *boot) runLoopWith(deps runLoopDeps) {
tick(evalSourceCached)
armContinuousWake()
armWebhookHealthRecheck()
case batch := <-b.verdictWakeChan():
b.logger.Info("review verdict wake: running an early eval cycle from the cached snapshot",
"pr", batch.Reason, "last_pr", batch.LastRe, "verdicts", batch.Count,
"coalesced", batch.Coalesced.Round(time.Millisecond).String())
tick(evalSourceCached)
armContinuousWake()
armWebhookHealthRecheck()
case batch := <-b.webhookWakeChan():
now := healthNow()
// Apply the delivery's targeted re-reads first so this cycle
Expand Down
31 changes: 31 additions & 0 deletions src/cmd/hive/review_event_dispatch.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"sort"
"sync/atomic"
"time"

"github.com/hivecommons/hive/pkg/review"
"github.com/hivecommons/hive/pkg/review/eventdispatch"
Expand All @@ -22,6 +24,12 @@
if b.reviewWake == nil {
b.reviewWake = make(chan struct{}, 1)
}
if b.verdictWaker == nil {
b.verdictWaker = newWebhookWaker(
func() time.Duration { return 0 },
func() time.Duration { return b.cfg.WebhookWakeMinGap() },
)
}
activeReviewEvents.Store(b.reviewEvents)
}

Expand All @@ -48,6 +56,29 @@
}
}

// wakeForReviewVerdict is the github.ReviewVerdictHook: the relay recorded a
// verdict bound to a review dispatch, so the head that wrote it is free and
// the PR may be ready for its next head or its decision kick. Without this
// the next kick waited for an unrelated webhook or the eval cadence
// (hivecommons/hive#14705). The first verdict wakes the loop at once; later
// ones inside governor.webhook_wake_min_gap_s fold into one wake at the end
// of the gap, so a burst of verdicts costs one cycle.
func (b *boot) wakeForReviewVerdict(repo string, number int) {
if b == nil || b.verdictWaker == nil {
return
}
b.verdictWaker.Request(fmt.Sprintf("%s#%d", repo, number))
}

// verdictWakeChan is the loop's receive side; nil when the waker is not
// installed so the select case never fires.
func (b *boot) verdictWakeChan() <-chan webhookWakeBatch {
if b.verdictWaker == nil {
return nil
}
return b.verdictWaker.out
}

// prioritizeEventPRs moves PRs whose current head a review webhook fired for
// to the front, keeping order otherwise. PlanDispatch walks the list in order
// and still applies every gate, so this changes who is first, not who is
Expand Down
72 changes: 71 additions & 1 deletion src/cmd/hive/review_event_dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ func TestInstallReviewEventsIsIdempotent(t *testing.T) {
if d == nil || wake == nil || activeReviewEvents.Load() != d {
t.Fatal("dispatcher not installed")
}
verdicts := b.verdictWaker
if verdicts == nil {
t.Fatal("verdict waker not installed")
}
b.installReviewEvents()
if b.reviewEvents != d || b.reviewWake != wake {
if b.reviewEvents != d || b.reviewWake != wake || b.verdictWaker != verdicts {
t.Fatal("reinstall replaced the dispatcher")
}
t.Cleanup(func() { activeReviewEvents.Store(nil) })
Expand Down Expand Up @@ -140,3 +144,69 @@ func TestRunLoopWithRunsAnEarlyCycleOnReviewWake(t *testing.T) {
t.Fatalf("log = %s", log.String())
}
}

// hivecommons/hive#14705: the first recorded verdict wakes the loop at once;
// the verdicts that follow inside governor.webhook_wake_min_gap_s fold into
// one wake at the end of the gap.
func TestWakeForReviewVerdictCoalescesInsideMinGap(t *testing.T) {
w, f := newTestWebhookWaker(0, 30*time.Second)
b := &boot{verdictWaker: w}

b.wakeForReviewVerdict("o/r", 1)
if len(f.delays) != 1 || f.delays[0] != 0 {
t.Fatalf("first verdict should wake at once, armed %v", f.delays)
}
f.fire(t)
if got := <-b.verdictWakeChan(); got.Count != 1 || got.Reason != "o/r#1" {
t.Fatalf("first wake = %+v", got)
}

f.now = f.now.Add(5 * time.Second)
b.wakeForReviewVerdict("o/r", 2)
b.wakeForReviewVerdict("o/r", 3)
if len(f.delays) != 2 || f.delays[1] != 25*time.Second {
t.Fatalf("verdicts inside the gap should arm one timer for the rest of it, armed %v", f.delays)
}
select {
case got := <-b.verdictWakeChan():
t.Fatalf("woke inside the min gap: %+v", got)
default:
}
f.fire(t)
if got := <-b.verdictWakeChan(); got.Count != 2 || got.Reason != "o/r#2" || got.LastRe != "o/r#3" {
t.Fatalf("coalesced wake = %+v", got)
}
}

func TestWakeForReviewVerdictWithoutWakerIsNoop(t *testing.T) {
var nilBoot *boot
nilBoot.wakeForReviewVerdict("o/r", 1)
b := &boot{}
b.wakeForReviewVerdict("o/r", 1)
if b.verdictWakeChan() != nil {
t.Fatal("uninstalled waker should leave the loop case nil")
}
}

func TestRunLoopWithRunsAnEarlyCycleOnVerdictWake(t *testing.T) {
b, log, cancel := newLoopBoot(t, 60, 0)
b.verdictWaker, _ = newTestWebhookWaker(0, 30*time.Second)
gov := newFakeTicker()
h := &loopHarness{startupOK: true, pending: []*fakeTicker{gov}, persisted: make(chan struct{}, 8)}
done := runLoopAsync(b, h.deps())
<-h.persisted // first cycle done

b.verdictWaker.out <- webhookWakeBatch{Reason: "o/r#1", LastRe: "o/r#2", Count: 2}
<-h.persisted // early cycle done
gov.fire()
<-h.persisted // cadence cycle still runs
cancel()
waitDone(t, done)

if len(h.evals) != 3 {
t.Fatalf("evals = %d, want first + verdict + cadence", len(h.evals))
}
if !strings.Contains(log.String(), "review verdict wake") || !strings.Contains(log.String(), "verdicts=2") {
t.Fatalf("log = %s", log.String())
}
}
11 changes: 11 additions & 0 deletions src/docs/operator-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,17 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode.
`review_requested` deliveries also queue an early review dispatch when
`review.event_driven` is on; see
[Event-driven dispatch](review-swarm.md#event-driven-dispatch).
- **A recorded review verdict also wakes the governor loop.** When the
relay records a verdict bound to a review dispatch, the head that wrote it
is free and the PR may be ready for its next head or its decision kick, so
the loop runs a cached-snapshot cycle now instead of waiting for an
unrelated delivery or `governor.eval_interval_webhook_s`. Advisory verdicts
do not wake it. The first verdict wakes at once; later ones inside
`governor.webhook_wake_min_gap_s` fold into one wake at the end of the gap.
`governor.webhook_wake_debounce_s` does not apply to this wake (its
debounce is fixed at 0, so a verdict is never held back), and the spoke logs `review verdict wake: running an early eval cycle from
the cached snapshot`
([#14705](https://github.com/hivecommons/hive/issues/14705)).
- Every status publish (cadence or wake) invalidates the `/api/stage-tracker`
cache and sends a `stage-tracker` event on `/api/events`, so the dashboard's
activity monitor refetches at once instead of waiting out
Expand Down
2 changes: 1 addition & 1 deletion src/docs/scale-envelope.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ ones to:
2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.**
This holds today and is easy to assume backwards:
- Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied
(`RankActionableIssues`, `src/pkg/github/client.go:4676-4693`, descending `AgeMinutes`).
(`RankActionableIssues`, `src/pkg/github/client.go:4679-4696`, descending `AgeMinutes`).
- PRs are sorted by **review class then oldest-first within class**
(`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`;
fixes β†’ refactors/docs β†’ tests).
Expand Down
2 changes: 1 addition & 1 deletion src/docs/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ These are the acceptance criteria of #13373 and the tests that hold them.
| `governor.eval_interval_webhook_s` | `900` | the base tick in webhook mode; `0` disables webhook mode (always poll) |
| `governor.eval_interval_max_s` | `1800` | cap on either tick after the API-budget stretch |
| `governor.webhook_wake_debounce_s` | `10` | a delivery and its check fan-out coalesce into one wake cycle |
| `governor.webhook_wake_min_gap_s` | `30` | minimum spacing between webhook-triggered cycles |
| `governor.webhook_wake_min_gap_s` | `30` | minimum spacing between webhook-triggered cycles, and between the cycles recorded review verdicts trigger |
| `forgestate.reconcile_interval_s` | `900` | ForgeState's own full-list interval per repo, shortened while drift is high |
| `forgestate.freshness_bound_s` | `60` | maximum item age before merge/close/claim/kick re-reads it |
| `forgestate.drift_threshold` | `0.05` | fraction of items a reconcile may find wrong before the interval shortens |
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ type Client struct {
// agentStartHook is told when an agent's own relay request shows it
// working an issue (#10527). See agent_start_hook.go.
agentStartHook atomic.Pointer[AgentStartHook]
// reviewVerdictHook is told when the relay records a verdict bound to a
// review dispatch (review_verdict_hook.go).
reviewVerdictHook atomic.Pointer[ReviewVerdictHook]
// mergeAuthz gates merge requests from the merge-request watcher against the
// per-agent ACMM merge-policy (CanMerge) + forge-resistance AND the merge
// TARGET (pinned SHA + governor merge-eligible membership; see
Expand Down
33 changes: 33 additions & 0 deletions src/pkg/github/review_verdict_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package github

// ReviewVerdictHook is told that the relay recorded at least one verdict
// bound to a review dispatch for PR number in repo. The head that wrote it
// is free again, and the PR may now be ready for its next head or its
// decision kick, so the hive uses it to wake the governor loop rather than
// leave the reviewers idle until the next eval cycle
// (hivecommons/hive#14705).
type ReviewVerdictHook func(repo string, number int)

// SetReviewVerdictHook installs (or with nil, removes) the verdict hook.
// Safe to call before or after the watchers start.
func (c *Client) SetReviewVerdictHook(fn ReviewVerdictHook) {
if c == nil {
return
}
if fn == nil {
c.reviewVerdictHook.Store(nil)
return
}
c.reviewVerdictHook.Store(&fn)
}

// notifyReviewVerdict reports a recorded dispatched verdict to the installed
// hook, if any.
func (c *Client) notifyReviewVerdict(repo string, number int) {
if c == nil {
return
}
if hook := c.reviewVerdictHook.Load(); hook != nil && *hook != nil {
(*hook)(repo, number)
}
}
94 changes: 94 additions & 0 deletions src/pkg/github/review_verdict_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package github

import (
"context"
"strings"
"testing"

"github.com/hivecommons/hive/pkg/review"
)

type verdictHookCall struct {
repo string
number int
}

func recordingVerdictHook(c *Client) *[]verdictHookCall {
var calls []verdictHookCall
c.SetReviewVerdictHook(func(repo string, number int) {
calls = append(calls, verdictHookCall{repo, number})
})
return &calls
}

// A recorded dispatched verdict frees its head, so the hook fires: once per
// request, even when a combined review answered several perspectives
// (hivecommons/hive#14705).
func TestRecordReviewVerdictCallsHookOnceForDispatchedVerdicts(t *testing.T) {
c := &Client{logger: testLogger()}
calls := recordingVerdictHook(c)
withVerdictDispatchState(t, review.DispatchState{Pending: []review.PendingReview{
pendingVerdict("o/r", 7, "correctness", "reviewer"),
pendingVerdict("o/r", 7, "security", "reviewer"),
}})
raw := "[" + strings.Join([]string{
validVerdictJSON(t, "o/r", 7, "correctness", "approve"),
validVerdictJSON(t, "o/r", 7, "security", "approve"),
}, ",") + "]"

c.recordReviewVerdict(context.Background(), ReviewRequest{Repo: "o/r", Number: 7, Agent: "reviewer", Report: raw}, t.TempDir())

if len(*calls) != 1 || (*calls)[0] != (verdictHookCall{"o/r", 7}) {
t.Fatalf("hook calls = %+v, want one for o/r#7", *calls)
}
}

// An advisory verdict binds no dispatch, so no head was freed and nothing
// wakes.
func TestRecordReviewVerdictDoesNotCallHookForAdvisoryVerdict(t *testing.T) {
c := &Client{logger: testLogger()}
calls := recordingVerdictHook(c)
withVerdictDispatchState(t, review.DispatchState{Pending: []review.PendingReview{
pendingVerdict("o/r", 2, review.PerspectiveSecurity, "reviewer"),
}})
raw := validVerdictJSONWithHead(t, "o/r", 1, "security", "approve", "abc123")

got := c.recordReviewVerdict(context.Background(), ReviewRequest{Repo: "o/r", Number: 1, Agent: "reviewer", Report: raw}, t.TempDir())

if len(got) != 1 {
t.Fatalf("advisory verdict was not recorded: %+v", got)
}
if len(*calls) != 0 {
t.Fatalf("advisory verdict called the hook: %+v", *calls)
}
}

// A refused verdict records nothing and wakes nothing.
func TestRecordReviewVerdictDoesNotCallHookForRefusedVerdict(t *testing.T) {
c := &Client{logger: testLogger()}
calls := recordingVerdictHook(c)
withVerdictDispatchState(t, review.DispatchState{})
raw := validVerdictJSON(t, "o/r", 1, "correctness", "approve")

got := c.recordReviewVerdict(context.Background(), ReviewRequest{Repo: "o/r", Number: 1, Agent: "reviewer", Report: raw}, t.TempDir())

if len(got) != 0 {
t.Fatalf("refused verdict was recorded: %+v", got)
}
if len(*calls) != 0 {
t.Fatalf("refused verdict called the hook: %+v", *calls)
}
}

func TestSetReviewVerdictHookNilClears(t *testing.T) {
c := &Client{logger: testLogger()}
calls := recordingVerdictHook(c)
c.SetReviewVerdictHook(nil)
c.notifyReviewVerdict("o/r", 1)
if len(*calls) != 0 {
t.Fatalf("cleared hook still called: %+v", *calls)
}
var nilClient *Client
nilClient.SetReviewVerdictHook(func(string, int) {})
nilClient.notifyReviewVerdict("o/r", 1)
}
Loading
Loading