diff --git a/changelog.d/fixed-15064-pending-ledger.md b/changelog.d/fixed-15064-pending-ledger.md new file mode 100644 index 0000000000..cff79e158a --- /dev/null +++ b/changelog.d/fixed-15064-pending-ledger.md @@ -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)). diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated.go b/src/pkg/dashboard/api_repo_item_decide_delegated.go index 0849a80d2c..5c6a51c237 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated.go @@ -270,7 +270,14 @@ func (s *Server) handleRepoItemDecideDelegated(w http.ResponseWriter, r *http.Re 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 diff --git a/src/pkg/dashboard/api_repo_item_decide_delegated_test.go b/src/pkg/dashboard/api_repo_item_decide_delegated_test.go index f8b66d8940..f272f9ba05 100644 --- a/src/pkg/dashboard/api_repo_item_decide_delegated_test.go +++ b/src/pkg/dashboard/api_repo_item_decide_delegated_test.go @@ -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: + } + } +} diff --git a/src/pkg/dashboard/operator_actions.go b/src/pkg/dashboard/operator_actions.go index 96a3426462..1921f3a173 100644 --- a/src/pkg/dashboard/operator_actions.go +++ b/src/pkg/dashboard/operator_actions.go @@ -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) { @@ -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 diff --git a/src/pkg/dashboard/operator_actions_test.go b/src/pkg/dashboard/operator_actions_test.go index cb6018bec4..f01cabe938 100644 --- a/src/pkg/dashboard/operator_actions_test.go +++ b/src/pkg/dashboard/operator_actions_test.go @@ -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