Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(schedule): split eligible-issue filter and repo-block check out of execute (#292)

fx-kurumi053f87f264bd
diff --git a/internal/schedule/execute_helpers_test.go b/internal/schedule/execute_helpers_test.go
new file mode 100644
index 00000000..f1f9289c
--- /dev/null
+++ b/internal/schedule/execute_helpers_test.go
@@ -0,0 +1,75 @@
+package schedule
+
+import (
+ "slices"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/repo"
+ "agentflow/internal/tracker"
+)
+
+func TestEligibleIssuesFiltersUndispatchable(t *testing.T) {
+ st, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(st.Close)
+ if err := st.PutTask(domain.Task{ID: "t", IssueID: "has-task", RunID: "r", Status: "completed"}); err != nil {
+ t.Fatal(err)
+ }
+ svc := New(st, tracker.NewService(), nil)
+ matched := []domain.Issue{
+ {ID: "ok", Status: "todo"},
+ {ID: "has-task", Status: "todo"},
+ {ID: "merged", Status: "todo", DeliveryState: "merged"},
+ {ID: "gf-closed", Status: "todo", Source: "gongfeng", ExternalRef: &domain.ExternalRef{ExternalState: "closed"}},
+ {ID: "done", Status: "done"},
+ {ID: "closed", Status: "closed"},
+ {ID: "cancelled", Status: "cancelled"},
+ {ID: "ignored", Status: "todo", AutomationState: "ignored"},
+ {ID: "own-wf", Status: "todo", WorkflowID: "issue-wf"},
+ }
+ got := func(job domain.ScheduleJob) []string {
+ var ids []string
+ for _, issue := range svc.eligibleIssues(job, matched) {
+ ids = append(ids, issue.ID)
+ }
+ return ids
+ }
+ if ids := got(domain.ScheduleJob{WorkflowID: "wf"}); !slices.Equal(ids, []string{"ok", "own-wf"}) {
+ t.Fatalf("eligible = %v", ids)
+ }
+ if ids := got(domain.ScheduleJob{UseIssueWorkflow: true}); !slices.Equal(ids, []string{"own-wf"}) {
+ t.Fatalf("issue-workflow eligible = %v", ids)
+ }
+}
+
+func TestIssueBlocked(t *testing.T) {
+ st, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(st.Close)
+ if err := st.PutProject(domain.Project{ID: "p", Name: "P", DefaultRepositoryID: "default"}); err != nil {
+ t.Fatal(err)
+ }
+ blocked := map[string]domain.ScheduleJobBlocker{"b": {Reason: "conflict"}, "default": {Reason: "conflict"}}
+ tests := []struct {
+ issue domain.Issue
+ want bool
+ }{
+ {domain.Issue{ID: "single-free", RepositoryID: "a"}, false},
+ {domain.Issue{ID: "multi-one-blocked", RepositoryIDs: []string{"a", "b"}}, true},
+ {domain.Issue{ID: "project-default", ProjectID: "p"}, true},
+ {domain.Issue{ID: "no-repo", ProjectID: "missing"}, false},
+ }
+ for _, tc := range tests {
+ if got := issueBlocked(st, tc.issue, blocked); got != tc.want {
+ t.Fatalf("%s: blocked=%v want %v", tc.issue.ID, got, tc.want)
+ }
+ if issueBlocked(st, tc.issue, nil) {
+ t.Fatalf("%s: blocked with no blockers", tc.issue.ID)
+ }
+ }
+}
diff --git a/internal/schedule/service.go b/internal/schedule/service.go
index 307731b6..69cdb26e 100644
--- a/internal/schedule/service.go
+++ b/internal/schedule/service.go
@@ -272,17 +272,7 @@ func (s *Service) execute(job domain.ScheduleJob, triggerOrigin string) (domain.
}
matched := s.matchingIssues(job)
job.LastPulled = len(matched)
- var eligible []domain.Issue
- for _, issue := range matched {
- workflowID := job.ResolveWorkflow(issue)
- if workflowID == "" {
- continue
- }
- if s.repo.IssueHasTask(issue.ID) || issue.DeliveryLocked() || issue.GongfengClosed() || issue.Status == "done" || issue.Status == "closed" || issue.Status == "cancelled" || issue.AutomationState == "ignored" {
- continue
- }
- eligible = append(eligible, issue)
- }
+ eligible := s.eligibleIssues(job, matched)
blocked := map[string]domain.ScheduleJobBlocker{}
if s.guard != nil && len(eligible) > 0 {
var guardErr error
@@ -302,16 +292,7 @@ func (s *Service) execute(job domain.ScheduleJob, triggerOrigin string) (domain.
if dispatched >= limit {
break
}
- // A multi-repository issue must wait when any of its repositories has
- // an integration blocker.
- waiting := false
- for _, repoID := range issueRepositoryIDs(s.repo, issue) {
- if _, isBlocked := blocked[repoID]; isBlocked {
- waiting = true
- break
- }
- }
- if waiting {
+ if issueBlocked(s.repo, issue, blocked) {
continue
}
if err := s.dispatch(job, issue, triggerOrigin); err != nil {
@@ -342,6 +323,34 @@ func (s *Service) execute(job domain.ScheduleJob, triggerOrigin string) (domain.
return job, nil
}
+// eligibleIssues keeps the matched issues the job may dispatch: a workflow
+// resolves for them, they have no task yet, and they are neither locked,
+// closed, finished nor ignored by automation.
+func (s *Service) eligibleIssues(job domain.ScheduleJob, matched []domain.Issue) []domain.Issue {
+ var eligible []domain.Issue
+ for _, issue := range matched {
+ if job.ResolveWorkflow(issue) == "" {
+ continue
+ }
+ if s.repo.IssueHasTask(issue.ID) || issue.DeliveryLocked() || issue.GongfengClosed() || issue.Status == "done" || issue.Status == "closed" || issue.Status == "cancelled" || issue.AutomationState == "ignored" {
+ continue
+ }
+ eligible = append(eligible, issue)
+ }
+ return eligible
+}
+
+// issueBlocked reports whether the issue must wait: a multi-repository issue
+// waits when any of its repositories has an integration blocker.
+func issueBlocked(st *repo.Store, issue domain.Issue, blocked map[string]domain.ScheduleJobBlocker) bool {
+ for _, repoID := range issueRepositoryIDs(st, issue) {
+ if _, ok := blocked[repoID]; ok {
+ return true
+ }
+ }
+ return false
+}
+
// issueRepositoryIDs lists every repository a scheduled issue touches:
// the bound repositories first, then the project default as fallback.
func issueRepositoryIDs(st *repo.Store, issue domain.Issue) []string {