Implement max_retries cap on maybeRetryParked to prevent infinite parked-task loops (archie)

Implements #61
This commit is contained in:
archie committed 2026-07-23 18:49:38 +10:00
1 parent 7534cbe2c4
commit d2b2f29096
11 files changed
+402 -19

No files matched your search

+40 -4
View File
@@ -322,8 +322,9 @@ func (d *Daemon) pollEither(ctx context.Context, repo config.Repo) []forge.Issue
return out
}
// maybeRetryParked requeues a parked task whose archie:parked label a
// human has removed — the forge-native retry trigger.
// maybeRetryParked requeues a parked task whose state label a human has
// removed — the forge-native retry trigger. When retry_count reaches the
// configured max_retries the task moves to dead instead of requeuing.
func (d *Daemon) maybeRetryParked(ctx context.Context, repo config.Repo, is forge.Issue) {
if hasLabel(is.Labels, d.Cfg.Dispatch.StateLabel("parked")) {
return
@@ -332,15 +333,50 @@ func (d *Daemon) maybeRetryParked(ctx context.Context, repo config.Repo, is forg
if err != nil || task == nil || task.Status != store.StatusParked {
return
}
maxRetries := repo.EffectiveMaxRetries(d.Cfg.MaxRetries)
if task.RetryCount >= maxRetries {
d.markDead(ctx, repo, task, maxRetries)
return
}
if err := d.Store.Requeue(ctx, task.ID, store.StatusParked, ""); err != nil {
d.Log.Error("label-triggered requeue failed", "issue", is.Number, "err", err)
return
}
d.Log.Info("parked task requeued via label removal", "repo", repo.FullName(), "issue", is.Number)
if err := d.Store.IncrementRetryCount(ctx, task.ID); err != nil {
d.Log.Error("increment retry_count failed", "issue", is.Number, "err", err)
}
d.Log.Info("parked task requeued via label removal", "repo", repo.FullName(), "issue", is.Number, "retry_count", task.RetryCount+1, "max_retries", maxRetries)
d.Forge.SetStateLabel(ctx, repo.Owner, repo.Name, is.Number, d.Cfg.Dispatch.StateLabel("queued"), d.Cfg.Dispatch.LabelValues())
d.emit(events.Event{
Kind: "task_retried", TaskID: task.ID,
Repo: repo.FullName(), Issue: is.Number, Detail: "archie:parked label removed",
Repo: repo.FullName(), Issue: is.Number, Detail: "parked label removed",
})
}
// markDead permanently parks a task that has exhausted its max_retries.
func (d *Daemon) markDead(ctx context.Context, repo config.Repo, task *store.Task, maxRetries int) {
reason := fmt.Sprintf("max retries reached (%d/%d) — task parked permanently", task.RetryCount, maxRetries)
if err := d.Store.Transition(ctx, task.ID, store.StatusParked, store.StatusDead, reason); err != nil {
d.Log.Error("transition to dead failed", "task", task.ID, "err", err)
return
}
d.Forge.SetStateLabel(ctx, task.Owner, task.Repo, task.IssueNumber,
d.Cfg.Dispatch.StateLabel("dead"), d.Cfg.Dispatch.LabelValues())
d.Log.Warn("task permanently parked after max retries",
"issue", task.IssueNumber, "retry_count", task.RetryCount, "max_retries", maxRetries)
body := fmt.Sprintf("**Max retries reached (%d/%d) — task parked permanently.**\n\n"+
"The task failed %d times and has been moved to `dead` status. "+
"Manual intervention is required to recover this task.",
task.RetryCount, maxRetries, task.RetryCount)
if _, err := d.Forge.Comment(ctx, task.Owner, task.Repo, task.IssueNumber, body); err != nil {
d.Log.Error("failed to post dead comment", "issue", task.IssueNumber, "err", err)
}
d.emit(events.Event{
Kind: events.KindTaskDead, TaskID: task.ID,
Repo: repo.FullName(), Issue: task.IssueNumber, Detail: reason,
})
}
+222 -1
View File
@@ -1,6 +1,16 @@
package daemon
import "testing"
import (
"context"
"log/slog"
"os"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/store"
)
func TestHasLabelUsesExactNames(t *testing.T) {
labels := []string{"bug", "archie:parked-old", "feature", "custom,label"}
@@ -15,3 +25,214 @@ func TestHasLabelUsesExactNames(t *testing.T) {
t.Fatal("hasLabel did not match the exact label name")
}
}
// testForge implements forge.Forge for daemon tests.
type testForge struct {
comments []commentCall
stateLabels []stateLabelCall
}
type commentCall struct {
owner, repo string
number int
body string
}
type stateLabelCall struct {
owner, repo string
number int
label string
knownLabels []string
}
func (f *testForge) Comment(_ context.Context, owner, repo string, number int, body string) (int64, error) {
f.comments = append(f.comments, commentCall{owner, repo, number, body})
return 1, nil
}
func (f *testForge) SetStateLabel(_ context.Context, owner, repo string, number int, label string, knownLabels []string) {
f.stateLabels = append(f.stateLabels, stateLabelCall{owner, repo, number, label, knownLabels})
}
func (f *testForge) AcceptInvitations(context.Context) error { return nil }
func (f *testForge) AssignedIssues(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *testForge) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *testForge) RepliesAfter(context.Context, string, string, int, int64, string) ([]forge.Reply, error) {
panic("unexpected call")
}
func (f *testForge) CreatePR(context.Context, string, string, string, string, string, string) (int, error) {
panic("unexpected call")
}
func (f *testForge) PRState(context.Context, string, string, int) (string, error) { panic("unexpected call") }
func (f *testForge) CloseIssue(context.Context, string, string, int, string) error { panic("unexpected call") }
func (f *testForge) React(context.Context, string, string, int, string) error { return nil }
func (f *testForge) VerifyPush(context.Context, string, string) error { return nil }
func testDaemon(t *testing.T, maxRetries int, _ int) (*Daemon, *store.Store, *testForge) {
t.Helper()
s := store.OpenTest(t)
fg := &testForge{}
cfg := config.Config{
MaxRetries: maxRetries,
Dispatch: config.Dispatch{
Labels: map[string]string{
"parked": "archie:parked",
"queued": "archie:queued",
"dead": "archie:dead",
},
},
}
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
return &Daemon{
Cfg: cfg,
Store: s,
Forge: fg,
Log: log,
}, s, fg
}
func setupParkedTask(t *testing.T, s *store.Store, retryCount int) *store.Task {
t.Helper()
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "sam", "todo", 1, "t", "b", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%v, %v)", task, err)
}
if err := s.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "parked"); err != nil {
t.Fatal(err)
}
for range retryCount {
if err := s.IncrementRetryCount(ctx, task.ID); err != nil {
t.Fatal(err)
}
}
task, err = s.TaskByIssue(ctx, "sam", "todo", 1)
if err != nil || task == nil {
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
}
return task
}
func TestMaybeRetryParkedUnderThreshold(t *testing.T) {
d, s, fg := testDaemon(t, 3, 0)
ctx := context.Background()
_ = setupParkedTask(t, s, 0)
is := forge.Issue{Number: 1, Labels: []string{}}
repo := config.Repo{Owner: "sam", Name: "todo"}
d.maybeRetryParked(ctx, repo, is)
task, err := s.TaskByIssue(ctx, "sam", "todo", 1)
if err != nil || task == nil {
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
}
if task.Status != store.StatusQueued {
t.Fatalf("expected status queued, got %s", task.Status)
}
if task.RetryCount != 1 {
t.Fatalf("expected retry_count=1, got %d", task.RetryCount)
}
if len(fg.stateLabels) != 1 || fg.stateLabels[0].label != "archie:queued" {
t.Fatalf("expected queued state label, got %+v", fg.stateLabels)
}
if len(fg.comments) != 0 {
t.Fatalf("expected no comments, got %d", len(fg.comments))
}
}
func TestMaybeRetryParkedAtThreshold(t *testing.T) {
d, s, fg := testDaemon(t, 3, 0)
ctx := context.Background()
_ = setupParkedTask(t, s, 3)
is := forge.Issue{Number: 1, Labels: []string{}}
repo := config.Repo{Owner: "sam", Name: "todo"}
d.maybeRetryParked(ctx, repo, is)
task, err := s.TaskByIssue(ctx, "sam", "todo", 1)
if err != nil || task == nil {
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
}
if task.Status != store.StatusDead {
t.Fatalf("expected status dead, got %s", task.Status)
}
if len(fg.stateLabels) != 1 || fg.stateLabels[0].label != "archie:dead" {
t.Fatalf("expected dead state label, got %+v", fg.stateLabels)
}
if len(fg.comments) != 1 || !strings.Contains(fg.comments[0].body, "Max retries reached") {
t.Fatalf("expected dead comment, got %+v", fg.comments)
}
}
func TestMaybeRetryParkedSkipsDead(t *testing.T) {
d, s, fg := testDaemon(t, 3, 0)
ctx := context.Background()
task := setupParkedTask(t, s, 0)
if err := s.Transition(ctx, task.ID, store.StatusParked, store.StatusDead, "manual"); err != nil {
t.Fatal(err)
}
is := forge.Issue{Number: 1, Labels: []string{}}
repo := config.Repo{Owner: "sam", Name: "todo"}
d.maybeRetryParked(ctx, repo, is)
if len(fg.stateLabels) != 0 {
t.Fatalf("expected no state label calls, got %d", len(fg.stateLabels))
}
if len(fg.comments) != 0 {
t.Fatalf("expected no comments, got %d", len(fg.comments))
}
}
func TestMaybeRetryParkedStillParked(t *testing.T) {
d, _, fg := testDaemon(t, 3, 0)
ctx := context.Background()
is := forge.Issue{Number: 1, Labels: []string{"archie:parked", "bug"}}
repo := config.Repo{Owner: "sam", Name: "todo"}
d.maybeRetryParked(ctx, repo, is)
if len(fg.stateLabels) != 0 {
t.Fatalf("expected no state label calls, got %d", len(fg.stateLabels))
}
}
func TestMaybeRetryParkedRepoOverride(t *testing.T) {
d, s, _ := testDaemon(t, 3, 1)
ctx := context.Background()
_ = setupParkedTask(t, s, 1)
is := forge.Issue{Number: 1, Labels: []string{}}
repo := config.Repo{Owner: "sam", Name: "todo", MaxRetries: 1}
d.maybeRetryParked(ctx, repo, is)
task, err := s.TaskByIssue(ctx, "sam", "todo", 1)
if err != nil || task == nil {
t.Fatalf("TaskByIssue = (%+v, %v)", task, err)
}
if task.Status != store.StatusDead {
t.Fatalf("expected status dead with repo override, got %s", task.Status)
}
}