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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
【架构优化】从 syncTasks 中拆出 Issue 最新 Task 选取 latestTaskByIssue 与 Issue 回写 syncIssuesToLatestTask (#820)
fx-kurumice0a29f212b8
diff --git a/internal/httpapi/tasks.go b/internal/httpapi/tasks.goindex 59a675c9..173a1599 100644--- a/internal/httpapi/tasks.go+++ b/internal/httpapi/tasks.go@@ -99,6 +99,12 @@ func (h *tasksHandlers) syncTasks() {_ = h.repo.UpdateTaskStatus(task, status)tasks[i].Status = status}+ h.syncIssuesToLatestTask(latestTaskByIssue(tasks))+}++// latestTaskByIssue picks each issue's newest task: the later CreatedAt wins,+// and on a tie the larger ID wins. Tasks without an IssueID are skipped.+func latestTaskByIssue(tasks []domain.Task) map[string]domain.Task {latest := map[string]domain.Task{}for _, task := range tasks {if task.IssueID == "" {@@ -109,6 +115,12 @@ func (h *tasksHandlers) syncTasks() {latest[task.IssueID] = task}}+ return latest+}++// syncIssuesToLatestTask points every issue at its latest task and derives the+// issue status from it, patching only issues whose linkage or status changed.+func (h *tasksHandlers) syncIssuesToLatestTask(latest map[string]domain.Task) {for _, issue := range h.repo.Issues() {task, ok := latest[issue.ID]if !ok {diff --git a/internal/httpapi/tasks_latest_sync_guard_test.go b/internal/httpapi/tasks_latest_sync_guard_test.gonew file mode 100644index 00000000..b470e1c6--- /dev/null+++ b/internal/httpapi/tasks_latest_sync_guard_test.go@@ -0,0 +1,53 @@+package httpapi++import (+ "testing"++ "agentflow/internal/domain"+ "agentflow/internal/repo"+)++// syncIssuesToLatestTask must refresh an issue whose latest task is unchanged+// but whose task status moved on, and must leave issues without any task in+// the map untouched (no blank LatestTaskID, no status rewrite).+func TestSyncIssuesToLatestTaskStatusOnlyAndUntracked(t *testing.T) {+ s, err := repo.OpenTest()+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { s.Close() })+ h := &tasksHandlers{deps: &deps{repo: s}}++ for _, issue := range []domain.Issue{+ {ID: "same-link", ProjectID: "platform", Title: "status only", Status: "in_progress", LatestTaskID: "task-1"},+ {ID: "no-task", ProjectID: "platform", Title: "untracked", Status: "blocked", LatestTaskID: "task-gone"},+ {ID: "relink", ProjectID: "platform", Title: "relink", Status: "todo", LatestTaskID: "task-old"},+ } {+ if err := s.PutIssue(issue); err != nil {+ t.Fatal(err)+ }+ }++ h.syncIssuesToLatestTask(map[string]domain.Task{+ "same-link": {ID: "task-1", IssueID: "same-link", Status: "completed"},+ "relink": {ID: "task-new", IssueID: "relink", Status: "running"},+ })++ got := func(id string) domain.Issue {+ t.Helper()+ issue, ok := s.Issue(id)+ if !ok {+ t.Fatalf("issue %s missing", id)+ }+ return issue+ }+ if issue := got("same-link"); issue.LatestTaskID != "task-1" || issue.Status != "in_review" {+ t.Fatalf("status-only change not synced: %+v", issue)+ }+ if issue := got("no-task"); issue.LatestTaskID != "task-gone" || issue.Status != "blocked" {+ t.Fatalf("issue without a latest task must be left alone: %+v", issue)+ }+ if issue := got("relink"); issue.LatestTaskID != "task-new" || issue.Status != "in_progress" {+ t.Fatalf("relinked issue = %+v", issue)+ }+}diff --git a/internal/httpapi/tasks_latest_sync_noop_test.go b/internal/httpapi/tasks_latest_sync_noop_test.gonew file mode 100644index 00000000..5bc8851b--- /dev/null+++ b/internal/httpapi/tasks_latest_sync_noop_test.go@@ -0,0 +1,58 @@+package httpapi++import (+ "testing"+ "time"++ "agentflow/internal/domain"+ "agentflow/internal/repo"+)++// syncTasks runs every two seconds, so syncIssuesToLatestTask must only write+// issues whose linkage or derived status actually changed. An issue already+// pointing at its latest task with the matching status keeps its UpdatedAt,+// and a task whose issue no longer exists must not resurrect that issue.+func TestSyncIssuesToLatestTaskSkipsUnchangedAndMissingIssues(t *testing.T) {+ s, err := repo.OpenTest()+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { s.Close() })+ h := &tasksHandlers{deps: &deps{repo: s}}++ for _, issue := range []domain.Issue{+ {ID: "in-sync", ProjectID: "platform", Title: "in sync", Status: "in_progress", LatestTaskID: "task-run"},+ {ID: "closed-in-sync", ProjectID: "platform", Title: "closed", Status: "closed", LatestTaskID: "task-closed"},+ } {+ if err := s.PutIssue(issue); err != nil {+ t.Fatal(err)+ }+ }+ before := map[string]domain.Issue{}+ for _, id := range []string{"in-sync", "closed-in-sync"} {+ issue, ok := s.Issue(id)+ if !ok {+ t.Fatalf("issue %s missing", id)+ }+ before[id] = issue+ }++ // Any write after this point would bump UpdatedAt.+ time.Sleep(20 * time.Millisecond)++ h.syncIssuesToLatestTask(map[string]domain.Task{+ "in-sync": {ID: "task-run", IssueID: "in-sync", Status: "running"},+ "closed-in-sync": {ID: "task-closed", IssueID: "closed-in-sync", Status: "running"},+ "ghost": {ID: "task-ghost", IssueID: "ghost", Status: "running"},+ })++ for id, prev := range before {+ got, _ := s.Issue(id)+ if got.LatestTaskID != prev.LatestTaskID || got.Status != prev.Status || !got.UpdatedAt.Equal(prev.UpdatedAt) {+ t.Fatalf("issue %s was rewritten although nothing changed: before %+v after %+v", id, prev, got)+ }+ }+ if _, ok := s.Issue("ghost"); ok {+ t.Fatal("a task for a missing issue must not create the issue")+ }+}diff --git a/internal/httpapi/tasks_latest_test.go b/internal/httpapi/tasks_latest_test.gonew file mode 100644index 00000000..f207c19b--- /dev/null+++ b/internal/httpapi/tasks_latest_test.go@@ -0,0 +1,40 @@+package httpapi++import (+ "testing"+ "time"++ "agentflow/internal/domain"+)++func TestLatestTaskByIssue(t *testing.T) {+ base := time.Date(2026, 11, 14, 12, 0, 0, 0, time.UTC)+ tasks := []domain.Task{+ {ID: "orphan", CreatedAt: base.Add(time.Hour)},+ {ID: "a-old", IssueID: "issue-a", CreatedAt: base},+ {ID: "a-new", IssueID: "issue-a", CreatedAt: base.Add(time.Minute)},+ {ID: "a-older", IssueID: "issue-a", CreatedAt: base.Add(-time.Minute)},+ {ID: "b-2", IssueID: "issue-b", CreatedAt: base},+ {ID: "b-9", IssueID: "issue-b", CreatedAt: base},+ {ID: "b-1", IssueID: "issue-b", CreatedAt: base},+ }+ latest := latestTaskByIssue(tasks)+ if len(latest) != 2 {+ t.Fatalf("latest = %+v, want only issue-a and issue-b", latest)+ }+ if _, ok := latest[""]; ok {+ t.Fatalf("task without issue should be skipped: %+v", latest)+ }+ if got := latest["issue-a"].ID; got != "a-new" {+ t.Fatalf("issue-a latest = %q, want a-new (latest CreatedAt)", got)+ }+ if got := latest["issue-b"].ID; got != "b-9" {+ t.Fatalf("issue-b latest = %q, want b-9 (largest ID on CreatedAt tie)", got)+ }+}++func TestLatestTaskByIssueEmpty(t *testing.T) {+ if latest := latestTaskByIssue(nil); len(latest) != 0 {+ t.Fatalf("latest = %+v, want empty", latest)+ }+}