diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 510f06cac..ac553bea4 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -652,6 +652,7 @@ func (b *boot) buildDaemon() { if btc, ok := b.stateStore.(storecontract.BindingTaskCreator); ok { b.d.BindingTaskCreator = btc } + b.d.WithdrawTask = b.taskActions().Withdraw b.d.Access = b.accessChain if b.accessChain != nil { @@ -691,7 +692,7 @@ func (b *boot) setupForgeWebhook() { return } - receiver := forgewebhook.New(secretValue, parser, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, b.d.PublishReaction, log) + receiver := forgewebhook.New(secretValue, parser, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, b.d.PublishReaction, b.withdrawIssue, 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, ReadHeaderTimeout: 5 * time.Second} diff --git a/internal/app/archied/task_actions.go b/internal/app/archied/task_actions.go index c10eed443..44ee21d3a 100644 --- a/internal/app/archied/task_actions.go +++ b/internal/app/archied/task_actions.go @@ -27,6 +27,15 @@ func (b *boot) taskActions() taskactions.Service { ) } +// withdrawIssue declines the task an issue queued, if it has one. +func (b *boot) withdrawIssue(ctx context.Context, owner, repo string, number int, reason string) error { + task, err := b.stateStore.TaskByIssue(ctx, owner, repo, number) + if err != nil || task == nil { + return err + } + return b.taskActions().Withdraw(ctx, task.ID, reason) +} + func (b *boot) cancelTask(id int64) bool { if b.d == nil { return false diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 93c4cef09..d8caef62a 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -198,6 +198,10 @@ type Daemon struct { WorkflowDefinitions interface { WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) } + // WithdrawTask declines a task whose issue a complete poll no longer + // returns. Nil leaves withdrawn issues' work running. + WithdrawTask func(ctx context.Context, taskID int64, reason string) error + // WorkflowEnablement supplies which workflows each org has disabled. A // binding targeting a workflow its org disabled does not dispatch. Nil // means every workflow is enabled. @@ -432,7 +436,10 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) { d.markPoll() cfg := configForIdentity(d.Cfg.Get(), id.Cfg) for _, repo := range id.Repos { - issues := d.pollIssuesWithConfig(ctx, id.Forge, cfg, repo) + issues, complete := d.pollIssuesWithConfig(ctx, id.Forge, cfg, repo) + if complete { + d.withdrawUnpolled(ctx, repo, string(id.ID), issues) + } for _, is := range issues { labels := strings.Join(is.Labels, ",") if d.Tasks != nil { @@ -457,71 +464,61 @@ func (d *Daemon) maintainAndDrain(ctx context.Context) { } } -// pollIssuesWithConfig discovers work for one repo using the given forge -// client and dispatch config. Single-identity mode passes d.Forge/d.Cfg; -// identity poll loops pass the identity's own forge and configForIdentity, -// so each identity polls with its own bot user, label, and trigger. -func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) []forge.Issue { +// pollIssuesWithConfig returns the repo's issues the dispatch rule makes +// work, using the given forge client and dispatch config: single-identity +// mode passes d.Forge/d.Cfg, identity poll loops their own forge and +// configForIdentity, so each identity polls with its own bot user, label and +// trigger. complete is false when any part of the poll failed or was skipped, so +// the result must not be read as the whole eligible set. +func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) (issues []forge.Issue, complete bool) { // Webhook intake hears about issues as they change; polling as well would // spend forge API quota to find the same work. The receiver runs from the // root forge settings, so their mode decides for every identity. if d.Cfg.Get().Forge.Intake == config.ForgeIntakeWebhook { - return nil + return nil, false } - switch cfg.Dispatch.Trigger { - case "label": + labelled := func() ([]forge.Issue, bool) { // Empty-label rule shared with workintake.MatchesDispatch: a label // trigger needs a non-empty label or it would match every open issue. - if workintake.RequiresLabel(cfg.Dispatch.Trigger) && cfg.Label == "" { + if cfg.Label == "" { d.Log.Error("label trigger configured with an empty label; refusing to poll (an empty label matches every open issue)", "repo", repo.FullName()) - return nil + return nil, false } issues, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, cfg.Label) if err != nil { d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err) - return nil + return nil, false } - return issues - case "either": - return d.pollEitherWithConfig(ctx, fg, cfg, repo) - default: // "assignee" (default) + return issues, true + } + assigned := func() ([]forge.Issue, bool) { issues, err := fg.AssignedIssues(ctx, repo.Owner, repo.Name, cfg.BotUser) if err != nil { - d.Log.Error("poll failed", "repo", repo.FullName(), "err", err) - return nil + d.Log.Error("assigned poll failed", "repo", repo.FullName(), "err", err) + return nil, false } - return issues + return issues, true } -} - -func (d *Daemon) pollEitherWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) []forge.Issue { - seen := map[int]bool{} - var out []forge.Issue - - assigned, err := fg.AssignedIssues(ctx, repo.Owner, repo.Name, cfg.BotUser) - if err != nil { - d.Log.Error("assigned poll failed", "repo", repo.FullName(), "err", err) - } else { - for _, is := range assigned { + switch cfg.Dispatch.Trigger { + case "label": + return labelled() + case "either": + byAssignee, assignedOK := assigned() + byLabel, labelledOK := labelled() + seen := map[int]bool{} + for _, is := range byAssignee { seen[is.Number] = true - out = append(out, is) } - } - if workintake.RequiresLabel(cfg.Dispatch.Trigger) && cfg.Label == "" { - d.Log.Error("either trigger configured with an empty label; skipping the label poll (an empty label matches every open issue)", "repo", repo.FullName()) - return out - } - labelled, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, cfg.Label) - if err != nil { - d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err) - } else { - for _, is := range labelled { + issues = byAssignee + for _, is := range byLabel { if !seen[is.Number] { - out = append(out, is) + issues = append(issues, is) } } + return issues, assignedOK && labelledOK + default: // "assignee" (default) + return assigned() } - return out } // Cycle is one poll-and-drain pass: enqueue newly assigned issues, @@ -1065,7 +1062,10 @@ func (d *Daemon) poll(ctx context.Context) { } d.markPoll() for _, repo := range d.Cfg.Get().Repos { - issues := d.pollIssues(ctx, repo) + issues, complete := d.pollIssues(ctx, repo) + if complete { + d.withdrawUnpolled(ctx, repo, "", issues) + } for _, is := range issues { labels := strings.Join(is.Labels, ",") if d.Tasks != nil { @@ -1075,6 +1075,35 @@ func (d *Daemon) poll(ctx context.Context) { } } +// withdrawUnpolled declines the queued or running work of an identity's +// issues in repo that a complete poll no longer returns: the issue was +// closed, unlabelled or unassigned. +func (d *Daemon) withdrawUnpolled(ctx context.Context, repo config.Repo, identity string, polled []forge.Issue) { + if d.WithdrawTask == nil { + return + } + eligible := make(map[int]bool, len(polled)) + for _, is := range polled { + eligible[is.Number] = true + } + active, err := d.Store.TasksPage(ctx, storecontract.TaskPage{ + Statuses: []string{taskstate.Queued, taskstate.Running}, Limit: 500, + }) + if err != nil { + d.Log.Warn("withdrawal check: active tasks unavailable", "repo", repo.FullName(), "err", err) + return + } + for _, t := range active { + if t.Owner != repo.Owner || t.Repo != repo.Name || t.Identity != identity || + !t.IsForgeBacked() || t.PRNumber > 0 || eligible[t.IssueNumber] { + continue + } + if err := d.WithdrawTask(ctx, t.ID, "the issue is no longer assigned, labelled or open"); err != nil { + d.Log.Warn("withdraw task", "task", t.ID, "err", err) + } + } +} + // pollNATS publishes discovered issues to NATS (new flow). func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, is forge.Issue, labels, identity string) { // Read-only existence check prevents rediscovering an existing task. @@ -1257,7 +1286,7 @@ func (d *Daemon) processNATSTask(ctx context.Context, msg eventbus.Message) { // pollIssues discovers work for one repo according to the root dispatch // config, using the root forge client. Identity poll loops use // pollIssuesWithConfig with their own forge and config instead. -func (d *Daemon) pollIssues(ctx context.Context, repo config.Repo) []forge.Issue { +func (d *Daemon) pollIssues(ctx context.Context, repo config.Repo) ([]forge.Issue, bool) { return d.pollIssuesWithConfig(ctx, d.Forge, d.Cfg.Get(), repo) } diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index fa953cdb6..ee11254d7 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -239,6 +239,30 @@ func (s Service) Apply(ctx context.Context, scope *string, actor Actor, id int64 return nil } +// Withdraw declines a queued or running task whose issue was withdrawn on +// the forge -- closed, unlabelled or unassigned -- cancelling its run. The +// issue is left as the person who withdrew it left it. A task in any other +// state, or one with a pull request open, is not withdrawn: its work is +// already in review or finished. +func (s Service) Withdraw(ctx context.Context, id int64, reason string) error { + task, err := s.Store.TaskByID(ctx, id) + if err != nil || task == nil { + return err + } + if (task.Status != taskstate.Queued && task.Status != taskstate.Running) || task.PRNumber > 0 { + return nil + } + event := s.attributed(task, Actor{}) + event.Kind, event.Detail = events.KindTaskWithdrawn, "issue withdrawn: "+reason + event.Data = map[string]any{"reason": reason} + if _, err := s.Store.CancelExecution(ctx, task.ID, event.Detail, declined); err != nil { + return err + } + s.deliver(task.ID) + s.emit(ctx, event) + return nil +} + // The statuses the cancel path records, taken from the lifecycle's action table // so the service cannot drift from the transitions it offers. var ( diff --git a/internal/events/events.go b/internal/events/events.go index 4a8e160ac..c79d73012 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -53,6 +53,9 @@ const ( KindTaskCancelled = "task_cancelled" KindTaskStopped = "task_stopped" KindTaskAbandoned = "task_abandoned" + // KindTaskWithdrawn records work declined because its issue was closed, + // unlabelled or unassigned. data: reason. + KindTaskWithdrawn = "task_withdrawn" KindTaskArchiveRequested = "task_archive_requested" KindWorkRequestSubmitted = "work_request_submitted" KindLog = "log" // data: level, msg diff --git a/internal/forge/webhook/receiver.go b/internal/forge/webhook/receiver.go index 91b194aee..8be5afc92 100644 --- a/internal/forge/webhook/receiver.go +++ b/internal/forge/webhook/receiver.go @@ -28,6 +28,9 @@ 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 +// WithdrawFunc declines the work an issue no longer asks for. +type WithdrawFunc func(ctx context.Context, owner, repo string, number int, reason string) error + // ReactionPublishFunc publishes one review reaction. type ReactionPublishFunc func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error @@ -52,6 +55,7 @@ type Receiver struct { botUser string publish PublishFunc reactionPublish ReactionPublishFunc + withdraw WithdrawFunc log *slog.Logger mu sync.Mutex @@ -71,7 +75,7 @@ type reactionWindow struct { } // New returns an unstarted Receiver. -func New(secret string, parser forge.WebhookParser, trigger, label, botUser string, publish PublishFunc, reactionPublish ReactionPublishFunc, log *slog.Logger) *Receiver { +func New(secret string, parser forge.WebhookParser, trigger, label, botUser string, publish PublishFunc, reactionPublish ReactionPublishFunc, withdraw WithdrawFunc, log *slog.Logger) *Receiver { if log == nil { log = slog.New(slog.DiscardHandler) } @@ -83,6 +87,7 @@ func New(secret string, parser forge.WebhookParser, trigger, label, botUser stri botUser: botUser, publish: publish, reactionPublish: reactionPublish, + withdraw: withdraw, reactionLimiter: map[string]*reactionWindow{}, log: log.With("component", "forge-webhook"), startedAt: time.Now(), @@ -211,7 +216,15 @@ func (r *Receiver) serveIssueEvent(w http.ResponseWriter, req *http.Request, iss task, ok := r.taskFromEvent(issueEvent) if !ok { // Delivered but not eligible work (PR, closed, unrelated action, - // dispatch mismatch). Acknowledge without publishing. + // dispatch mismatch). A withdrawal declines the work it queued; + // anything else is acknowledged without publishing. + if reason, withdrawn := r.withdrawal(issueEvent); withdrawn && r.withdraw != nil { + if err := r.withdraw(req.Context(), issueEvent.Owner, issueEvent.Repo, issueEvent.Number, reason); err != nil { + r.log.Error("withdraw task", "repo", issueEvent.Owner+"/"+issueEvent.Repo, "issue", issueEvent.Number, "err", err) + http.Error(w, "withdraw failed", http.StatusInternalServerError) + return + } + } w.WriteHeader(http.StatusAccepted) return } @@ -321,6 +334,23 @@ func (r *Receiver) allowReactionDelivery(remote string) bool { return w.count <= reactionRateLimit } +// withdrawal reports whether an issues event takes an issue out of dispatch: +// it was closed, or lost the label or assignee that made it work. +func (r *Receiver) withdrawal(event *forge.IssueEvent) (string, bool) { + if event.PullRequest || event.Owner == "" || event.Repo == "" { + return "", false + } + switch event.Action { + case "closed": + return "the issue was closed", true + case "unlabeled", "unassigned": + if !workintake.MatchesDispatch(r.trigger, r.label, r.botUser, event.Labels, event.Assignees) { + return "the issue was " + event.Action, true + } + } + return "", false +} + // taskFromEvent reports the task an issues event makes eligible, or // (zero, false) when it is not eligible work. func (r *Receiver) taskFromEvent(event *forge.IssueEvent) (workintake.TaskEnvelope, bool) { diff --git a/internal/forge/webhook/receiver_test.go b/internal/forge/webhook/receiver_test.go new file mode 100644 index 000000000..f3966720c --- /dev/null +++ b/internal/forge/webhook/receiver_test.go @@ -0,0 +1,34 @@ +package webhook + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/forge" +) + +// An issue leaves dispatch when it is closed or loses what made it work; an +// unlabel or unassign that still leaves it eligible withdraws nothing. +func TestWithdrawal(t *testing.T) { + r := New("", nil, "either", "archie", "bot", nil, nil, nil, nil) + tests := []struct { + name string + event forge.IssueEvent + want bool + }{ + {"closed", forge.IssueEvent{Action: "closed", State: "closed"}, true}, + {"label removed, still assigned", forge.IssueEvent{Action: "unlabeled", Assignees: []string{"bot"}}, false}, + {"label removed, nothing left", forge.IssueEvent{Action: "unlabeled"}, true}, + {"unassigned, still labelled", forge.IssueEvent{Action: "unassigned", Labels: []string{"archie"}}, false}, + {"unassigned, nothing left", forge.IssueEvent{Action: "unassigned"}, true}, + {"edited", forge.IssueEvent{Action: "edited"}, false}, + {"a pull request closing", forge.IssueEvent{Action: "closed", PullRequest: true}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.event.Owner, tt.event.Repo = "acme", "widget" + if _, got := r.withdrawal(&tt.event); got != tt.want { + t.Fatalf("withdrawal = %v, want %v", got, tt.want) + } + }) + } +}