fix(daemon): withdraw only issues the same dispatch rule found eligible and then lost

This commit is contained in:
sam committed 2026-10-05 22:04:41 +11:00
1 parent 09653025f6
commit 3cac3c859a
2 files changed
+95 -4

No files matched your search

+42 -4
View File
@@ -134,6 +134,12 @@ type Daemon struct {
// reactionConsumer is built lazily by drainReactions and kept for its
// lifetime drop counter.
reactionConsumer *reactionConsumer
// lastPolled remembers each repo's eligible issues from its previous
// complete poll, under the dispatch rule that produced them, so only an
// issue that was eligible and dropped out reads as withdrawn.
lastPolledMu sync.Mutex
lastPolled map[string]polledIssues
// reactionPublisher is built lazily by scanPRReviews; an indirection
// only so tests can swap the delivery without the bus.
reactionPublisher ReactionPublisher
@@ -438,7 +444,7 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) {
for _, repo := range id.Repos {
issues, complete := d.pollIssuesWithConfig(ctx, id.Forge, cfg, repo)
if complete {
d.withdrawUnpolled(ctx, repo, string(id.ID), issues)
d.withdrawUnpolled(ctx, repo, string(id.ID), dispatchRule(cfg), issues)
}
for _, is := range issues {
labels := strings.Join(is.Labels, ",")
@@ -1065,7 +1071,7 @@ func (d *Daemon) poll(ctx context.Context) {
for _, repo := range d.Cfg.Get().Repos {
issues, complete := d.pollIssues(ctx, repo)
if complete {
d.withdrawUnpolled(ctx, repo, "", issues)
d.withdrawUnpolled(ctx, repo, "", dispatchRule(d.Cfg.Get()), issues)
}
for _, is := range issues {
labels := strings.Join(is.Labels, ",")
@@ -1076,10 +1082,34 @@ func (d *Daemon) poll(ctx context.Context) {
}
}
// polledIssues is one repo's eligible issues under one dispatch rule.
type polledIssues struct {
rule string
eligible map[int]bool
}
// rememberPoll records a repo's latest eligible set and returns the previous
// one, reporting whether it was taken under the same rule.
func (d *Daemon) rememberPoll(key string, current polledIssues) (polledIssues, bool) {
d.lastPolledMu.Lock()
defer d.lastPolledMu.Unlock()
previous, seen := d.lastPolled[key]
if d.lastPolled == nil {
d.lastPolled = map[string]polledIssues{}
}
d.lastPolled[key] = current
return previous, seen && previous.rule == current.rule
}
// dispatchRule identifies the rule a poll selects issues by.
func dispatchRule(cfg config.Config) string {
return cfg.Dispatch.Trigger + "\x00" + cfg.Label + "\x00" + cfg.BotUser
}
// 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) {
func (d *Daemon) withdrawUnpolled(ctx context.Context, repo config.Repo, identity, rule string, polled []forge.Issue) {
if d.WithdrawTask == nil {
return
}
@@ -1087,6 +1117,14 @@ func (d *Daemon) withdrawUnpolled(ctx context.Context, repo config.Repo, identit
for _, is := range polled {
eligible[is.Number] = true
}
// Only an issue the same rule found eligible last time and no longer
// finds was withdrawn. A changed label, trigger or bot user, or the first
// poll after a restart, only records the new set: tasks picked up under
// the old rule were not withdrawn by anyone.
previous, sameRule := d.rememberPoll(identity+"\x00"+repo.FullName(), polledIssues{rule: rule, eligible: eligible})
if !sameRule {
return
}
active, err := d.Store.TasksPage(ctx, storecontract.TaskPage{
Statuses: []string{taskstate.Queued, taskstate.Running}, Limit: 500,
})
@@ -1096,7 +1134,7 @@ func (d *Daemon) withdrawUnpolled(ctx context.Context, repo config.Repo, identit
}
for _, t := range active {
if t.Owner != repo.Owner || t.Repo != repo.Name || t.Identity != identity ||
!t.IsForgeBacked() || t.PRNumber > 0 || eligible[t.IssueNumber] {
!t.IsForgeBacked() || t.PRNumber > 0 || eligible[t.IssueNumber] || !previous.eligible[t.IssueNumber] {
continue
}
if err := d.WithdrawTask(ctx, t.ID, "the issue is no longer assigned, labelled or open"); err != nil {
+53
View File
@@ -0,0 +1,53 @@
package daemon
import (
"context"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/forge"
)
type activeTasks struct {
storecontract.TaskStore
tasks []workflow.Task
}
func (a activeTasks) TasksPage(context.Context, storecontract.TaskPage) ([]workflow.Task, error) {
return a.tasks, nil
}
// Only an issue the same dispatch rule found eligible and then lost is
// withdrawn: a first poll, or a poll under a changed rule, withdraws nothing.
func TestWithdrawUnpolled(t *testing.T) {
repo := config.Repo{Owner: "acme", Name: "widget"}
task := workflow.Task{ID: 7, Owner: "acme", Repo: "widget", IssueNumber: 3, Source: "forge", Status: "running"}
steps := []struct {
name string
rule string
polls []int
want bool
}{
{"first poll only records", "label:archie", nil, false},
{"issue becomes eligible", "label:archie", []int{3}, false},
{"rule changed: no withdrawal", "label:bot", nil, false},
{"eligible again under the new rule", "label:bot", []int{3}, false},
{"dropped under the same rule: withdrawn", "label:bot", nil, true},
}
d := &Daemon{Log: slog.New(slog.DiscardHandler), Store: activeTasks{tasks: []workflow.Task{task}}}
for _, step := range steps {
withdrawn := false
d.WithdrawTask = func(context.Context, int64, string) error { withdrawn = true; return nil }
var issues []forge.Issue
for _, n := range step.polls {
issues = append(issues, forge.Issue{Number: n})
}
d.withdrawUnpolled(t.Context(), repo, "", step.rule, issues)
if withdrawn != step.want {
t.Fatalf("%s: withdrawn = %v, want %v", step.name, withdrawn, step.want)
}
}
}