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(repo): extract run notification binding selection from enqueueRunNotifications (#603)

fx-kurumief94871807d2
diff --git a/internal/repo/notifications.go b/internal/repo/notifications.go
index b50d6738..78d519f9 100644
--- a/internal/repo/notifications.go
+++ b/internal/repo/notifications.go
@@ -189,28 +189,7 @@ func (r *NotificationsRepo) enqueueRunNotifications(tx *gorm.DB, run domain.Run,
nodes := append([]domain.WorkflowNode(nil), run.WorkflowSnapshot.Nodes...)
nodes = append(nodes, domain.WorkflowNode{Type: "agent", Notifications: domain.WorkflowNotifications(run.WorkflowSnapshot)})
for _, node := range nodes {
- bindings := domain.NodeReminderBindings(domain.WorkflowNotifications(run.WorkflowSnapshot))
- if node.ID == "" {
- bindings = node.Notifications
- if runTrigger == "failed" {
- // Setup/runtime failures may end a SOP without a node failure event.
- hasNodeFailure := false
- for _, prior := range run.Events {
- if notificationTrigger(prior.Type) == "failed" && prior.NodeID != "" {
- hasNodeFailure = true
- break
- }
- }
- if !hasNodeFailure {
- bindings = domain.NodeReminderBindings(node.Notifications)
- for i := range bindings {
- bindings[i].Scope = "run"
- bindings[i].Events = []string{"failed"}
- }
- }
- }
- }
- for _, binding := range bindings {
+ for _, binding := range runNotificationBindings(run, node, runTrigger) {
trigger := nodeTrigger
nodeID := node.ID
labelPrefix := "节点"
@@ -258,6 +237,36 @@ func (r *NotificationsRepo) enqueueRunNotifications(tx *gorm.DB, run domain.Run,
}
return nil
}
+
+// runNotificationBindings selects the bindings evaluated for one node of a run.
+// Workflow nodes use the SOP reminder bindings; the synthetic SOP-level node
+// (empty ID) uses its own bindings, falling back to run-scoped "failed"
+// reminders when the run failed without any node failure event.
+func runNotificationBindings(run domain.Run, node domain.WorkflowNode, runTrigger string) []domain.NotificationBinding {
+ if node.ID != "" {
+ return domain.NodeReminderBindings(domain.WorkflowNotifications(run.WorkflowSnapshot))
+ }
+ // Setup/runtime failures may end a SOP without a node failure event.
+ if runTrigger != "failed" || runHasNodeFailureEvent(run.Events) {
+ return node.Notifications
+ }
+ bindings := domain.NodeReminderBindings(node.Notifications)
+ for i := range bindings {
+ bindings[i].Scope = "run"
+ bindings[i].Events = []string{"failed"}
+ }
+ return bindings
+}
+
+// runHasNodeFailureEvent reports whether any recorded event is a node-level failure.
+func runHasNodeFailureEvent(events []domain.RunEvent) bool {
+ for _, event := range events {
+ if notificationTrigger(event.Type) == "failed" && event.NodeID != "" {
+ return true
+ }
+ }
+ return false
+}
func (r *NotificationsRepo) enqueueTaskNotifications(tx *gorm.DB, sourceType, id, projectID, name, trigger string, at time.Time, bindings []domain.NotificationBinding, result, errorText string) error {
for _, binding := range bindings {
if !notificationEventSelected(binding, trigger) {
diff --git a/internal/repo/notifications_test.go b/internal/repo/notifications_test.go
index deffe8ea..7b055eab 100644
--- a/internal/repo/notifications_test.go
+++ b/internal/repo/notifications_test.go
@@ -229,3 +229,49 @@ func TestDisabledBotDoesNotPreventEditingItsWorkflow(t *testing.T) {
t.Fatal("disabled bot enqueued notification")
}
}
+
+func TestRunNotificationBindings(t *testing.T) {
+ sopBindings := []domain.NotificationBinding{{BotID: "bot-sop", Events: []string{"succeeded", "failed"}, Scope: "run"}}
+ run := domain.Run{WorkflowSnapshot: domain.WorkflowDefinition{Notifications: sopBindings, Nodes: []domain.WorkflowNode{{ID: "agent", Type: "agent"}}}}
+ sopNode := domain.WorkflowNode{Type: "agent", Notifications: sopBindings}
+ nodeFailed := []domain.RunEvent{{NodeID: "agent", Type: "node.failed"}}
+
+ got := runNotificationBindings(run, domain.WorkflowNode{ID: "agent"}, "failed")
+ if len(got) != 1 || got[0].BotID != "bot-sop" || got[0].Scope != "node" || fmt.Sprint(got[0].Events) != "[waiting failed]" {
+ t.Fatalf("workflow node should use SOP reminder bindings, got %+v", got)
+ }
+ if got := runNotificationBindings(run, sopNode, "succeeded"); fmt.Sprint(got) != fmt.Sprint(sopBindings) {
+ t.Fatalf("SOP node on success should keep its own bindings, got %+v", got)
+ }
+ run.Events = nodeFailed
+ if got := runNotificationBindings(run, sopNode, "failed"); fmt.Sprint(got) != fmt.Sprint(sopBindings) {
+ t.Fatalf("SOP node after a node failure should keep its own bindings, got %+v", got)
+ }
+ run.Events = nil
+ got = runNotificationBindings(run, sopNode, "failed")
+ if len(got) != 1 || got[0].BotID != "bot-sop" || got[0].Scope != "run" || fmt.Sprint(got[0].Events) != "[failed]" || got[0].WaitingPolicy != "actionable" {
+ t.Fatalf("SOP node failing without node failure should fall back to run-scoped failed reminders, got %+v", got)
+ }
+ if fmt.Sprint(sopBindings[0].Events) != "[succeeded failed]" {
+ t.Fatalf("fallback must not mutate the workflow bindings, got %+v", sopBindings)
+ }
+}
+
+func TestRunHasNodeFailureEvent(t *testing.T) {
+ cases := []struct {
+ name string
+ events []domain.RunEvent
+ want bool
+ }{
+ {"empty", nil, false},
+ {"run-level failure", []domain.RunEvent{{Type: "node.failed"}}, false},
+ {"node success", []domain.RunEvent{{NodeID: "agent", Type: "node.succeeded"}}, false},
+ {"node failure", []domain.RunEvent{{NodeID: "agent", Type: "node.succeeded"}, {NodeID: "agent", Type: "node.failed"}}, true},
+ {"declined turns", []domain.RunEvent{{NodeID: "agent", Type: "agent.turns.declined"}}, true},
+ }
+ for _, tc := range cases {
+ if got := runHasNodeFailureEvent(tc.events); got != tc.want {
+ t.Errorf("%s: runHasNodeFailureEvent = %v, want %v", tc.name, got, tc.want)
+ }
+ }
+}