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-15064-pending-ledger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fast delegated decisions now settle their operator-action ledger entries reliably instead of leaving the activity monitor badge pending forever ([#15064](https://github.com/hivecommons/hive/issues/15064)).
9 changes: 8 additions & 1 deletion src/pkg/dashboard/api_repo_item_decide_delegated.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dashboard

import (
Expand Down Expand Up @@ -270,7 +270,14 @@
s.runDelegatedDecision(ctx, key, repoFull, number, itemType, user, mergerRole, body)
}
if delegatedDecisionRunAsync {
go run()
// The response wrapper records the pending action after this handler
// returns. Starting sooner lets a fast job settle a nonexistent row.
start := func() { go run() }
if recorder, ok := w.(*operatorActionWriter); ok {
recorder.afterRecord = start
} else {
start()
}
w.WriteHeader(http.StatusAccepted)
jsonResponse(w, snapshot)
return
Expand Down
34 changes: 34 additions & 0 deletions src/pkg/dashboard/api_repo_item_decide_delegated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,37 @@ func TestDelegatedDecisionReadyForHumanMergeAnswersWithMergeOptions(t *testing.T
t.Errorf("merge-now for a writer = %+v", o)
}
}

// All nested PR jobs already running is a fast finish with no GitHub calls.
// The async parent still needs a settled ledger entry rather than an orphaned
// pending entry (hivecommons/hive#15064).
func TestDelegatedAsyncFanOutSettlesOperatorAction(t *testing.T) {
srv := newDelegatedServer(t, &repoItemDecisionMock{})
delegatedDecisionRunAsync = true
srv.operatorActionStore = newOperatorActionStore("")
for _, number := range []int{17, 20} {
key := delegatedJobKey("testorg/testrepo", number)
srv.delegatedJobs.start(key, &delegatedDecisionJob{Status: delegatedJobStatusRunning}, time.Now())
}
w := postDelegated(t, srv, 7, "writer", `{"type":"issue","prs":[17,20]}`, nil)
if w.Code != http.StatusAccepted {
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
}
deadline := time.After(5 * time.Second)
tick := time.NewTicker(time.Millisecond)
defer tick.Stop()
for {
got := srv.operatorActions().List("testorg/testrepo", 7)
if len(got) == 1 && got[0].Outcome == operatorActionOutcomeFailed {
if !strings.Contains(got[0].Detail, "already running") {
t.Fatalf("missing failure detail: %+v", got)
}
return
}
select {
case <-deadline:
t.Fatalf("async action never settled: %+v", got)
case <-tick.C:
}
}
}
7 changes: 7 additions & 0 deletions src/pkg/dashboard/operator_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ type operatorActionWriter struct {
http.ResponseWriter
status int
body bytes.Buffer
// afterRecord starts delegated work only after the pending row is written.
afterRecord func()
}

func (w *operatorActionWriter) WriteHeader(code int) {
Expand Down Expand Up @@ -422,6 +424,11 @@ func (s *Server) recordOperatorAction(h http.HandlerFunc) http.HandlerFunc {
endpoint := segs[len(segs)-1]
req, known := operatorActionFromBody(endpoint, body)
cw := &operatorActionWriter{ResponseWriter: w}
defer func() {
if cw.afterRecord != nil {
cw.afterRecord()
}
}()
h(cw, r)
if !known {
return
Expand Down
35 changes: 35 additions & 0 deletions src/pkg/dashboard/operator_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ func TestSettleOperatorActionNoOp(t *testing.T) {
}
}

// A job that settles immediately must see the pending row, including when
// the handler has already returned its 202 response.
func TestOperatorActionRecordedBeforeDelegatedStart(t *testing.T) {
for _, outcome := range []string{operatorActionOutcomeApplied, operatorActionOutcomeNoOp, operatorActionOutcomeFailed} {
t.Run(outcome, func(t *testing.T) {
s := &Server{operatorActionStore: newOperatorActionStore("")}
started := false
h := s.recordOperatorAction(func(w http.ResponseWriter, r *http.Request) {
w.(*operatorActionWriter).afterRecord = func() {
started = true
ok, err := s.operatorActions().Settle("org/repo", 7, operatorActionLetHiveDecide, outcome, "finished immediately")
if !ok || err != nil {
t.Errorf("settle before record: ok=%v err=%v", ok, err)
}
}
w.WriteHeader(http.StatusAccepted)
jsonResponse(w, map[string]string{"status": delegatedJobStatusRunning})
if started {
t.Error("job started before handler returned")
}
})
r := httptest.NewRequest(http.MethodPost, "/api/repos/org/repo/items/7/decide-delegated", strings.NewReader(`{}`))
r.SetPathValue("owner", "org")
r.SetPathValue("repo", "repo")
r.SetPathValue("number", "7")
w := httptest.NewRecorder()
h(w, r)
got := s.operatorActions().List("org/repo", 7)
if w.Code != http.StatusAccepted || !started || len(got) != 1 || got[0].Outcome != outcome || got[0].Detail != "finished immediately" {
t.Fatalf("code=%d started=%v records=%+v", w.Code, started, got)
}
})
}
}

func TestStageTrackerOperatorActionStates(t *testing.T) {
now := time.Date(2026, 10, 28, 16, 0, 0, 0, time.UTC)
window := 30 * time.Minute
Expand Down
Loading