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.

【架构优化】合并 notification.ProcessOne 中重复的等待节点过期判断 (#239)

fx-kurumiddd6d881946b
diff --git a/internal/notification/service.go b/internal/notification/service.go
index dc1c35eb..530291a9 100644
--- a/internal/notification/service.go
+++ b/internal/notification/service.go
@@ -117,6 +117,15 @@ func (s *Service) Start(ctx context.Context) {
}
}()
}
+
+// waitingNodeStale reports whether the node a waiting notification refers to
+// is no longer waiting for the same approval round (run finished, node gone,
+// status changed, or a newer attempt/rework has started).
+func waitingNodeStale(run domain.Run, item domain.NotificationDelivery) bool {
+ node := run.Nodes[item.NodeID]
+ return domain.TerminalRunStatus(run.Status) || node == nil || node.Status != "waiting_approval" || node.Attempt != item.Attempt || node.ReworkCount != item.ReworkCount
+}
+
func (s *Service) ProcessOne(ctx context.Context) (bool, error) {
token := make([]byte, 16)
if _, err := rand.Read(token); err != nil {
@@ -157,15 +166,13 @@ func (s *Service) ProcessOne(ctx context.Context) (bool, error) {
if !ok {
return finish("expired", "运行已删除")
}
- node := run.Nodes[item.NodeID]
- if item.EventType == "waiting" && item.WaitingPolicy != "event" && (domain.TerminalRunStatus(run.Status) || node == nil || node.Status != "waiting_approval" || node.Attempt != item.Attempt || node.ReworkCount != item.ReworkCount) {
+ if item.EventType == "waiting" && item.WaitingPolicy != "event" && waitingNodeStale(run, *item) {
return finish("expired", "节点已无需人工处理")
}
}
if item.EventType == "waiting" && item.WaitingPolicy == "event" {
run, _ := s.Store.RunState(item.RunID)
- node := run.Nodes[item.NodeID]
- if domain.TerminalRunStatus(run.Status) || node == nil || node.Status != "waiting_approval" || node.Attempt != item.Attempt || node.ReworkCount != item.ReworkCount {
+ if waitingNodeStale(run, *item) {
item.Content += "\n\n此消息记录进入等待的状态;当前该次等待已结束,无需按旧提醒操作。"
}
}
@@ -201,8 +208,7 @@ func (s *Service) ProcessOne(ctx context.Context) (bool, error) {
if !ok {
return errNotificationExpired
}
- node := run.Nodes[item.NodeID]
- if domain.TerminalRunStatus(run.Status) || node == nil || node.Status != "waiting_approval" || node.Attempt != item.Attempt || node.ReworkCount != item.ReworkCount {
+ if waitingNodeStale(run, *item) {
return errNotificationExpired
}
return nil
diff --git a/internal/notification/waiting_stale_test.go b/internal/notification/waiting_stale_test.go
new file mode 100644
index 00000000..77642587
--- /dev/null
+++ b/internal/notification/waiting_stale_test.go
@@ -0,0 +1,95 @@
+package notification
+
+import (
+ "context"
+ "strings"
+ "testing"
+ "time"
+
+ "agentflow/internal/domain"
+)
+
+func TestWaitingNodeStale(t *testing.T) {
+ item := domain.NotificationDelivery{NodeID: "n", Attempt: 2, ReworkCount: 1}
+ waiting := func() *domain.NodeRun {
+ return &domain.NodeRun{NodeID: "n", Status: "waiting_approval", Attempt: 2, ReworkCount: 1}
+ }
+ cases := []struct {
+ name string
+ status string
+ node func() *domain.NodeRun
+ want bool
+ }{
+ {"still waiting", "waiting", waiting, false},
+ {"terminal run", "succeeded", waiting, true},
+ {"node missing", "waiting", func() *domain.NodeRun { return nil }, true},
+ {"node no longer waiting", "running", func() *domain.NodeRun { n := waiting(); n.Status = "running"; return n }, true},
+ {"newer attempt", "waiting", func() *domain.NodeRun { n := waiting(); n.Attempt = 3; return n }, true},
+ {"newer rework", "waiting", func() *domain.NodeRun { n := waiting(); n.ReworkCount = 2; return n }, true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ run := domain.Run{Status: tc.status, Nodes: map[string]*domain.NodeRun{}}
+ if node := tc.node(); node != nil {
+ run.Nodes["n"] = node
+ }
+ if got := waitingNodeStale(run, item); got != tc.want {
+ t.Fatalf("waitingNodeStale() = %v, want %v", got, tc.want)
+ }
+ })
+ }
+ if !waitingNodeStale(domain.Run{}, item) {
+ t.Fatal("zero-value run (deleted) must be stale")
+ }
+}
+
+// Delivery rows queued before waiting policies were standardized may still
+// carry WaitingPolicy "event". Those record the transition itself, so they are
+// delivered even after the wait ends, but must tell the reader it is over.
+func TestLegacyEventPolicyWaitingReminderMarksEndedWait(t *testing.T) {
+ const endedNote = "当前该次等待已结束"
+ for _, tc := range []struct {
+ name string
+ resolve bool
+ }{
+ {"still waiting", false},
+ {"wait ended before send", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ svc, f, bot, run := serviceFixture(t)
+ if err := svc.Store.DB.Where("run_id = ?", run.ID).Delete(&domain.NotificationDelivery{}).Error; err != nil {
+ t.Fatal(err)
+ }
+ if tc.resolve {
+ run.Status = "succeeded"
+ run.Nodes["n"].Status = "succeeded"
+ if err := svc.Store.PutRun(run); err != nil {
+ t.Fatal(err)
+ }
+ if err := svc.Store.DB.Where("run_id = ?", run.ID).Delete(&domain.NotificationDelivery{}).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ now := time.Now().UTC()
+ legacy := domain.NotificationDelivery{
+ ID: "legacy-event", ProjectID: bot.ProjectID, BotID: bot.ID, RunID: run.ID, NodeID: "n",
+ SourceType: "run", SourceID: run.ID, EventType: "waiting", WaitingPolicy: "event",
+ TargetID: "chat", TargetType: "group", TargetName: "研发群", ContentSource: "template",
+ Content: "节点进入等待", Status: "pending", Attempt: 1, CreatedAt: now, NextAttemptAt: now,
+ }
+ if err := svc.Store.CreateNotificationDelivery(legacy); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := svc.ProcessOne(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ items, _ := svc.Store.NotificationDeliveries(bot.ProjectID)
+ if f.sent != 1 || len(items) != 1 || items[0].Status != "sent" {
+ t.Fatalf("legacy event reminder not delivered: sent=%d items=%+v", f.sent, items)
+ }
+ if got := strings.Contains(f.content, endedNote); got != tc.resolve {
+ t.Fatalf("ended note present=%v, want %v; content=%q", got, tc.resolve, f.content)
+ }
+ })
+ }
+}